最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Is there a redraw-event for HTML5's canvas element? - Stack Overflow

programmeradmin1浏览0评论

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.

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.

Share Improve this question edited Apr 18, 2010 at 7:35 Oded 500k102 gold badges893 silver badges1k bronze badges asked Apr 1, 2010 at 12:37 Timo ErnstTimo Ernst 16k25 gold badges115 silver badges174 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

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.

发布评论

评论列表(0)

  1. 暂无评论