Skip to main content

JavaScript canvas remove image

I'm writing a platformer and I want to implement own art, now I know how to display it and this isn't my question. It rather is how do I remove it? I tried drawing a rectangle over it and removing it, but that doesn't seem to work, here's my code:

function clearImage(id) {
    for (var i = 0; i < 6;) {
        if (img_id[i] == id) {
            console.log(img_loc_x[i]);
            console.log(img_loc_y[i]);
            ctx.fillRect(img_loc_x[i], img_loc_y[i], "1000", "1000");
            ctx.clearRect(img_loc_x[i], img_loc_y[i], "1000", "1000");

            img_loc_x[i] = null;
            img_loc_y[i] = null;
            img_id[i] = null
            console.log("Cleared image with id: "+id+" succesfully!");
        } else {
            i++;
        }
    }
}

I'm using some arrays to store the image data such as the location and an id form management, but I didn't implement a method yet to get the dimensions of it, thats why I'm giving it the size parameters. (Strangely it works if I enter the fillRect and clearRect commands in the console)

Betrahm
  • 11
  • 1
  • 1
  • 2