mpvue-cropper.vue
2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<template>
<div>
<canvas
v-if="_canvasId"
:canvasId="_canvasId"
@touchstart="touchstart"
@touchmove="touchmove"
@touchend="touchend"
disable-scroll
:style="{ width: _width + 'px', height: _height + 'px', background: 'rgba(0, 0, 0, .8)' }">
</canvas>
<canvas
v-if="_targetId"
:canvas-id="_targetId"
disable-scroll
:style="{
position: 'fixed',
top: -_width * _pixelRatio + 'px',
left: -_height * _pixelRatio + 'px',
width: _width * _pixelRatio + 'px',
height: _height * _pixelRatio + 'px'
}">
</canvas>
</div>
</template>
<script>
import WeCropper from 'we-cropper'
export default {
name: 'mpvue-cropper',
props: {
option: {
type: Object
}
},
data () {
return {
_wecropper: null
}
},
computed: {
_canvasId () {
return this.option.id
},
_targetId () {
return this.option.targetId
},
_width () {
return this.option.width
},
_height () {
return this.option.height
},
_pixelRatio () {
return this.option.pixelRatio
}
},
methods: {
touchstart ($event) {
this._wecropper.touchStart($event.mp)
},
touchmove ($event) {
this._wecropper.touchMove($event.mp)
},
touchend ($event) {
this._wecropper.touchEnd($event.mp)
},
pushOrigin (src) {
this._wecropper.pushOrign(src)
},
updateCanvas () {
this._wecropper.updateCanvas()
},
getCropperBase64 (fn) {
return this._wecropper.getCropperBase64(fn)
},
getCropperImage (opt, fn) {
return this._wecropper.getCropperImage(opt, fn)
},
init () {
this._wecropper = new WeCropper(Object.assign(this.option, {
id: this._canvasId,
targetId: this._targetId,
pixelRatio: this._pixelRatio
}))
.on('ready', (...args) => {
this.$emit('ready', ...args)
})
.on('beforeImageLoad', (...args) => {
this.$emit('beforeImageLoad', ...args)
})
.on('imageLoad', (...args) => {
this.$emit('imageLoad', ...args)
})
.on('beforeDraw', (...args) => {
this.$emit('beforeDraw', ...args)
})
}
},
onLoad () {
if (!this.option) {
return console.warn('[mpvue-cropper] 请传入option参数\n参数配置见文档:https://we-plugin.github.io/we-cropper/#/api')
}
this.init()
}
}
</script>