canvas图片旋转之后在拖动,坐标系应该怎么转换?

图片不旋转的时候可以直接拖动,图片如果旋转过了,坐标系就会变掉,再次拖动的话,怎么判断坐标系是在原点还是已经变过了的呢?
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();
}

},

阅读 4.2k
2 个回答

针对坐标系的操作不一直都要保存状态吗,在操作canvas的时候一直都是save,restore的,你旋转完之后还要restore,也就是说操作完之后坐标系会恢复到最初状态 不会有你说的这个问题

楼主 我也遇到这个问题,请问下你是怎么解决的啊,我都快搞晕了

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题