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

  • Home
  • SEARCH
  • 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 4063612
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:44:55+00:00 2026-05-20T15:44:55+00:00

I wrote a little drawing script (canvas) for this website: http://scri.ch/ When you click

  • 0

I wrote a little drawing script (canvas) for this website: http://scri.ch/

When you click on the document, every mousemove event basically executes the following:
– Get coordinates.
– context.lineTo() between this point and the previous one
– context.stroke() the line

As you can see, if you move the cursor very fast, the event isn’t triggering enough (depending on your CPU / Browser / etc.), and a straight line is traced.

In pseudocode:

window.addEventListener('mousemove', function(e){
  myContext.lineTo(e.pageX, e.pageY);
  myContext.stroke();
}, false);

This is a known problem, and the solution is fine, but I would like to optimize that.

So instead of stroke() each time a mousemove event is triggered, I put the new coordinates inside an array queue, and regularly draw / empty it with a timer.

In pseudocode:

var coordsQueue = [];

window.addEventListener('mousemove', function(e){
  coordsQueue.push([e.pageX, e.pageY]);
}, false);

function drawLoop(){
  window.setTimeout(function(){
    var coords;
    while (coords = coordsQueue.shift()) {
      myContext.lineTo(coords[0], coords[1]);
    }
    myContext.stroke();
    drawLoop();
  }, 1000); // For testing purposes
}

But it did not improve the line. So I tried to only draw a point on mousemove. Same result: too much space between the points.

It made me realize that the first code block is efficient enough, it is just the mousemove event that is triggering too slowly.

So, after having myself spent some time to implement a useless optimization, it’s your turn: is there a way to optimize the mousemove triggering speed in DOM scripting?

Is it possible to “request” the mouse position at any time?

Thanks for your advices!

  • 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-05-20T15:44:55+00:00Added an answer on May 20, 2026 at 3:44 pm

    If you want to increase the reporting frequency, I’m afraid you’re out of luck. Mice only report their position to the operating system n times per second, and I think n is usually less than 100. (If anyone can confirm this with actual specs, feel free to add them!)

    So in order to get a smooth line, you’ll have to come up with some sort of interpolation scheme. There’s a whole lot of literature on the topic; I recommend monotone cubic interpolation because it’s local, simple to implement, and very stable (no overshoot).

    Then, once you’ve computed the spline, you can approximate it with line segments short enough so that it looks smooth, or you can go all-out and write your own Bresenham algorithm to draw it.

    If all this is worth it for a simple drawing application… that’s for you to decide, of course.

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

Sidebar

Related Questions

First a little intro: Last year i wrote this http://dragan.yourtree.org/code/canvas-3d-graph/ Now, i want to
I wrote this little test script in Ruby (on MacOS): #!/usr/bin/ruby require 'digest/sha2' def
So I just wrote this little script. The problem is I want the newest
I wrote a little Python script which uses an Adder plugin to mix two
I wrote a little script that exports certain Excel cell values into Word. However,
I wrote a little PHP script below to demonstrate my question. Run the code
I wrote this little snippet that should format money but its failing on the
So I wrote a little multithreaded SMTP program. The problem is every time I
i've wrote a little database class which goes like this: class dbClass extends mysqli
Some time ago I wrote a little piece of code to ask about on

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.