I have a page with fairly heavy (average weight) canvas
operations. In order to serve users on mobile devices and old computers, I thought that I could implement a mechanism that checks whether the canvas element is really visible, and decide whether to perform constant calculations and canvas updates (animation running at 30 frames per second), or not.
This works great, but when performing a performance test using the Chrome Dev Tools, I noticed that even when I turn off the visibility check and just let things constantly show that the CPU usage of this function drops very little, no part of the canvas’s elements (s) are visible (although theoretically, it should still perform the same tasks). So: at least on my computer running Chrome 17, it doesn't really matter if I check the actual visibility of the item.
To shorten the long story: do I need to do this or are the browsers smart enough to handle such a case without even telling them (and I can save the visibility check)?
EDIT
So I did some “research” on this topic and created this script.
What happens is that it generates noise at a speed of 30 frames per second. Not too nice for the eyes, but, well ... The top is just a div
to block the viewport. When I scroll down and I have a canvas
element in the viewport, then using the processor tells me that it takes about 40%, so there seems to be a lot of browser here. When I scroll through the backup, so that I only have a maroon div
color in my viewport and the CPU usage profiling is reduced to 10%. When I scroll back, usage again increases.
So, when I implement a visibility check, as in this modified fiddle , I see an increase (a tiny one to be honest) in using the CPU instead of falling (since it has the additional task of checking if the canvas is inside the viewport).
So I’m still wondering if this is some side effect of what I don’t know (or am I making some serious mistake when profiling), or if I can expect browsers to be smart enough to handle such situations?
If anyone could shed light on this, I would be very grateful!