Home

Awesome

vuejs-clipper

Vue.js image clipping components using Vue-Rx.

not found status

Demo/Document

You can find the source code of examples under the examples branch.

Table of Contents

Notice

Before using the plugin & components, here's something you should know:

Installation (NPM & ESM)

Install vuejs-clipper

npm install vuejs-clipper --save

Install peer dependencies if you haven't.

npm install vue-rx rxjs --save

Vuejs-clipper is based on vue-rx, so make sure you have the vue-rx plugin installed.

import Vue from 'vue'
import VueRx from 'vue-rx'
// install vue-rx
Vue.use(VueRx)

Make sure to install vue-rx plugin first.

Import

use dist files (Recommanded)

import Vue from 'vue'
import VueRx from 'vue-rx'
// Use build files
import VuejsClipper from 'vuejs-clipper/dist/vuejs-clipper.umd.min'
import 'vuejs-clipper/dist/vuejs-clipper.css'

Vue.use(VueRx)
Vue.use(VuejsClipper)

use vuejs-clipper soruce

Not Recommanded, through this way you need to make sure your project' sass and sass-loader version is supported

You are using vuejs-clipper directly with your build process(webpack etc).

So make sure you have css loader, ex: sass-loader if you haven't installed :

vuejs-clipper@4 only support sass (not node-sass) for this approach

npm install -D sass-loader sass
import Vue from 'vue'
import VueRx from 'vue-rx'
import VuejsClipper from 'vuejs-clipper'
// install vue-rx
Vue.use(VueRx)
// install vuejs-clipper
Vue.use(VuejsClipper)

Config

By default vuejs-clipper plugin will register all components to Vue global scope.

register some components to global with default component name

Vue.use(VuejsClipper, {
  components: {
    clipperBasic: true,
    clipperPreview: true
  }
})

with the customized component name

Vue.use(VuejsClipper, {
  components: {
    clipperBasic: 'image-clipper-basic',
    clipperPreview: 'my-preview'
  }
})

not register any components, but with some plugin options

Vue.use(VuejsClipper, {
  components: null,
  parentPropName: 'myCustomerName'
  /*
   parentPropName:
    Vuejs-clipper Adds property to Vue instance in order to store `clipper-preview` list.
    You can change the property name
    default: '_imgPreviewLists'
  */
})

separately import components (need css loader)

import Vue from 'vue'
import VueRx from 'vue-rx'
// install vue-rx
Vue.use(VueRx)

then import in your components (SFC)

import { clipperBasic, clipperPreview } from 'vuejs-clipper'

export default {
  components: {
    clipperBasic,
    clipperPreview
  }
}

Installation (Script)

Include vuejs-clipper umd script after Vue.js.

<!-- rxjs-->
<script src="https://unpkg.com/rxjs/bundles/rxjs.umd.js"></script>
<!--vue-->
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.js"></script>
<!-- vue-rx-->
<script src="https://unpkg.com/vue-rx@6.2.0/dist/vue-rx.js"></script>
<!--vuejs-clipper script & style -->
<script src="./dist/vuejs-clipper.umd.min.js"></script>
<link rel="stylesheet" href="./dist/vuejs-clipper.css" />

Use in html/template

<clipper-basic src="example.jpg"></clipper-basic>

Components

404

See detail examples.

clipper-basic

an image clipping component

import { clipperBasic } from 'vuejs-clipper'
PropTypedefaultdescription
srcstringimage src
previewstringmatches clipper-preview's name to show preview image.
bordernumber1border width
outlinenumber6outlines near by the border to help user zooming.
cornerbooleantrueshow corner layout
gridbooleantrueshow grid layout
rationumberratio of clipping area (width/height). ex: 1, 4/3 .
wrap-rationumberNaNratio of clipping container (width/height). ex: 1, 4/3 .
mode'normal'/'switch''normal'if ratio is set, this prop will affect how clipping area zoom.
bg-colorstring'white'background color
lineColorstring'#1baae8'clip box line color
shadowstring'rgba(0,0,0,0.4)'shadow color
rotatenumber0rotate degree
scalenumber1transform scale
min-widthnumber1minimum width(%) of clipping box related to clipping component's width
min-heightnumber1minimum height(%) of clipping box related to clipping component's height.
init-widthnumber50clipping area's width(%) when the image loaded.
init-heightnumber50clipping area's height(%) when the image loaded.
touch-createbooleantrueenable/disable create new clipping area on touch device
cross-originstringundefinedcrossorigin attribute of <img /> inside clipper. ex: anonymous

For more detail about the layout settings, pleases see Component layout in depth.

methodargumentreturndescription
clipoptionscanvas elementget clipping canvas element
getDrawPos{pos, translate}: positions and transformationget result canvas information

clip() arguments

Resulting-canvas-size

nametypedefaultdescription
options.wPixelnumberundefinedSet the the width (pixel) of result canvas.
options.maxWPixelnumberundefinedSet the the maximum width (pixel) of result canvas.

set ref to use component methods

<clipper-basic ref="clipper" />

in your Vue instance methods

const canvas = this.$refs.clipper.clip()
eventparametersdescription
load$eventimage onload
error$errorimage onerror

usage :

<clipper-basic @error="errorCb" @load="loadCb" />
datatypedefaultdescription
imgRationumberNaNupload image's ratio (image naturalWidth/natrualHeight). Default value is NaN, after the load event the value will be set.
zoomTL$objectclipping area's position(%), can be top/bottom and left/right.
zoomWH$objectclipping area's width and height(%)

usage :

this.$refs.clipper.imgRatio
this.$refs.clipper.zoomWH$.width
slotdescription
placeholderif no src provided, show placeholder
<clipper-basic src="">
  <div slot="placeholder">No image</div>
</clipper-basic>
subjectdescription
setTL$Set the position of the zooming area.
setWH$Set the width and height of the zooming area.
onChange$Subject that subscribes to zooming, moving, and rotating subjects.

usage:

this.$refs.clipper.setTL$.next({ left: 0, top: 0 }) // percentage 0%
this.$refs.clipper.setTL$.next({ right: 0, bottom: 10 })
this.$refs.clipper.setWH$.next({ width: 100, height: 100 }) // percentage 100%

this.$refs.clipper.onChange$.subscribe(() => {
  // This happens whenever zooming, moving, and rotating occur.
})

clipper-fixed

an image clipping component

import { clipperFixed } from 'vuejs-clipper'
PropTypedefaultdescription
srcstringimage src
previewstringmatches clipper-preview's name to show preview image.
rationumber1ratio of clipping area (width/height). ex: 1, 4/3 .
zoom-ratenumber0.04zooming faster if this value is larger
min-scalenumber0.1minimum transform scale
bordernumber1border width
border-colorstring'white'border color
gridbooleantrueshow grid layout
roundbooleanfalseUse a round clipping area, this only effect the component layout, also affect clipping results.
bg-colorstring'white'background color
shadowstring'rgba(0,0,0,0.4)'shadow color
rotatenumber0rotate degree
areanumber50width or height (%) of clipping box(depends on ratio).
cross-originstringundefinedcrossorigin attribute of <img /> inside clipper. ex: anonymous
handle-zoom-eventfunction(scale) => scalehandle zooming, accept the calculated scale value, return the scale value.
methodargumentreturndescription
clipoptionscanvas elementget clipping canvas element.
getDrawPos{pos, translate}: positions and transformationget result canvas information

clip() arguments

nametypedefaultdescription
options.wPixelnumberundefinedSet the the width (pixel) of result canvas.
options.maxWPixelnumberundefinedSet the the maximum width (pixel) of result canvas.
eventparametersdescription
load$eventimage onload
error$errorimage onerror
datatypedefaultdescription
imgRationumberNaNupload image's ratio (image naturalWidth/natrualHeight). Default value is NaN, after the load event the value will be set.
bgTL$objectimage's translate(X,Y)
bgWH$numberimage's scaling
slotdescription
placeholderif no src provided, show placeholder
subjectdescription
setTL$Set the top and left of the image.
setWH$Set the sizing(scaling) of the image.
onChange$Subject that subscribes to zooming, moving, and rotating subjects.

usage:

this.$refs.clipper.setTL$.next({ left: 50, top: 50 }) // percentage 0%
this.$refs.clipper.setWH$.next(0.6) // transform scale(0.6)

this.$refs.clipper.onChange$.subscribe(() => {
  // This happens whenever zooming, moving, and rotating occur.
})

clipper-preview

preview clipping result

import { clipperPreview } from 'vuejs-clipper'
PropTypedefaultdescription
namestringname that matches clipper component's preview property
slotdescription
placeholderif no src provided, show placeholder

clipper-range

a simple input range component

import { clipperRange } from 'vuejs-clipper'

use v-model binding data with clipper-range

PropTypedefaultdescription
maxnumber10maximum value of range
minnumber0minimum value of range

clipper-upload

a new component in 0.2.0

an upload button that transforms image files to URL

import { clipperUpload } from 'vuejs-clipper'

use v-model binding data with clipper-upload

Props that are not defined below will apply to the file input as attributes, for example: accept, id, and name.

PropTypedefaultdescription
checkbooleantrueCheck if upload file is an image. If set to true, when upload files that are not images, it will do nothing, so you will not get an error event on the clipping component.
exifbooleantrueTransform EXIF image to correct orientation when uploading.
eventparametersdescription
input$eventResult domgstring on change
datatypedefaultdescription
fileFile ObjectnullUploaded file's original File Object.

Changelog


!Before v3.1.0, clipper-basic with ratio prop has a bug that the clip area is not calculated correctly with border prop, so the ratio of the clipping result isn't precise. (issue #80)