Note that there are some explanatory texts on larger screens.

plurals
  1. POIs there any way to accelerate the mousemove event?
    primarykey
    data
    text
    <p>I wrote a little drawing script (canvas) for this website: <a href="http://scri.ch/" rel="noreferrer">http://scri.ch/</a></p> <p>When you click on the document, every <code>mousemove</code> event basically executes the following:<br> - Get coordinates.<br> - <code>context.lineTo()</code> between this point and the previous one<br> - <code>context.stroke()</code> the line</p> <p>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.</p> <p>In pseudocode:</p> <pre><code>window.addEventListener('mousemove', function(e){ myContext.lineTo(e.pageX, e.pageY); myContext.stroke(); }, false); </code></pre> <p>This is a known problem, and the solution is fine, but I would like to optimize that.</p> <p>So instead of <code>stroke()</code> each time a mousemove event is triggered, I put the new coordinates inside an array queue, and regularly draw / empty it with a timer.</p> <p>In pseudocode:</p> <pre><code>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 } </code></pre> <p>But it did not improve the line. So I tried to only draw a point on <code>mousemove</code>. Same result: too much space between the points.</p> <p>It made me realize that the first code block is efficient enough, it is just the <code>mousemove</code> event that is triggering too slowly.</p> <p>So, after having myself spent some time to implement a useless optimization, it’s your turn: is there a way to optimize the <code>mousemove</code> triggering speed in DOM scripting?</p> <p>Is it possible to “request” the mouse position at any time?</p> <p>Thanks for your advices!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload