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.
Join the DZone community and get the full member experience.
Join For Free
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
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.
ugh!
the drawing code couldn’t be simpler:
drawparticle(particle) {
let { x, y } = particle;
this.context.beginpath();
this.context.arc(x, y, 1, 0, 2*math.pi, false);
this.context.stroke();
}
componentdidupdate() {
let particles = this.props.particles;
console.time('drawing');
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++) {
this.drawparticle(particles[i]);
}
console.timeend('drawing');
}
even profiling confirms that drawing has become the slow part:
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?
Published at DZone with permission of Swizec Teller, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments