简体   繁体   中英

can't draw image on canvas after clearRect

i am having some trouble with the following code. Essentially this is wrapped up in an image uploader. So once the uploader completes its callback the following code gets executed:

var c = canvas.getContext('2d');
var logoImage = new Image();
logoImage.onload = function() {

   c.clearRect(0, 0, canvas.width, canvas.height);
   c.drawImage(logoImage, 0, 0, 200, 300); // i've simplified this call...
}
logoImage.src = "/path/to/" + fileName;

So this works the first time. But I expected that it would work subsequent times as well. So when another image is uploaded, the canvas is cleared and that image is drawn. Instead what happens is that the canvas is cleared but no further drawing takes place...

Any ideas?

try this:

replace the

 c.clearRect(0, 0, canvas.width, canvas.height);

with:

canvas.width=canvas.width;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM