canvas画图,使 ctx.rotate()旋转图像,为何图像还会偏移?

发布于 8 年前作者 lei3415037 次浏览最后编辑 8 年前来自 ask

canvas绘图,调用wx.createCanvasContext();

// 旋转前

ctx.drawImage(a.imgurl1, x, y, a.tempImgWidth, a.tempImgheight);

ctx.setStrokeStyle('#fd749c'

ctx.setLineDash([5,5], 10);

ctx.strokeRect(x, y, a.tempImgWidth, a.tempImgheight)

//旋转后

ctx.rotate(0.2880659);//大概16.5°

ctx.drawImage(a.imgurl1, x, y, a.tempImgWidth, a.tempImgheight);

ctx.setStrokeStyle('#fd749c');

ctx.setLineDash([5,5], 10);

ctx.strokeRect(x, y, a.tempImgWidth, a.tempImgheight)

结果如下:

官方文档的示例确实也偏移了,有什么办法不让整个图片偏移,仅以左上角旋转即可,基础库版本为2.10的。

2 回复
rsong
rsong1 楼6 年前

中心点没设置吧, translate

xiuying36
xiuying362 楼6 年前

你要看偏移的中心点是哪个点