Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to buffer graphics for an html5 canvas

Tags:

html5-canvas

I'm working on a game which is beginning to get pretty graphically intensive. There are lots of points, arcs, and gradients that need to be drawn. Problem is that drawing all these graphics is beginning to get slow. RGBA radial gradients seem to take an exceptionally long time to draw when drawn over top of other gradients (ie, for the background).

If there was some way to buffer the graphics, that could save me a lot computations every frame. According to this question graphics buffering can be accomplished by creating a hidden html5 canvas on the html document. Unfortunately this will not work because I need to be able to buffer an undefined number of graphics for the game.

Is there any way to buffer a graphics for an html5 canvas?

like image 904
JSideris Avatar asked Dec 27 '11 18:12

JSideris


People also ask

Is HTML5 canvas still used?

The HTML5 canvas has the potential to become a staple of the web, enjoying ubiquitous browser and platform support in addition to widespread webpage support, as nearly 90% of websites have ported to HTML5.

How do you optimize a canvas?

The best canvas optimization technique for animations is to limit the amount of pixels that get cleared/painted on each frame. The easiest solution to implement is resetting the entire canvas element and drawing everything over again but that is an expensive operation for your browser to process.


1 Answers

From 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;
};
like image 65
JSideris Avatar answered Nov 12 '22 11:11

JSideris