js.foresight
Version:
Predicts mouse trajectory to trigger actions as users approach elements, enabling anticipatory UI updates or pre-loading. Made with vanilla javascript and usable in every framework.
19 lines • 1.3 kB
TypeScript
import type { MousePosition, Point } from "../../types/types";
/**
* Predicts the next mouse position based on a history of recent movements.
* It calculates velocity from the historical data and extrapolates a future point.
* The `history` array is mutated by this function: the new `currentPoint` is added,
* and if the history exceeds `positionHistorySize`, the oldest entry is removed.
*
* @param currentPoint - The current actual mouse coordinates.
* @param history - An array of previous mouse positions with timestamps.
* This array will be modified by this function.
* @param positionHistorySize - The maximum number of past positions to store and consider
* for the prediction.
* @param trajectoryPredictionTimeInMs - How far into the future (in milliseconds)
* to predict the mouse position.
* @returns The predicted {@link Point} (x, y coordinates). If history is insufficient
* (less than 2 points) or time delta is zero, returns the `currentPoint`.
*/
export declare function predictNextMousePosition(currentPoint: Point, history: MousePosition[], positionHistorySize: number, trajectoryPredictionTimeInMs: number): Point;
//# sourceMappingURL=predictNextMousePosition.d.ts.map