HTML5 画布的暂停和恢复?

发布于 2024-11-10 13:54:29 字数 537 浏览 0 评论 0原文

有没有办法暂停/恢复 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

征棹 2024-11-17 13:54:29

尝试在屏幕外绘制线条,而不是“暂停”画布: http://kaioa.com/node/103

会有相同的结果。

var renderToCanvas = function (width, height, renderFunction) {
  var buffer = document.createElement('canvas');
  buffer.width = width;
  buffer.height = height;
  renderFunction(buffer.getContext('2d'));
  return buffer;
};

(代码片段取自上述网页)

Try to draw the lines offscreen instead of "pausing" the canvas: http://kaioa.com/node/103

It will have the same result.

var renderToCanvas = function (width, height, renderFunction) {
  var buffer = document.createElement('canvas');
  buffer.width = width;
  buffer.height = height;
  renderFunction(buffer.getContext('2d'));
  return buffer;
};

(code fragment taken from above mentioned web page)

凉世弥音 2024-11-17 13:54:29

我解决无事发生时资源使用问题的一种方法是设置 var isStarted = false;

然后根据事物是否有动画来打开和关闭它。

然后使用该 var 来控制您的 setTimeout/requestAnimFrame

if (isStarted) {
  setTimeout(function() {}, 1000/33);// animation loop thing
}

我为我开始构建的游戏环境执行了此操作,并且效果完美。

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

if (isStarted) {
  setTimeout(function() {}, 1000/33);// animation loop thing
}

I did this for a game environment I started to build and it works perfectly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文