I've been trying to get a start on creating a game with Javascript, but am currently stumped on how to get the animation going for a simple image. The image I am using is a black square png and when I use setInterval()
, it duplicates the image and doesn't remove the image from its previous state. It draws another image at each pixel but doesn't get rid of the last frame so it looks like a black rectangle growing upwards. I want the block to move upwards and delete the last drawn frame. What am I doing wrong?
my js code:
var cvs = document.getElementById("game");
var ctx = cvs.getContext("2d");
var block = new Image();
block.src = "images/Solid_black.png";
var imgCount = 1;
var blockX = 10;
var blockY = 462;
var speed = 5;
window.onload = function() {
setInterval(draw, 1);
}
function draw() {
if (--imgCount > 0) { return; }
console.log(cvs.clientWidth);
ctx.drawImage(block, blockX, blockY, 50, 50);
blockY--;
}
draw();