Home > Article > Web Front-end > A brief discussion on using cache to optimize the performance of HTML5 Canvas program_html5 tutorial skills
After you play with canvas too much, you will automatically start to consider performance issues. How to optimize canvas animation?
【Use cache】
Using cache means using off-screen canvas for pre-rendering. The principle is very simple, that is, first draw into an off-screen canvas, and then draw the off-screen canvas into the main canvas through drawImage. Many people may misunderstand this. Isn't this a double-buffering mechanism used a lot in games?
In fact, the double buffering mechanism is used in game programming to prevent screen flickering. Therefore, there will be a canvas displayed in front of the user and a background canvas. When drawing, the screen content will first be drawn to the background canvas, and then the background canvas will be drawn. The data in the canvas is drawn to the front canvas. This is double buffering, but there is no double buffering in canvas, because modern browsers basically have a built-in double buffering mechanism. Therefore, using off-screen canvas is not double buffering, but treating off-screen canvas as a cache area. Cache screen data that needs to be drawn repeatedly to reduce the consumption of calling the canvas API.
As we all know, calling the canvas API consumes performance. Therefore, when we want to draw some repeated screen data, proper use of off-screen canvas can greatly improve performance. You can take a look at the following DEMO
2. Cache is used but the width and height of the off-screen canvas are not set
3. Cache is used but the width and height of the off-screen canvas are not set
4. Use caching and set the width and height of the off-screen canvas
You can see that the performance of the above DEMO is different. Let’s analyze the reasons below: In order to achieve the style of each circle, I used loop drawing when drawing circles. If caching is not enabled, when the number of circles on the page When it reaches a certain point, a large number of canvas API calls are required for each frame of the animation, and a large amount of calculations are required, so that no matter how good the browser is, it will be brought down.
XML/HTML CodeCopy content to clipboard
所以,我的方法很简单,每个圈圈对象里面给他一个离屏canvas作缓存区。
除了创建离屏canvas作为缓存之外,下面的代码中有一点很关键,就是要设置离屏canvas的宽度和高度,canvas生成后的默认大小是300X150;对于我的代码中每个缓存起来圈圈对象半径最大也就不超过80,所以300X150的大小明显会造成很多空白区域,会造成资源浪费,所以就要设置一下离屏canvas的宽度和高度,让它跟缓存起来的元素大小一致,这样也有利于提高动画性能。上面的四个demo很明显的显示出了性能差距,如果没有设置宽高,当页面超过400个圈圈对象时就会卡的不行了,而设置了宽高1000个圈圈对象也不觉得卡。
When I instantiate the circle object, I directly call the cache method and draw the complex circle directly into the off-screen canvas of the circle object and save it.
XML/HTML Code
XML/HTML Code
There is another note about off-screen canvas. If the effect you do is to continuously create and destroy objects, please use off-screen canvas with caution. At least do not bind the attributes of each object as I wrote above. Set off-screen canvas.
Because if bound like this, when the object is destroyed, the off-screen canvas will also be destroyed, and a large number of off-screen canvases are constantly being created and destroyed, which will cause the canvas buffer to consume a lot of GPU resources and easily cause the browser to Crash or serious frame freeze. The solution is to create an off-screen canvas array, preload a sufficient number of off-screen canvases, cache only the objects that are still alive, and uncache them when the objects are destroyed. This will not cause the off-screen canvas to be destroyed.
【Use requestAnimationFrame】
I won’t explain this in detail. I guess many people know that this is the best loop for animation, not setTimeout or setInterval. Directly post the compatibility writing method:
【Avoid floating point operations】
Although JavaScript provides some very convenient rounding methods, such as Math.floor, Math.ceil, and parseInt, foreign friends have done tests and the parseInt method does some extra work (such as detecting whether the data is valid value, parseInt even converts the parameter into a string first!), so using parseInt directly is relatively more performance-intensive. So how to round up, you can directly use a very clever method written by foreigners:
1.rounded = (0.5 somenum) | 0;
2.rounded = ~~ (0.5 somenum); 3.rounded = (0.5 somenum) << 0;
【Reduce canvasAPI calls as much as possible】
When making particle effects, try to use circles as little as possible and preferably use squares. Because the particles are too small, squares look similar to circles. As for the reason, it is easy to understand. We need three steps to draw a circle: first beginPath, then use arc to draw an arc, and then use fill to fill it to produce a circle. But to draw a square, you only need one fillRect. Although there is only a difference of two calls, when the number of particle objects reaches a certain level, the performance gap will show up.
There are some other things to note, I won’t list them all because there are quite a few on Google. This can be regarded as a record for myself, mainly to record the usage of cache. If you want to improve the performance of canvas, the most important thing is to pay attention to the structure of the code, reduce unnecessary API calls, reduce complex operations in each frame, or change complex operations from once for each frame to once for several frames. At the same time, for the cache usage mentioned above, for convenience, I used an off-screen canvas for each object. In fact, off-screen canvas cannot be used too extensively. If you use too many off-screen canvases, there will be performance problems. Please try your best. Make reasonable use of off-screen canvas.
Source code address: https://github.com/whxaxes/canvas-test/tree/gh-pages/src/Other-demo/cache