【发布时间】:2021-08-30 20:38:36
【问题描述】:
我试图让一个矩形在游戏循环中逆时针旋转 10 度。我希望盒子只旋转 10 度。不要在下一个循环中再增加 10 度,因为这就是它正在做的事情:
这是我目前的绘图功能:
...
class Bok {
static width = 17;
static height = 12;
constructor(position) {
this.position = position;
}
draw() {
...
ctx.beginPath();
ctx.rect(this.position.x, this.position.y, 17, 12);
ctx.fillStyle = "red";
ctx.strokeStyle = "black";
ctx.lineWidth = 10;
ctx.translate(this.position.x + Bok.width / 2, this.position.y + Bok.height / 2);
// this keeps adding 10 degrees to the box every update,
// how can i rotate it 10 degrees without adding on to the
// previous rotation?
ctx.rotate(10);
ctx.translate(-(this.position.x + Bok.width / 2), -(this.position.y + Bok.height / 2));
ctx.stroke();
ctx.fill();
...
}
...
【问题讨论】:
-
您是只想阻止轮换累加,还是还要阻止翻译等?
-
我只想防止轮换叠加,并且能够在每个更新循环中设置不同的轮换。
标签: rotation html5-canvas rectangles