Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8622023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:57:15+00:00 2026-06-12T06:57:15+00:00

I have a lot of elements and mousemove method which, when selecting an item

  • 0

I have a lot of elements and mousemove method which, when selecting an item does redraw canvas.

function redraw(ctx) { // ctx - canvas context
    if (!needRedraw) 
        return;
    ctx.save();
    ctx.clearRect(0, 0, w, h);
    drawItems(ctx);
    ctx.restore();
}

function drawItems(ctx) {
    var l = nodes.length(); // array of elements for drawing (from 100 to 100000)
    for(var i = 0; i < l; i++) {
        var item = nodes[i];
        ctx.beginPath();
        ctx.strokeStyle = colors(item.type);
        ctx.fillStyle = fill(item);
        ctx.arc(item.x, item.y, item.r, 0, Math.PI * 2, true);
        ctx.fill();
        ctx.stroke();
        ctx.closePath();
    }
}

How to optimize this process, since it runs through all the elements of this highly expensive?
May be used async method, but i’m not understand how to apply him?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T06:57:17+00:00Added an answer on June 12, 2026 at 6:57 am

    Your calls to save, restore and closePath do nothing as they are written. Remove them.

    save and restore copy alllll of the canvas state and should be rarely if ever used. In a perfect performant world the only reason you’ll ever need to use them is to reset the clipping region.

    Draw one and only one path, and fill (and stroke) it only once, at the end.

    Like this:

    function redraw(ctx) { // ctx - canvas context
        if (!needRedraw) 
            return;
        ctx.clearRect(0, 0, w, h);
        drawItems(ctx);
    }
    
    function drawItems(ctx) {
        var l = nodes.length(); // array of elements for drawing (from 100 to 100000)
        ctx.beginPath(); //outside of loop!
        for(var i = 0; i < l; i++) {
            var item = nodes[i];
            ctx.moveTo(item.x+item.r,item.y); // set up subpath to be at the right point
            ctx.arc(item.x, item.y, item.r, 0, Math.PI * 2, true);
        }
        ctx.fill();     //outside of loop!
        ctx.stroke();   //outside of loop!
    }
    

    Depending on what you’re doing its very possible there are more optimizations to be had, but that should help!

    Hope your project goes well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have dropdown-Lists which have a lot of elements (>1000). When I select one
I have a form which contains a lot of elements, my DB guy has
At the moment, I have lot's of Java which does all kind of nifty
On my page I have a lot of various elements which respond to click
I have generated few elements in my page using following method. For example, $(#button+caption).click(function(){
I have a lot of UI elements placed inside a canvas. Some of them
In my application I have a lot of UI elements (particuarly buttons) where I
I have an outside div, inside this is a lot of P elements and
I have read in a lot of places to center elements (a div for
I have lot of code in my Tornado app which looks like this: @tornado.web.asynchronous

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.