1.1.18 • Published 3 years ago

xcrop v1.1.18

Weekly downloads
10
License
MIT
Repository
github
Last release
3 years ago

移动端裁剪插件

npm GitHub license

移动端裁剪插件,原生 JavaScript 实现,无依赖,支持 Vue 2.0,React。

Demo

Demo

安装

Install with npm: npm install xcrop --save

直接使用

<input type="file" id="file-input" accept="image/*">
import Crop from 'xcrop'

const options = {}
const crop = new Crop(options)

crop.on('cancel', crop => {
  crop.hide()
})
crop.on('confirm', crop => {
  const canvas = crop.get({ format: 'canvas' })
  document.body.appendChild(canvas)
  crop.hide()
})

function onChange (e) {
  var file = e.target.files[0]
  if (!file) return false
  crop.load(file)
  this.value = ''
}

document.getElementById('file-input').onchange = onChange

基于 Vue 2.0 使用

<Crop
  :file="file"
  :options="options"
  @on-cancel="onCancel"
  @on-confirm="onConfirm"
  @on-error="onError"
/>
<input type="file" @change="onChange($event)" accept="image/*" :value="''">
<img v-if="output" :src="output" width="100%">
import Crop from 'xcrop/src/components/VueCrop'

export default {
  data () {
    return {
      file: null,
      options: {},
      output: ''
    }
  },
  methods: {
    onChange (e) {
      this.file = e.target.files[0]
    },
    onCancel (crop) {
      this.file = null
      crop.hide()
    },
    onConfirm (crop) {
      this.output = crop.get()
      this.file = null
      crop.hide()
    },
    onError (error) {
      console.log(error)
    }
  },
  components: {
    Crop
  }
}

基于 React 使用

import React, { Component } from 'react'
import Crop from 'xcrop/src/components/ReactCrop'

export default class App extends Component {

  constructor (props) {
    super(props)

    this.state = {
      options: {},
      output: ''
    }

    this.onChange = this.onChange.bind(this)
    this.onCancel = this.onCancel.bind(this)
    this.onConfirm = this.onConfirm.bind(this)
    this.onError = this.onError.bind(this)
  }

  onChange (e) {
    this.crop[0].load(e.target.files[0])
  }

  onCancel (crop) {
    crop.hide()
  }

  onConfirm (crop) {
    this.setState({
      output: crop.get()
    })
    crop.hide()
  }

  onError (error) {
    console.log(error)
  }

  render () {
    return (
      <div className="App">
        <input type="file" onChange={this.onChange} accept="image/*" value="" />

        {this.state.output && <img src={this.state.output} width="100%" alt="" />}

        <Crop
          ref={component => this.crop = component || null}
          options={this.state.options}
          onCancel={this.onCancel}
          onConfirm={this.onConfirm}
          onError={this.onError}
        />
      </div>
    )
  }
}

Options

Type: Object

实例化选项:new Crop(options)

参数必选类型默认说明
elfalseElementbody插入节点
viewWidthfalseNumberdocument.documentElement.clientWidth容器宽度
viewHeightfalseNumberdocument.documentElement.clientHeight容器高度
borderfalseObject{x,y,width,height}裁剪框位置大小,默认居中,为容器90%大小
circlefalseBooleanfalse裁剪框是否为圆形,仅样式改变,裁剪后输出的图片依然是矩形,不支持安卓<=4.1的版本
maxScalefalseNumber2允许缩放的最大比例
confirmTextfalseString确认确认按钮文字
cancelTextfalseString取消取消按钮文字
beforeShowClassfalseStringcrop_slide-to-left显示的动画类名,会在显示之前添加,之后移除,可选:crop-slide-to, : left/right/top/bottom
beforeHideClassfalseStringcrop_slide-to-bottom隐藏的动画类名,会在隐藏之前添加,参数同上

实例方法

load(target)

加载图片

参数必选类型默认说明
targettrueString/File/Element-图片目标,可以是flile/base64/imageElement/objectUrl/canvas

get(options)

获取裁剪图片

options 属性必选类型默认说明
widthfalseNumber默认宽度基于原图比例裁剪宽度
heightfalseNumber默认高度基于原图比例裁剪高度
typefalseStringimage/jpeg图片格式
formatfalseStringbase64输出格式,可选:canvas/objectUrl/base64/file
qualityfalseNumber0.85图片质量,对应canvas toDataURL方法
返回值类型说明
resultString/Element/File返回结果,根据输入选项返回对应结果

show / hide

显示/隐藏组件

参数必选类型默认说明
transitionfalseBooleantrue是否需要过渡动画

setBorder

设置裁剪框位置大小:setBorder(border)

参数必选类型默认说明
bordertrueObject-裁剪框大小:{x, y, width, height}

on

监听自定义事件,函数返回自身,可以链式调用,

参数必选类型默认说明
eventNametrueString-事件名
fntrueFunction-事件函数
oncefalseBooleanundefined此事件是否只执行一次

可监听的事件有:

事件名说明
loaded图片加载完成
error图片加载失败
cancel按钮取消
confirm按钮确认
dragstart单指按下
dragmove单指拖动
dragend单指拖动结束
pinchstart双指按下
pinchmove双指拖动
pinchend双指拖动结束

emit

分发自定义事件

参数必选类型默认说明
eventNametrueString-事件名
argumentsfalse*-携带的参数

off

移除自定义事件

参数必选类型默认说明
eventNametrueString-事件名
fntrueFunction-移除的函数

this.canvas.moveTo

移动图片到指定位置

参数必选类型默认说明
xtrueNumber-x坐标
ytrueNumber-y坐标

this.canvas.sacleTo

缩放图片

参数必选类型默认说明
pointtrueObject-以点为中心缩放:{x:0, y:0}
scaletrueNumber-缩放比例

destroy

销毁组件

静态方法

Crop.imageToCanvas

图片转canvas

参数必选类型默认说明
targettruestring/file/element-图片
callbacktrueFunction-成功回调,回调函数中第一个参数为canvas
optionsfalseObject{orientation: true, errorCallback: function () {}}可选项,orientation:是否需要更正图片方向,errorCallback: 出错回调

Browser support

Android 4.2+, iOS 8+

1.1.18

3 years ago

1.1.17

3 years ago

1.1.16

4 years ago

1.1.15

4 years ago

1.1.14

5 years ago

1.1.13

5 years ago

1.1.12

5 years ago

1.1.11

5 years ago

1.1.9

5 years ago

1.1.8

5 years ago

1.1.7

5 years ago

1.1.6

5 years ago

1.1.5

5 years ago

1.1.4

6 years ago

1.1.3

6 years ago

1.1.2

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago

0.0.11

7 years ago

0.0.10

7 years ago

0.0.9

7 years ago

0.0.8

7 years ago

0.0.7

7 years ago

0.0.6

7 years ago

0.0.5

7 years ago

0.0.3

7 years ago

0.0.2

7 years ago