很感谢鹅神做出这样一款小程序裁剪组件,方便好用功能强大。我在使用的时候发现如果在setData里设置图片的宽高,也就是img_width与img_height会导致图片加载的时候出现位移的情况,触摸一下裁剪框就会回到中间,我发现watch方法里没有img_width(value, that)与img_height(value, that),不知是不是和这个有关。我尝试加了一下,不知有没有问题:
img_width(value, that) {
that.data.INIT_IMGWIDTH = value
that.data.INIT_IMGHEIGHT = that.data.img_height
that._computeCutSize()
},
img_height(value, that) {
that.data.INIT_IMGWIDTH = that.data.img_width
that.data.INIT_IMGHEIGHT = value
that._computeCutSize()
},
第二个bug是getImg(res)方法获取图片有时候图片会出现拉伸,导致裁剪过多,并不是必现,有时候又正常,不知是不是和我设置img_width与img_height有关。
一个建议:你在输出图片的时候通过
destWidth: this.data.width * this.data.export_scale,
destHeight: Math.round(this.data.height) * this.data.export_scale,
来放大图片,但是这样图片会失真,因为本身的canvas绘制的图片尺寸比较小,这样把小图拉大就会变模糊。于是我尝试在你的canvas下面加一个大尺寸的canvas,跟随你的canvas绘制,在 _draw(callback)方法里最后面加上:
if (callback) {
let img_width2 = this.data.img_width * this.data.scale * this.data.export_scale
let img_height2 = this.data.img_height * this.data.scale * this.data.export_scale
var xpos2 = (this.data._img_left - this.data.cut_left) * this.data.export_scale
var ypos2 = (this.data._img_top - this.data.cut_top) * this.data.export_scale
this.data.ctxBig.translate(xpos2, ypos2)
this.data.ctxBig.rotate((this.data.angle * Math.PI) / 180)
this.data.ctxBig.drawImage(
this.data.imgSrc,
-img_width2 / 2,
-img_height2 / 2,
img_width2,
img_height2
)
this.data.ctxBig.draw(false, () => {
callback()
})
}
wx.canvasToTempFilePath(
canvasId: 'image-cropper-big',
fileType: 'png',
quality: this.data.quality,
success: res => {
let src = res.tempFilePath
wx.getImageInfo({
src: src,
success: res => {
let iw = res.width
let ih = res.height
getCallback({
url: src,
width: ih,
height: iw
})
}
})
}
)