HTML5 画布的暂停和恢复?
有没有办法暂停/恢复 HTML5 Canvas?
说出我的代码:
// Draw lines with decreasing widths
for (i = 20; i > 0; i--)
{
var v=i*20
ctx.strokeStyle = "rgb("+v+", "+v+", "+v+")";
ctx.lineWidth = i;
ctx.beginPath();
ctx.moveTo(55, 20 + (20 - i) * 24);
ctx.lineTo(335, 20 + (20 - i) * 24);
ctx.stroke();
}
在代码开始时,我想 Pause() 意思是,我会告诉浏览器“好吧,你现在不必浪费任何资源进行任何实际绘图”,我只是要告诉你命令。然后在循环之后,我将调用 Resume() ,意思是“好吧,你现在可以开始绘制它们了”
顺便说一句,有人知道 JavaScript 中是否有上下文对象的完整引用(我在 google 和 MDC 中都找不到它......)
is there a way to pause / resume for HTML5 Canvas?
Say my code:
// Draw lines with decreasing widths
for (i = 20; i > 0; i--)
{
var v=i*20
ctx.strokeStyle = "rgb("+v+", "+v+", "+v+")";
ctx.lineWidth = i;
ctx.beginPath();
ctx.moveTo(55, 20 + (20 - i) * 24);
ctx.lineTo(335, 20 + (20 - i) * 24);
ctx.stroke();
}
At the start of the code, I would like to Pause() meaning, i will tell the browser "ok you don't really have to waste any resources doing any actual drawing right now", i'm just gonna tell you the commands. Then after looping through i will call Resume() meaning "ok you can start drawing them now"
Btw does anyone know if there is a complete reference for the context object in javascript (I can't find it in google nor MDC..)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试在屏幕外绘制线条,而不是“暂停”画布: http://kaioa.com/node/103
会有相同的结果。
(代码片段取自上述网页)
Try to draw the lines offscreen instead of "pausing" the canvas: http://kaioa.com/node/103
It will have the same result.
(code fragment taken from above mentioned web page)
我解决无事发生时资源使用问题的一种方法是设置 var isStarted = false;
然后根据事物是否有动画来打开和关闭它。
然后使用该 var 来控制您的 setTimeout/requestAnimFrame
我为我开始构建的游戏环境执行了此操作,并且效果完美。
One way I have solved this issue of resource usage when nothing is happening is by setting a var isStarted = false;
Then turn it on and off based on whether or not things are animating.
Then use that var to control your setTimeout/requestAnimFrame
I did this for a game environment I started to build and it works perfectly.