Canvas 的 GWT 图像加载( onLoad )
在不使用任何图像加载库的情况下,我加载和处理图像如下:
ImageGenerator(){
for(int i=0;i<100;i++){
ImageProcess( data-i , i );
}
}
ImageProcess( String data , int i ){
final Image img = new Image(data);
img.setVisible(false);
RootPanel.get().add(img);
GWT.log( "image " + i );
img.addLoadHandler(new LoadHandler() {
@Override
public void onLoad(LoadEvent event) {
GWT.log( "onload " + i + " " + img.getWidth() );
ImageElement data = ImageElement.as(img.getElement());
RootPanel.get().remove(img);
obj.setData(data);
..obj processing
}
});
}
结果:
image 1;
image 2;
image 3;
..
image 98;
image 99; <-- finish for loop
onload 1; <-- start loading image
onload 2;
onload 3;
..
onload 98;
onload 99;
但我希望它看起来像:
image 1; <-- generate image and load image step by step
onload 1;
image 2;
onload 2;
image 3;
onload 3;
..
image 98;
onload 98;
image 99;
onload 99;
我知道在为 img.src 赋值后,图像不会立即可用。必须先加载它。图像加载完成后,img 元素将触发 onload 事件。我尝试添加计时器并将图像放入不同的 DOM 元素。这不起作用。
我的问题是,如何触发图像元素立即加载?或者让 ImageProcess 等待 onload 完成?
Without using any image loading library, I am loading and processing images like below:
ImageGenerator(){
for(int i=0;i<100;i++){
ImageProcess( data-i , i );
}
}
ImageProcess( String data , int i ){
final Image img = new Image(data);
img.setVisible(false);
RootPanel.get().add(img);
GWT.log( "image " + i );
img.addLoadHandler(new LoadHandler() {
@Override
public void onLoad(LoadEvent event) {
GWT.log( "onload " + i + " " + img.getWidth() );
ImageElement data = ImageElement.as(img.getElement());
RootPanel.get().remove(img);
obj.setData(data);
..obj processing
}
});
}
The result :
image 1;
image 2;
image 3;
..
image 98;
image 99; <-- finish for loop
onload 1; <-- start loading image
onload 2;
onload 3;
..
onload 98;
onload 99;
But I hope it looks like:
image 1; <-- generate image and load image step by step
onload 1;
image 2;
onload 2;
image 3;
onload 3;
..
image 98;
onload 98;
image 99;
onload 99;
I know that after I assigned a value to img.src the image is not instantly available. It has to be loaded first. The img element will fire an onload event, as soon as the image is done loading. I try to add timer and put image to different DOM element. It doesn't work.
My questions are, How to trigger the image element to load immediately? or let ImageProcess wait until onload finish ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过这一行,您可以强制直接加载图像。
我认为你的问题是,循环在从服务器传输一个图像之前就完成了 - 因此你首先看到图像输出,然后看到加载输出。
现在我不确定为什么添加图像、执行其加载、添加下一个图像等等对您来说如此重要......
但我确信通过编写一个将图像添加到的方法可以轻松实现这一点DOM,并在其 onload 处理程序中为下一个图像再次调用该方法。依此类推,直到达到停止标准。
问候
斯特凡
With this line you force the image to load directly.
I think your problem is, that the loop finished before even one image has been transferred from the server - therefor you see the image outputs first and then the onload outputs.
Now I am not sure why it is so important for you to add an image, perform its onload, add the next image, and so on...
But I am sure this can be easily achieved by writing a method that adds an image to the DOM, and in its onload handler you call the method again for the next image. And so on until you reach your stop criterion.
Greetings
Stefan