画布旋转 - JavaScript
如果有人有答案,可以帮我吗? 我有 2 张图像 - 背景类似于弧线和箭头。我需要将箭头图像移动到背景图像上,就像这是一个时钟收报机箭头。所以我需要:
- 旋转箭头,使其与弧上的当前刻度平行
- 将箭头移动到下一个点
为此,我需要在 JavaScript 中使用 Canvas 对象及其方法转换 - 这将允许移动箭头并旋转它。
问题是:如何使用 canvas.transform 方法围绕圆弧旋转(和理想的移动)箭头?在这种情况下,哪些值以及它们之间的关系应该是什么:
contextData.clearRect (0, 0, contextData.canvas.width, contextData.canvas.height);
contextData.save ();
contextData.translate(indicatorData.width () / 2, indicatorData.height () / 2);
contextData.transform(1, 0, 0, 1, x, y); // the question is HERE
contextData.drawImage(rotationArrow, -rotationArrow.width / 2, -rotationArrow.height / 2);
contextData.restore ();
提前致谢。
If someone has the answer could you help me, please?
I have 2 images - background similar to arc and arrow. And i need to move arrow image over background image like this is a clock ticker arrow. So i need :
- Rotate arrow to make it parallel to current tick on the arc
- Move arrow to the next point
For this i need to use Canvas object in JavaScript and its method transform - this will allow to move arrow and rotate it.
The question is : how to use canvas.transform method to rotate (and desirable move) arrow around the arc? And which values and what relation between them should be in this case :
contextData.clearRect (0, 0, contextData.canvas.width, contextData.canvas.height);
contextData.save ();
contextData.translate(indicatorData.width () / 2, indicatorData.height () / 2);
contextData.transform(1, 0, 0, 1, x, y); // the question is HERE
contextData.drawImage(rotationArrow, -rotationArrow.width / 2, -rotationArrow.height / 2);
contextData.restore ();
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要使用变换矩阵,而是使用辅助函数 .rotate
这样做可以让您围绕其中心旋转对象
Instead of fooling around with a transformation matrix, use the helper function .rotate
Doing this will let you rotate an object about its center
差点忘了......我找到了答案,它有效,这是最终的代码。要使其在 IE 中工作,您应该添加来自 Google 的 excanvas 库,并修复一些 IE8 问题,您需要添加兼容性元标记:
Almost forgot ... i found the answer, it works and this is the final code. To make it worked in IE you should add excanvas library from Google and to fix some IE8 issues you need to add compatibility meta tag :