1.1.9 • Published 4 years ago

vuejs-clipperk v1.1.9

Weekly downloads
9
License
MIT
Repository
github
Last release
4 years ago

vuejs-clipper

Vue.js image clipping components using Vue-Rx.

not found not found

  • Add image clipping components to your Vue application in nothing flat.
  • Touch devices supported and fully responsive.

Demo/Document

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

Table of Contents

Notice

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

  • It's based on vue-rx.
  • Use vuejs-clipper plugin also add vue-rx plugin to vue.
  • Components are responsive base on width instead of height, see Component Layout.
  • You can clip your own images (local uploaded images or images served on your site), but you cannot clip a cross-origin image unless the image server set the CORS headers.
  • Components' input is an image URL, output is a canvas element, they only help you clip images to canvas, you need to handle other things like transform file input to image URL or transform output canvas to image by yourself.

Installation

NPM & ESM

install vuejs-clipper

$npm install vuejs-clipper --save

need css loader, ex: sass-loader, if you haven't installed :

$npm install -D sass-loader node-sass

(1) use vuejs-clipper plugin

Use vuejs-clipper plugin also add vue-rx plugin to Vue.

By default it will register all components to Vue global scope.

import Vue from 'vue'
import VuejsClipper from 'vuejs-clipper'
// install
Vue.use(VuejsClipper)

register some components to global with default component name

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

with 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:
    Add property to Vue instance to store `clipper-preview` list.
    You can change the property name
    default: '_imgPreviewLists'
  */
})

(2) separately import components

install vue-rx and it's peer dependency rxjs

$npm install --save vue-rx rx-js

use vue-rx

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
  }
}

Script

Include vuejs-clipper umd script after Vue.js.

<script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.js"></script>
<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'
  • Props
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 .
wrapRationumberNaNratio 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
shadowstring'rgba(0,0,0,0.4)'shadow color
rotatenumber0rotate degree
scalenumber1transform scale
minWidthnumber1minimum width(%) of clipping box related to clipping component's width
minHeightnumber1minimum height(%) of clipping box related to clipping component's height.
initWidthnumber50clipping area's width(%) when the image loaded.
initHeightnumber50clipping area's height(%) when the image loaded.
touch-createbooleantrueenable/disable create new clipping area on touch device
crossOriginstringundefinedcrossorigin attribute of <img /> inside clipper. ex: anonymous

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

  • Methods
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()
  • Event
eventparametersdescription
load$eventimage onload
error$errorimage onerror

usage :

<clipper-basic @error="errorCb" @load="loadCb" />
  • Data
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
  • Slot
slotdescription
placeholderif no src provided, show placeholder
<clipper-basic src="">
  <div slot="placeholder">No image</div>
</clipper-basic>
  • rxjs Subject
subjectdescription
setTL$Set the position of the zooming area.
setWH$Set the width and height of the zooming area.
onChange$Subject that subscribe 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'
  • Props
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, clipping results are still rectangular.
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).
crossOriginstringundefinedcrossorigin attribute of <img /> inside clipper. ex: anonymous
  • Method
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.
  • Event
eventparametersdescription
load$eventimage onload
error$errorimage onerror
  • Data
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
  • Slot
slotdescription
placeholderif no src provided, show placeholder
  • rxjs Subject
subjectdescription
setTL$Set the top and left of the image.
setWH$Set the sizing(scaling) of the image.
onChange$Subject that subscribe 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'
  • Props
PropTypedefaultdescription
namestringname that matches clipper component's preview property
  • Slot
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

  • Props
PropTypedefaultdescription
maxnumber10maximum value of range
minnumber0minimum value of range

clipper-upload

a new component in 0.2.0

an upload button that transform image files to URL

import { clipperUpload } from 'vuejs-clipper'

use v-model binding data with clipper-upload

  • Props
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 clipping component.
acceptstring'*'Bind accept attribute to file input tag.
exifbooleantrueTransform EXIF image to correct orientation when uploading.
  • Event
eventparametersdescription
input$eventResult domgstring on change
  • Data
datatypedefaultdescription
fileFile ObjectnullUploaded file's original File Object.

Changelog

  • 1.1.6
    • Update dependencies.
  • 1.1.5
    • Add new prop area for clipper-fixed.
  • 1.1.4
    • Add crossorigin attribute biding for <img/> in clipper (crossOrigin prop).
  • 1.1.3
    • Add !important statements to components' style.
  • 1.1.2
    • Set pixel of clip result canvas. clip({ wPixel, maxWPixel })
  • 1.1.1
    • Add clipper-fixed placeholder slot.
  • 1.1.0
    • Fixed clipper-basic props ratio and wrapRatio behaviors, now ratio will not affect clipper's layout since there's wrapRatio to control the layout.
  • 1.0.1
    • Fixed clipper-fixed loading images overflow.
    • Add wrapRatio, initWidth and initHeight props to clipper-basic
  • 1.0.0
    • Change the clipper-basic design, it will judge layout depends on the ratio.
    • Production version.
  • 0.2.13
    • Decrease css specificity
  • 0.2.12
  • 0.2.11
  • 0.2.10
    • Use passive event listener on wheel event (issue #8).
  • 0.2.9
    • New prop border-color for clipper-fixed
    • Fixed issue #4
  • 0.2.8
    • New prop accept for clipper-upload (issue #1)
    • Add EXIF image transformation feature to clipper-upload (issue #2)