Over a million developers have joined DZone.

Livecoding: Reaching the Limits of Canvas Redraw Speed

DZone's Guide to

Livecoding: Reaching the Limits of Canvas Redraw Speed

Want to bring out your inner artist through your code? Read on to learn how to use React and Konva to make animations in your web application.

· Web Dev Zone ·
Free Resource

Access over 20 APIs and mobile SDKs, up to 250k transactions free with no credit card required

Help, I need an expert! It looks like we pushed the particle generator to the limits of canvas performance. But that can’t be right, can it? Surely I’m still doing something wrong.

Pure canvas frame redraw speed

Pure canvas frame redraw speed

We removed everything that could possibly slow us down. There’s no more React or Konva for the main drawing part. All that’s left are the raw HTML5 canvas APIs. Despite our best efforts, it still takes almost 50 milliseconds to draw 10,000 circles.

A huge improvement from last time, yes. But it’s not good enough. With a drawing time of 50ms per frame, the upper bound on my laptop is about 20 frames per second. Animated, but choppy.

And there are some 5ms of React and Redux overhead on top of that. Down to 18 frames per second.


The drawing code couldn’t be simpler:

drawParticle(particle) {
    let { x, y } = particle;

    this.context.arc(x, y, 1, 0, 2*Math.PI, false);

componentDidUpdate() {
    let particles = this.props.particles;

    this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);

    this.context.lineWidth = 1;
    this.context.strokeStyle = 'black';

    for (let i = 0; i < particles.length; i++) {

Even profiling confirms that drawing has become the slow part:

Profiling shows canvas operations are the bottleneck

Profiling shows canvas operations are the bottleneck

I am at a loss. What else is there to try? There’s no lower layer of abstraction to drop to. Canvas is the bottom. It’s the last in the chain.

Well … there’s always manual bitmaps and those base64-encoded strings for images. If you knew how to manipulate those directly, you could trick the browser … no, that’s a silly idea. We’re not doing that. That’s crazy talk.

A friend suggested drawing a single circle in an off-screen canvas, then using drawImage to copy-paste it around. But we tried that with Konva, and it didn’t quite work. Maybe it was a Konva problem, and it’s going to work better if we do it ourselves.

I dunno. It does look like stroke and arc are the biggest bottlenecks in drawCircle. Sprites might help.

Oh, another optimization that we made was to give Konva more context about what’s going on. Disable most transformations with transformsEnabled = 'position' on all shapes, and set listening = false. That way it knows not to try scale/rotation transformations, and not to listen for events.

It helped a bit, but not as much as going to the bare metal for the key drawing part.

Next time, we’ll try the drawImage approach, and if that doesn’t work, we’re going to start exploring WebGL. If that can’t do it, nothing can.

Are you a canvas expert? Please help. What am I doing wrong?

Try the particle generator.

#1 for location developers in quality, price and choice, switch to HERE.

web dev ,react ,web application development ,web animations

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}