How do I know when HTML5 Canvas#39; Rendering is Finished?(我如何知道 HTML5 Canvas 的渲染何时完成?)
问题描述
html:
<canvas id="cnv" width="786" height="1113">
js:
var img = new Image(),
cnv = document.getElementById('cnv');
var context = cnv.getContext('2d');
img.onload = function () {
context.drawImage(img, 0, 0, 786, 1113);
alert('finished drawing');
}
img.src = "aHR0cHM6Ly9zczAuYmRzdGF0aWMuY29tLzVhVjFianFoX1EyM29kQ2Yvc3RhdGljL3N1cGVybWFuL2ltZy9sb2dvL2xvZ29fd2hpdGVfZmU2ZGExZWMucG5n";
http://jsfiddle.net/FxrSa/14/
我想在画布完成渲染后显示警报.但是在绘制图像之前会显示警报.
I want to show the alert after the canvas finished his rendering. But the alert show before the image is drawn.
如何等待 GUI 线程完成他的渲染?
How can I wait for the GUI thread to finish his rendering?
推荐答案
Canvas drawImage
是同步的.甚至,真正同步.
Canvas drawImage
is synchronous. And even, really synchronous.
您的问题是 alert()
正在阻塞,甚至是真正阻塞.
Your problem is that alert()
is blocking, and even really blocking.
我的意思是它不仅会阻止 js 执行,还会阻止页面渲染.所以浏览器已经在你的画布上绘制了你的图像,但是当你调用 alert()
时还没有显示它.
By this I mean it does not only block the js execution, it also blocks the page rendering. So the browser has painted your image on your canvas, but didn't display it yet when you called alert()
.
应始终避免使用 alert
并将其替换为普通的 DOM 元素.
One should always avoid alert
and replace it with normal DOM elements.
Ps:这是一个证据,您的图像确实已经绘制在画布上.
Ps: Here is a proof that your image is indeed already painted on the canvas.
这篇关于我如何知道 HTML5 Canvas 的渲染何时完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:我如何知道 HTML5 Canvas 的渲染何时完成?


- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01
- 400或500级别的HTTP响应 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06