图片不旋转的时候可以直接拖动,图片如果旋转过了,坐标系就会变掉,再次拖动的话,怎么判断坐标系是在原点还是已经变过了的呢?
cvsMove: function (e) {
if (e.touches.length > 1) {//二指缩放
console.log("双指缩放")
var xMove2 = Math.abs(e.touches[1].x - e.touches[0].x);
var yMove2 = Math.abs(e.touches[1].y - e.touches[0].y);
var distance2 = Math.sqrt(xMove2 * xMove2 + yMove2 * yMove2);//开根号
console.log(distance2);
// this.data.distanceList.shift()
// this.data.distanceList.push(distance)
// if (this.data.distanceList[0] == 0) { return }
// var distanceDiff = this.data.distanceList[1] - this.data.distanceList[0]//两次touch之间, distance的变化. >0,放大图片.<0 缩小图片
// var baseScale = 1 + 0.005 * distanceDiff
var baseScale = this.data.distance1 / distance2 ;
console.log("缩放率"+baseScale);
const ctx = wx.createCanvasContext('myCanvas');
ctx.scale(baseScale, baseScale);
ctx.drawImage(this.data.tempFilePaths, this.data.lastX, this.data.lastY, 200 , 300);
ctx.restore();
} else if (e.touches.length == 1){
var touchs = e.touches[0];
console.log('canvas被拖动了....')
var x = touchs.x;
var y = touchs.y;
var moveX = x - this.data.currentX;
var moveY = y - this.data.currentY;
this.setData({
newX : this.data.lastX + moveX,
newY : this.data.lastY + moveY
})
const ctx = wx.createCanvasContext('myCanvas');
ctx.save();
ctx.setFillStyle('white');
ctx.drawImage(this.data.tempFilePaths, this.data.newX, this.data.newY, 200, 300)
ctx.draw();
ctx.restore();
}
},
针对坐标系的操作不一直都要保存状态吗,在操作canvas的时候一直都是save,restore的,你旋转完之后还要restore,也就是说操作完之后坐标系会恢复到最初状态 不会有你说的这个问题