【发布时间】:2020-05-08 21:30:14
【问题描述】:
我画了canvas 并在上面添加了Plane。当我使用setInterval() 时canvas 和Plane 工作,但如果我使用requestAnimationFrame(),Plane 就会消失。 Plane 图像是本地的,其位置取决于鼠标位置。
const canvas = this.refs.canvas
const contex = canvas.getContext('2d')
function DrawBoard() {
contex.fillStyle = "black"
contex.fillRect(0, 0, 1200, 350)
}
function Plane() {
const image1 = new Image()
image1.src = "Plane.png"
image1.onload = function () {
contex.drawImage(image1, planeX - 110, planeY - 90, 200, 160);
}
}
function Game() {
DrawBoard()
Plane()
}
function Animate() {
Game()
window.requestAnimationFrame(Animate)
}
Animate()
【问题讨论】:
-
您在每次
Plane()调用时创建图像,我想您可以在声明画布后创建它并使用该函数将图像放置在您想要的位置。
标签: javascript node.js reactjs html5-canvas requestanimationframe