canvas画图,使 ctx.rotate()旋转图像,为何图像还会偏移?
发布于 6 年前 作者 lei34 14863 次浏览 来自 问答

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 回复

中心点没设置吧, translate

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

回到顶部