Component({
properties: {
imgSrc: {
type: String
},
height: {
type: Number,
value: 200
},
width: {
type: Number,
value: 200
},
min_width: {
type: Number,
value: 100
},
min_height: {
type: Number,
value: 100
},
max_width: {
type: Number,
value: 300
},
max_height: {
type: Number,
value: 300
},
disable_width: {
type: Boolean,
value: !1
},
disable_height: {
type: Boolean,
value: !1
},
disable_ratio: {
type: Boolean,
value: !1
},
export_scale: {
type: Number,
value: 3
},
quality: {
type: Number,
value: 1
},
cut_top: {
type: Number,
value: null
},
cut_left: {
type: Number,
value: null
},
canvas_top: {
type: Number,
value: null
},
canvas_left: {
type: Number,
value: null
},
img_width: {
type: null,
value: null
},
img_height: {
type: null,
value: null
},
scale: {
type: Number,
value: 1
},
angle: {
type: Number,
value: 0
},
min_scale: {
type: Number,
value: .5
},
max_scale: {
type: Number,
value: 2
},
disable_rotate: {
type: Boolean,
value: !1
},
limit_move: {
type: Boolean,
value: !1
}
},
data: {
el: "image-cropper",
info: wx.getSystemInfoSync(),
MOVE_THROTTLE: null,
MOVE_THROTTLE_FLAG: !0,
INIT_IMGWIDTH: 0,
INIT_IMGHEIGHT: 0,
TIME_BG: null,
TIME_CUT_CENTER: null,
_touch_img_relative: [ {
x: 0,
y: 0
} ],
_flag_cut_touch: !1,
_hypotenuse_length: 0,
_flag_img_endtouch: !1,
_flag_bright: !0,
_canvas_overflow: !0,
_canvas_width: 200,
_canvas_height: 200,
origin_x: .5,
origin_y: .5,
_cut_animation: !1,
_img_top: wx.getSystemInfoSync().windowHeight / 2,
_img_left: wx.getSystemInfoSync().windowWidth / 2,
watch: {
width: function(t, a) {
t < a.data.min_width && a.setData({
width: a.data.min_width
}), a._computeCutSize();
},
height: function(t, a) {
t < a.data.min_height && a.setData({
height: a.data.min_height
}), a._computeCutSize();
},
angle: function(t, a) {
a._moveStop(), a.data.limit_move && a.data.angle % 90 && a.setData({
angle: 90 * Math.round(a.data.angle / 90)
});
},
_cut_animation: function(t, a) {
clearTimeout(a.data._cut_animation_time), t && (a.data._cut_animation_time = setTimeout(function() {
a.setData({
_cut_animation: !1
});
}, 300));
},
limit_move: function(t, a) {
t && (a.data.angle % 90 && a.setData({
angle: 90 * Math.round(a.data.angle / 90)
}), a._imgMarginDetectionScale(), !a.data._canvas_overflow && a._draw());
},
canvas_top: function(t, a) {
a._canvasDetectionPosition();
},
canvas_left: function(t, a) {
a._canvasDetectionPosition();
},
imgSrc: function(t, a) {
a.pushImg();
},
cut_top: function(t, a) {
a._cutDetectionPosition(), a.data.limit_move && !a.data._canvas_overflow && a._draw();
},
cut_left: function(t, a) {
a._cutDetectionPosition(), a.data.limit_move && !a.data._canvas_overflow && a._draw();
}
}
},
attached: function() {
this.data.info = wx.getSystemInfoSync(), this._watcher(), this.data.INIT_IMGWIDTH = this.data.img_width,
this.data.INIT_IMGHEIGHT = this.data.img_height, this.setData({
_canvas_height: this.data.height,
_canvas_width: this.data.width
}), this._initCanvas(), this.data.imgSrc && (this.data.imgSrc = this.data.imgSrc),
this._initImageSize(), this._computeCutSize(), this._cutDetectionPosition(), this._canvasDetectionPosition(),
this.triggerEvent("load", {
cropper: this
});
},
methods: {
upload: function() {
var t = this;
wx.chooseImage({
count: 1,
sizeType: [ "original", "compressed" ],
sourceType: [ "album", "camera" ],
success: function(a) {
var i = a.tempFilePaths[0];
t.pushImg(i), wx.showLoading({
title: "加载中..."
});
}
});
},
getImg: function(t) {
var a = this;
this._draw(function() {
wx.canvasToTempFilePath({
width: a.data.width * a.data.export_scale,
height: Math.round(a.data.height * a.data.export_scale),
destWidth: a.data.width * a.data.export_scale,
destHeight: Math.round(a.data.height) * a.data.export_scale,
fileType: "png",
quality: a.data.quality,
canvasId: a.data.el,
success: function(i) {
t({
url: i.tempFilePath,
width: a.data.width * a.data.export_scale,
height: a.data.height * a.data.export_scale
});
}
}, a);
});
},
setTransform: function(t) {
if (t) {
this.data.disable_rotate || this.setData({
angle: t.angle ? this.data.angle + t.angle : this.data.angle
});
var a = this.data.scale;
t.scale && (a = (a = (a = this.data.scale + t.scale) <= this.data.min_scale ? this.data.min_scale : a) >= this.data.max_scale ? this.data.max_scale : a),
this.data.scale = a;
var i = this.data.cut_left, h = this.data.cut_top;
t.cutX && (this.setData({
cut_left: i + t.cutX
}), this.data.watch.cut_left(null, this)), t.cutY && (this.setData({
cut_top: h + t.cutY
}), this.data.watch.cut_top(null, this)), this.data._img_top = t.y ? this.data._img_top + t.y : this.data._img_top,
this.data._img_left = t.x ? this.data._img_left + t.x : this.data._img_left, this._imgMarginDetectionScale(),
this._moveDuring(), this.setData({
scale: this.data.scale,
_img_top: this.data._img_top,
_img_left: this.data._img_left
}), !this.data._canvas_overflow && this._draw(), this._moveStop();
}
},
setCutXY: function(t, a) {
this.setData({
cut_top: a,
cut_left: t
});
},
setCutSize: function(t, a) {
this.setData({
width: t,