Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
31
rated 0 times [  37] [ 6]  / answers: 1 / hits: 64417  / 14 Years ago, wed, august 11, 2010, 12:00:00

there's an example, which loads 2 images:



    canvas = document.getElementById(canvas);
ctx = canvas.getContext(2d);

var img1 = new Image();
img.src = /path/to/image/img1.png;
img.onload = function() {
ctx.drawImage(img, 0, 0);
};

var img2 = new Image();
img2.src = /path/to/image/img2.png;
img2.onload = function() {
ctx.drawImage(img2, 100, 100);
};


I need to remove(replace) img2 from canvas. What is the best way to do it?


More From » html

 Answers
14

It's not clear what you want the canvas to show when the image is gone. If you want it to be transparent, you could get the image data and fill it with transparent pixels:



var img = ctx.createImageData(w, h);
for (var i = img.data.length; --i >= 0; )
img.data[i] = 0;
ctx.putImageData(img, 100, 100);


where w and h would be the width and height of your original image.



edit — if you just want another image there, why not just put one there? It will overwrite whatever pixels are there on the canvas.


[#95955] Monday, August 9, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
josuea

Total Points: 609
Total Questions: 121
Total Answers: 104

Location: South Georgia
Member since Fri, Nov 13, 2020
4 Years ago
josuea questions
;