Skip to content

Instantly share code, notes, and snippets.

@domenicrosati
Last active October 14, 2018 21:05
Show Gist options
  • Save domenicrosati/2af46406b7a73585e641f76d180924e9 to your computer and use it in GitHub Desktop.
Save domenicrosati/2af46406b7a73585e641f76d180924e9 to your computer and use it in GitHub Desktop.
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
function drawRect(x, y) {
ctx.fillStyle = "green";
ctx.fillRect(x, y, 50, 50);
}
// box co-ordinates
var x = 0;
var y = 0;
// destroy?
var destroy = false;
function draw() {
ctx.clearRect(0, 0, 500, 500); // clear canvas
drawRect(x, y);
if(destroy != true) {
if (x <= 450) {
x = x + 5;
}
}
if (destroy == true) {
x = x - 5;
}
if (x == 450) {
destroy = true;
}
if (x == 0) {
destroy = false;
}
window.requestAnimationFrame(draw);
}
window.requestAnimationFrame(draw);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment