image.src to large image freezes while loading

荒凉一梦 提交于 2021-02-07 21:48:21

问题


I keep reading that images are already loaded asynchronously, or to use onload (which doesn't solve the problem). When loading a large image, specifically from canvas.toDataURL(), the whole page freezes while the image is being loaded.

What I'm doing specifically is prerendering a procedurally generated map (for a game) in a hidden canvas, then loading that data into an image to draw faster. The actual problem is setting the src, which happens even if I just img.src=img.src to invoke loading again.

Is there any way at all to make large images load over time instead of all at once and causing this huge disruption?


回答1:


This question is 6 years old but I stumbled across it and others may as well. I am guessing that you first load the image by assigning img.src="image.png", then in its onload you are doing the processing and then replacing the image's data with the processed data by going img.src = canvas.toDataURL(), which then fires the onload again and goes on in an endless recursion.



来源:https://stackoverflow.com/questions/24105931/image-src-to-large-image-freezes-while-loading

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!