4 ms·
You pretty much have to implement it that way. Not only because the canvas can later be shown, as halter73 points out, but also because of the getImageData API
by bzbarsky 15y ago
You pretty much have to implement it that way. Not only because the canvas can later be shown, as halter73 points out, but also because of the getImageData API, which allows one to extract the pixel data from a canvas that's not being shown any time you feel like it.
Also, a hidden canvas can be drawn to other canvases via drawImage, can be serialized to a data: URI at any time and so forth.
So you really do need to be able to produce the right pixel data on demand. That means either doing the drawing when told to draw or buffering up the drawing commands (which can get pretty complicated, because some of them depend on the style of the canvas at the time the draw happened, so you either have to cache a bunch of style information or flush the drawing command buffer when style information changes or ... something). And as halter73 points out, it's very easy to end up with an effective memory leak when doing this sort of buffering.