我一直在學習一些JavaScript,這是練習之一。它工作得很好,但其中一個挑戰是讓動畫從右向左移動,而我一直在那裏停留。我敢肯定,是非常簡單的,但一切我都試過是行不通的如何讓此動畫從右向左移動?
"use strict";
var Test = {
canvas: undefined,
canvasContext: undefined,
rectanglePosition: 0
};
Test.start = function() {
Test.canvas = document.getElementById("myCanvas");
Test.canvasContext = Test.canvas.getContext("2d");
Test.mainLoop();
};
document.addEventListener('DOMContentLoaded', Test.start);
Test.update = function() {
var d = new Date();
Test.rectanglePosition = d.getTime() % Test.canvas.width;
};
Test.draw = function() {
Test.canvasContext.fillStyle = "green";
Test.canvasContext.fillRect(Test.rectanglePosition, 100, 50, 50);
};
Test.mainLoop = function() {
Test.clearCanvas();
Test.update();
Test.draw();
window.setTimeout(Test.mainLoop, 1000/60);
};
Test.clearCanvas = function() {
Test.canvasContext.clearRect(0, 0, Test.canvas.width, Test.canvas.height);
};
<div id="testArea">
<canvas id="myCanvas" width="800" height="480"></canvas>
</div>
這一行需要改變。現在,X正在變得積極,但是你需要使它向左走: - Test.rectanglePosition = d.getTime()%Test.canvas.width;'祝你好運 – ndugger