【发布时间】:2019-11-20 13:35:56
【问题描述】:
我正在尝试在画布中制作简单的动画,尝试通过更新 x 轴将矩形向右移动,但它不起作用我不知道为什么?
我被困住了,我需要帮助和感谢吗?
类主
// import zone
import Player from "./player.js";
import Loop from "./loop.js";
// var zone
let canvas = document.getElementById("canvas"); // get canvas
let display = canvas.getContext("2d"); // get context
let loop;
loop = new Loop(canvas, display); // instance loop
// draw loop
function drawLoop() {
loop.player.point.x++;
loop.update(); // update
loop.draw(); // draw
requestAnimationFrame(drawLoop); // loop
}
// invoke
drawLoop();
类循环
// import zone
import Player from "./player.js";
// class
export default class Loop {
// init
constructor(canvas, display) {
this.canvas = canvas;
this.display = display;
this.player = new Player(this.canvas, this.display);
}
// update
update() {
this.player.update();
}
// draw
draw() {
this.player.draw();
}
}
职业选手
// import zone
import Size from "./size.js";
import Point from "./point.js";
// class
export default class Player {
constructor(canvas, display) {
this.display = display;
this.canvas = canvas;
this.point = new Point(200, 200);
this.size = new Size(100, 25);
}
// update
update() {
this.point.x++; // trying to update x but the rect does not move
}
// draw
draw() {
this.display.fillStyle = "#ffffff";
this.display.fillRect(this.point.x, this.point.y, this.size.w, this.size.h);
}
}
上课点
export default class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
}
矩形应该向右移动 1 个像素,谢谢。
【问题讨论】:
标签: javascript animation ecmascript-6 graphics html5-canvas