HTML5 的 canvas 元素是否有重绘事件?

发布于 2024-08-27 12:46:19 字数 124 浏览 4 评论 0原文

正如标题所示,当画布元素的内容被重绘时,我需要一个通知。 这可能吗?

如果没有,重新绘制整个页面时发出通知也可以(重新绘制而不是重新加载!)。

我需要这个的原因是我想获取画布内运行的动画的当前 FPS。

As the title says, I need a notification when the content of a canvas element was redrawn.
Is this possible?

If not, a notification when the whole page was redrawn would also be ok (reDRAWN not reLOADED!).

The reason why I need this is that I want to get the current FPS of an animation running inside a canvas.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

番薯 2024-09-03 12:46:19

Canvas 本身没有“重绘”方法或事件。该架构使得任何函数都可以在任何时候在上下文中进行绘制。然而,一个可能的解决方案是“覆盖”标记新框架绘制的特定函数。具体来说,我认为重写 context.clearRect 将是可行的方法。

var context=canvas.getContext('2d');
context._clearRect=context.clearRect;
context.clearRect=function(x,y,w,h){
    context._clearRect(x,y,w,h);
    //do your stuff here
};

此方法的唯一问题是它假设每帧调用一次clearRect。不过,只要是这样,那么上面的代码就可以工作。

Canvas itself has no "redraw" method or event. The architecture is such that at any time, any function can draw in the context. However, a possible solution would be to "override" specific functions which mark the drawing of a new frame. Specifcially, I am thinking that overridding context.clearRect would be the way to go.

var context=canvas.getContext('2d');
context._clearRect=context.clearRect;
context.clearRect=function(x,y,w,h){
    context._clearRect(x,y,w,h);
    //do your stuff here
};

The only issue with this method is that it assumes that clearRect is called exactly once every frame. However, as long as this is the case, then the above code will work.

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