我正在尝试一个示例代码,该代码将彩色图像转换为 WebOS (enyo) 画布上的灰度图像。当我使用 ctx.getImageData 方法读取像素时,imageData 仅包含零。我正在使用的示例在下面的链接中提供:
http://chopapp.com/#x8t2ymad
是否有 WebOS支持从canvas读取像素数据吗?我在这里做错了什么吗?
我参考了以下链接的逻辑和代码:
http://net.tutsplus.com/tutorials/javascript-ajax/how-to-transition-an-image-from-bw-to-color-with-canvas/
这有效美好的。
I am trying out a sample code that converts a color image to grey scale on a canvas in WebOS (enyo). When I use the ctx.getImageData method to read the pixels, the imageData contains only zeros. The sample I am using is provided in the link below:
http://chopapp.com/#x8t2ymad
Does WebOS support reading pixel data from canvas? Am I doing something wrong here?
I have refered to the following link for the logic and the code:
http://net.tutsplus.com/tutorials/javascript-ajax/how-to-transition-an-image-from-bw-to-color-with-canvas/
This works fine.
发布评论
评论(1)
您应该将 getimagedata 移至图像的 onload 事件 的回调中。
类似于:
并在绑定事件后设置源
以避免竞争情况
,现在在加载实际像素之前检索图像数据。这会导致一个空数组。
you should move the getimagedata in the callback from the onload event of the image.
something like:
and set the source after binding the event
to avoid a racing condition
now the imagedata is retrieved before the actual pixels are loaded. Which results in an empty array.