Question

I understand it's often faster to pre-render graphics to an off-screen canvas. Is this the case for a shape as simple as a circle? Would it make a significant difference for rendering 100 circles at a game-like framerate? 50 circles? 25?

Was it helpful?

Solution

To break this into two slightly different problems, there are two aspects to what you're asking:

1) is drawing a shape off-screen and putting it on-screen faster

2) is drawing a shape one time and copying it to 100 different places faster than drawing a shape 100 times

The answer to the first one is "it depends".
That's a technique known as "buffering" and it's not really about speed.

The goal of buffering an image is to remove jerkiness from it.
If you drew everything on-screen, then as you loop through all of your objects and draw them, they're updating in real-time.
In the NES-days, that was normal, because there wasn't much room in memory, or much power to do anything about it, and because programmers didn't know much better, with the limited instructions they had to work with.

But that's not really the way games do things, these days.
Typically, they call all of the draw updates for one frame, then they take that whole frame as a finished image, and paste that whole thing on the screen.
The GPU (and GL/DirectX) takes care of this, by default, in a process called "double-buffering".
It's a double-buffer, because there's room for the "in-progress" buffer used for the updates, as well as the buffer that holds the final image from the last frame, that's being read by the monitor.
At the end of the frame processing, the buffers will "swap". The newly full frame will be sent to the monitor and the old frame will be overwritten with the new image data from the other draw calls.

Now, in HTML5, there isn't really access to the frame-buffer, so we do it ourselves; make every draw call to an offscreen canvas. When all of the updates are finished (the image is stable), then copy and paste that whole image to the onscreen canvas.

There is a large speed-optimization in here, called "blitting", which basically copies over only the parts that have changed, and reuses the old image.
There's a lot more to it than that, and there are a lot of caveats, these days, because of all of the special-effects we add, but there it is.

The second part of your question has to do with a concept called "instancing".
Instancing is similar to blitting, but while blitting is about only redrawing what's changed, instancing is about drawing the exact same thing several times in different places.

Say you're painting a forest in Photoshop.
You've got two options:

  1. Draw every tree from scratch.
  2. Draw one tree, copy it, paste it all over the image.

The downside of the second one is that each "instance" of the image looks exactly the same.
If your "template" image changes colour or takes damage, then all instances of the image do, too. Also, if you had 87 different tree variations for an 8000 tree forest, making instances of them all would still be very fast, but it would take more memory, because you now need to save 87x more images than when it was just one tree, to reference on every draw call.

The upside is that it's still much, much faster.

To answer your specific question about X circles, versus instancing 1 circle:
Yes, it's still going to be a lot faster.

What a "lot" means, though, will change based on a lot of different things, because now you're talking about browsers on PCs.

How strong is the PC?
How good is the videocard?
How large is the canvas in software-pixels (not CSS pixels)? How large are the circles? Do they have alpha-blending?
Is this written in WebGL or software?
If software is the canvas compositing in hardware mode?

For a typical PC, you should still be able to hit 60fps in Chrome, drawing 20 circles, I think (depending on what you're doing to them... ...just drawing them onscreen, every frame is simple), so in this case, the instances are still a "lot" faster, but it's not going to matter, because you've already passed the performance-ceiling of Canvas.

I don't know that the same would be true on phones/tablets, or battery-powered laptops/netbooks, though.

OTHER TIPS

Yes, transferring from an offscreen canvas is faster than even primitive drawings like an arc-circle.

That's because the GPU just copies the pixels from the offscreen canvas (not much CPU effort required)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top