Editor
(write code below)
var canvas = document.getElementById('clear_rect_interactive1');
var context = canvas.getContext('2d');
if (window.animation) clearTimeout(window.animation);
context.save();
context.clearRect(0, 0, canvas.width, canvas.height);
var boxX = 0;
var boxY = 0;
var boxCommandQueue = [];
function setLoopInstructions() {
boxX = 50;
boxY = 50;
moveBox(1, 0, 100);
moveBox(1, 1, 200);
moveBox(-1, 0, 100);
moveBox(-1, -1, 100);
moveBox(0, 1, 100);
moveBox(1, -1, 200);
moveBox(-1, 0, 300);
}
function runAnimation() {
if (boxCommandQueue.length > 0) {
context.clearRect(0, 0, canvas.width, canvas.height);
var command = boxCommandQueue.shift();
drawBox(command[0], command[1]);
} else {
setLoopInstructions();
}
window.animation = setTimeout(runAnimation, 10);
}
function moveBox(xShift, yShift, steps) {
for (var i=0; i < steps; ++i) {
boxCommandQueue.push([xShift, yShift]);
}
}
function drawBox(xShift, yShift) {
boxX += xShift;
boxY += yShift;
context.fillRect(boxX, boxY, 25, 25);
}
drawBox(0, 0);
runAnimation();
context.restore();