【发布时间】:2014-12-02 09:50:16
【问题描述】:
我已经完成了让用户单击一个按钮(位于画布右侧)的代码,然后将图像添加到画布并被限制为只能绕圆周移动。为了移动图像,用户只需单击图像然后移动鼠标。要释放图像,用户只需单击图像在画布上的位置即可。
这是一个显示当前代码功能的小提琴。 http://jsfiddle.net/smacnabb/68awv7sq/9/
问题:我希望能够让围绕圆周移动的图像在围绕圆周移动的同时旋转。
这就是我的意思:
这是我添加的代码以尝试实现这一点 http://jsfiddle.net/smacnabb/68awv7sq/11/
在handlemousemove 方法中,每次鼠标移动我将mouseX、mouseY 传递给state.draw 时,它都会调用state.draw()。
state.draw() 在 addstate 方法中,这是我为使图像旋转而添加的代码
var dx = mouseX - centerX;
var dy = mouseY - centerY;
var radianAngle = Math.atan2(dy, dx);
context.save();
context.translate(centerX, centerY);
context.rotate(radianAngle);
if (this.dragging) {
context.strokeStyle = 'black';
context.strokeRect(this.x, this.y, this.width + 2, this.height + 2)
}
context.drawImage(this.image, this.x, this.y);
context.restore();
我做错了什么?
【问题讨论】:
-
P.S 在使用
parseInt方法时使用radix 10 参数。 -
希望你能找到一些探索这个例子的线索:stackoverflow.com/questions/20163948/…
标签: javascript jquery html canvas html5-canvas