DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. JavaScript
  4. Livecoding: Reaching the Limits of Canvas Redraw Speed

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.

Swizec Teller user avatar by
Swizec Teller
·
May. 18, 17 · Tutorial
Like (1)
Save
Tweet
Share
3.31K Views

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

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

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 .

Frame (networking) IT React (JavaScript library) WebGL Strings Overhead (computing) Abstraction (computer science) Bitmap

Published at DZone with permission of Swizec Teller, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How to Develop a Portrait Retouching Function
  • Why Does DevOps Recommend Shift-Left Testing Principles?
  • A Guide To Successful DevOps in Web3
  • Type Variance in Java and Kotlin

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: