gpt4 book ai didi

javascript - Canvas 动画显示所有先前的帧

转载 作者:行者123 更新时间:2023-12-02 15:53:10 27 4
gpt4 key购买 nike

我正在尝试旋转一个对象,但所有先前的帧仍然显示在 Canvas 上。我认为这与这一行有关:

setTimeout(this.rotate.bind(this), 1000 / 10);

如何让它只显示当前帧?

(function () {
var spinner = {
playerx: 810 * 0.5,
playery: 600 * 0.75,
shield: 1.80,
rotation: 0,
polyxx: [0, 12, 12, 0, -12, -12],
polyyy: [-13.92, -5.35, 5.35, 13.92, 5.35, -5.35],
size: 5,

canvasInit: function () {
this.canvas = document.getElementById('loadingScreen');
this.ctx = this.canvas.getContext('2d');
},

rotate: function () {

this.rotation += 0.02;
this.shield -= 0.01;
this.ctx.strokeStyle = 'rgba(255,255,255,' + this.shield + ')';

this.ctx.save();
this.ctx.translate(this.playerx, this.playery - 10);
this.ctx.rotate(this.rotation);
this.ctx.translate(-this.playerx, -(this.playery - 10));
this.ctx.beginPath();
this.ctx.lineWidth = 17;
for (var a = 0; a < 6; a++) {
this.ctx.lineTo(this.playerx + this.polyxx[a] * this.size, this.playery - 10 + this.polyyy[a] * this.size);
}

this.ctx.closePath();
this.ctx.stroke();
this.ctx.restore();


this.loop();

},

loop: function () {
setTimeout(this.rotate.bind(this), 1000 / 10);
},

init: function () {
this.canvasInit();
this.loop();
}
};
spinner.init();
})();
<body>
<canvas style="background:#000;" id="loadingScreen" width="810" height="600"></canvas>
</body>

最佳答案

每次制作动画时都需要清除 Canvas 。

this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height); 添加到 rotate 方法的开头以实现这个。

例如:

(function () {
var spinner = {
playerx: 810 * 0.5,
playery: 600 * 0.75,
shield: 1.80,
rotation: 0,
polyxx: [0, 12, 12, 0, -12, -12],
polyyy: [-13.92, -5.35, 5.35, 13.92, 5.35, -5.35],
size: 5,

canvasInit: function () {
this.canvas = document.getElementById('loadingScreen');
this.ctx = this.canvas.getContext('2d');
},

rotate: function () {
// Clear canvas here, so that we're ready to draw the next frame
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);

this.rotation += 0.02;
this.shield -= 0.01;
this.ctx.strokeStyle = 'rgba(255,255,255,' + this.shield + ')';

this.ctx.save();
this.ctx.translate(this.playerx, this.playery - 10);
this.ctx.rotate(this.rotation);
this.ctx.translate(-this.playerx, -(this.playery - 10));
this.ctx.beginPath();
this.ctx.lineWidth = 17;
for (var a = 0; a < 6; a++) {
this.ctx.lineTo(this.playerx + this.polyxx[a] * this.size, this.playery - 10 + this.polyyy[a] * this.size);
}

this.ctx.closePath();
this.ctx.stroke();
this.ctx.restore();


this.loop();

},

loop: function () {
setTimeout(this.rotate.bind(this), 1000 / 10);
},

init: function () {
this.canvasInit();
this.loop();
}
};
spinner.init();
})();
<body>
<canvas style="background:#000;" id="loadingScreen" width="810" height="600"></canvas>
</body>

注意

您可能还需要考虑使用 requestAnimationFrame 而不是 setTimeout 来提高动画性能。

关于javascript - Canvas 动画显示所有先前的帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31732774/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com