简体   繁体   English

clearRect后无法在画布上绘制图像

[英]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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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