top | item 5429764

Mouse path smoothing

95 points| harrison_clarke | 13 years ago |twistedoakstudios.com | reply

25 comments

order
[+] eduardordm|13 years ago|reply
Why don't you just find two mid-points based on the last and current positions and use bezier? It should be absurdly faster.

Example: x = (p1.x + p2.x) * 0.5; y = (p1.y + p2.y) * 0.5;

Implementation (easily portable to html5):

https://github.com/eduardordm/wireframeapp/blob/master/wiref...

[+] Skroob|13 years ago|reply
Be warned, that code is GPLv3. I don't know the specific legal issues with using it or porting it to another language, but if you're going to use it you should check.
[+] nitrogen|13 years ago|reply
Judging from the demos, it feels like this code is treating each point with an equal weight, regardless of velocity. My mouse is configured to send 500 events per second, so the smoothing doesn't smooth all that much.

Depending on the latency limitations of the application in question, it seems like it would be better to use a curve simplifying algorithm similar to the one used by Inkscape, or maybe just dividing the curve into equally spaced points on the plane (instead of equally spaced points in time), taking care to preserve obvious changes in direction (perhaps by adding a point anywhere the locally smoothed first or second derivative crosses zero).

[+] blaze33|13 years ago|reply
Just tested it with my mouse & graphics tablet. The unsmoothed version clearly works better for the stylus vs. mouse. What's interesting is the smoothed version ~equally improving both input methods.

Nice work !

[+] tlarkworthy|13 years ago|reply
Did u try a kalman filter? That would be my first choice. Observed path = desired path plus Gaussian noise in position and velocity domains.
[+] kghose|13 years ago|reply
That's an interesting manner of averaging. The end result is an exponential filter, but he displays the results online. It is fun to draw lines with high frequency components (wiggles) and watch them straighten out over half a second or so. It is aesthetically quite pleasing!
[+] yeureka|13 years ago|reply
Interesting.

I don't know signal processing theory, but I derived the same formula years ago to filter high frequency changes in motion.

Now I know what to call it: an exponential moving average.