Skip to main content
1 of 2
Betrahm
  • 11
  • 1
  • 1
  • 2

JavaScript canvas remove image

im writing a platformer and 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 heres 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++;
        }
    }
}

im 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 im giving it the size parameters. (Strangely it works if i enter the fillRect and clearRect commands in the console)

Betrahm
  • 11
  • 1
  • 1
  • 2