I am trying to draw and move an image using JS.
This code works up until the moveImage function which simply does nothing. Can anyone help me figure this out?
I believe that I can get the image to move if I place it in the html, but I would prefer to have the script code place it instead if possible.
function init() {
var x = 1, y = 1;
var context = document.getElementById("Vehicle").getContext("2d");
var img = new Image();
img.onload = function () { context.drawImage(img, x, y, 24, 20); }
img.src = "images/Image.png";
//move
function moveImage(imgX, imgY) {
img.style.left = imgX + "px";
img.style.top = imgY + 'px';
}
setInterval(function () {
var FPS = 60;
x++;
y++;
if (x > 1000) { x = 1; }
if (y > 1000) { y = 1; }
moveImage(x, y);
}, 1000/FPS);
};
My guess is that img.style.left/right is either not correct or that I am not pointing to img properly.
If there is no way to do this, is there a way for me to remove (not just hide) the image so I can redraw it in the new location?