简体   繁体   English

添加到画布的图像的奇怪行为

[英]strange behavior of image adding to canvas

I try to display some images on canvas that should displayed on click event. 我尝试在画布上显示一些应该在click事件上显示的图像。 And everything seems ok, but I found some bug, that I cannot fix for a long. 一切似乎都没问题,但我发现了一些错误,我无法解决这个问题。 Bug is that on the first page opening lamp image does not display. 错误是在第一页打开灯图像不显示。 It can be reproduced by following steps: 它可以通过以下步骤复制:

  1. Open link in browser; 在浏览器中打开链接 ;
  2. click to button image <- clear rectangle will be displayed 单击按钮图像< - 将显示清除矩形
  3. click again. 再次点击。 <- rectangle will be disappeared < - 矩形将消失
  4. click again. 再次点击。 <- rectangle with image will be displayed < - 将显示带图像的矩形

Why image doesnot display first time? 为什么图像不会第一次显示?

At least you have one (very common) error in your code. 至少你的代码中有一个(非常常见的)错误。 The method .onload expects to be assigned with a function reference. 期望为.onload方法分配函数引用。 You are assigning it to the result (the return value) of the immediate function call processButtonImages(i, delta, buttons[i], object) . 您将它分配给立即函数调用processButtonImages(i, delta, buttons[i], object)的结果(返回值processButtonImages(i, delta, buttons[i], object) So as long as you are not returning a function reference from this function call (which you don't) this will not work as expected. 因此,只要您没有从此函数调用返回函数引用(您没有),这将无法按预期工作。

You are calling and setting somethings in image onload event. 您在图像onload事件中调用并设置了一些东西。 So, at first it doesn't execute the whole thing correctly. 所以,起初它并没有正确地执行整个事情。 Check this jsfiddle. 检查这个jsfiddle。 I edited the code at the end, http://jsfiddle.net/sKymY/14/ 我在最后编辑了代码, http://jsfiddle.net/sKymY/14/

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

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