javascript - 如何旋转画布以使先前绘制的形状也随旋转的上下文一起移动
问题描述
我有这个方法试图在 mouseMove 事件上旋转画布,虽然我能够旋转它,但之前在上下文中绘制了一些形状
我的最终目标:我需要这个画布来旋转,并且要旋转的图纸、形状以及我想要制作的任何后续注释都应该在旋转的形状上,但是当我点击刷新和旋转未完成时,这些形状应该正好在哪里它们是在旋转的上下文中绘制的
rotateCanvas = (context, canvas, newCtx, newCanvas) => {
//canvas and newCanvas are drawn on the same canvas element
newCanvas.width = canvas.width;
newCanvas.height = canvas.height;
newCtx.drawImage(canvas, 0, 0, canvas.width, canvas.height);
context.clearRect(0, 0, canvas.width + 2, canvas.height + 2);
context.fillStyle = 'black';
context.fillRect(0, 0, canvas.width, canvas.height);
context.translate(canvas.width / 2, canvas.height / 2);
context.rotate(this.diffAngleRotated);
context.translate(-canvas.width / 2, -canvas.height / 2);
context.drawImage(newCanvas, 0, 0, canvas.width, canvas.height);
context.setTransform(1, 0, 0, 1, 0, 0);
};
旋转和未旋转的图像
解决方案
推荐阅读
- xcode - 如何在 SwiftUI 中使用带有结构的 if 语句?
- javascript - 使用 styled-component 将数组渲染到行
- asp.net - ASP.NET_SessionId cookieSameSite 问题
- java - 如何在其成员函数kotlin中调用构造函数?
- cassandra - 如何建模账户逻辑?
- python - Python:请求 POST 方法未将文件上传到 Web 服务器
- r - 跨不同行和列的多个矩阵元素
- laravel - Laravel 7.2 Illuminate\Foundation\Http\FormRequest 中的奇怪行为
- javascript - puppeteer web 抓取条件 if 语句
- android - 即使在使用导航 2.2.1 后仍然有错误 no current navigation node