javascript - Is there any way to accelerate the mousemove event? -


I have written a short drawing script (canvas) for this website:

Click, each mousemove event basically executes the following:
- Get the coordinates.
- context.lineTo () between this point and the previous one
- context.stroke () the line

As You can see, if you move the cursor very quickly, then this event is probably not triggered (your CPU / Browser / etc), and a straight line is detected.

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 want to optimize it.

Therefore instead of a stroke () Every time a seasonal event begins, I put new coordinates inside an array line and regularly it will be accompanied by a timer Evacuate

In pseudocode:

  var CoordsQueue = []; Window.addEventListener ('mousemove', function (e) {coordsQueue.push ([e.pageX, e.pageY]);}, false); Function drop () {window.setTimeout (function () (vor coorus; while (cos = coinquyue.shift ()) {myContext.lineTo (Coord [0], coors [1]);} myContext.stroke (); Draw Loop ();}, 1000); // for test purposes}   

but this line did not improve, so I only wanted to draw a point on mousemove Try the same result: too much space between points.

From this I realized that the first code block is efficient enough, it is just mousemove Gradually triggering. / P>

Then, after spending some time in implementing useless optimization, it's your turn: do you want to speed up mousemove in dom scripting Is there a way?

Is this possible ???? Request?

If you are reporting Want to increase the frequency, I'm afraid that you are out of luck, only the operating system n report your status for every second T do, and I think that n is generally less than 100. (If someone can confirm it with real glasses, feel free to add them!)

To get a smooth line, you have to come up with some kind of interpolation plan on the subject There is a whole lot of literature; I recommend because it is local, easy to implement, and very stable (no overshoots).

Then, once you calculate the strip, you can approximate it with the line segment so that it looks smooth, or you can all go out and your own algorithm Can write.

If all this is worth a simple drawing application ... it's definitely for fixing.

Comments