Mouse Position Prediction Library – mpredict.js

Category: Javascript | June 10, 2016
Views Total:474 views
Official Page:Go to website
Last Update:June 10, 2016


Mouse Position Prediction Library – mpredict.js


mpredict.js is a no-dependency JavaScript library used to predict the mouse position.

See also:

How to use it:

Make sure the mpredict.js and mpredict-templates.json files are placed at the same folder. Then include the mpredict.js script on your webpage as follow:

<script src="mpredict.js"></script>

Start predicting mouse position:


Possible options with default values:

  // The time interval(by milliseconds) of sampling the mouse trace
  sampleInterval: 10,
  // he time threshold of pausing; when the time between two consecutive mouse events is larger than this threshold, the second mouse event will be regarded as the beginning of a new trace
  pauseThreshold: 50,
  // The number of nearest neighbors in the KNN algorithm
  K: 5,
  // The path to the JSON file of templates used for the KNN algorithm
  pathToTemplates: './mpredict-templates.json',
  // The selector of target element to record mouse trace from
  targetElement: 'document'


// Get the current mouse trace recorded and sampled by MPredict.js on the target DOM element
// Sample a mouse trace for prediction
// trace : Array: [[x0, y0, t0], ..., [xn, yn, tn]], xi and yi are mouse positions(in pixels), ti is the timestamp
// Predict the mouse position for the given trace after deltaTime
// trace : sampled mouse trace
// deltaTime : integer(in milliseconds), deltaTime <= 0 means predicting the endpoint for the given trace
mPredict.predictPosition(trace, deltaTime)

You Might Be Interested In:

Leave a Reply