Skip to content

Commit 237aeee

Browse files
committed
(#130) Reworked mouse movement to use easing functions for timestep calculation
1 parent c131440 commit 237aeee

4 files changed

+83
-28
lines changed

Diff for: lib/mouse-movement.function.spec.ts

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import {calculateBaseStepDuration, calculateStepDuration, linear, calculateMovementTimesteps} from "./mouse-movement.function";
2+
3+
describe("MovementType", () => {
4+
describe("baseStepDuration", () => {
5+
it("should calculate the base step duration in nanoseconds", () => {
6+
// GIVEN
7+
const speedInPixelsPerSecond = 1000;
8+
const expectedBaseStepDuration = 1_000_000;
9+
10+
// WHEN
11+
const result = calculateBaseStepDuration(speedInPixelsPerSecond);
12+
13+
// THEN
14+
expect(result).toBe(expectedBaseStepDuration);
15+
});
16+
});
17+
18+
describe("stepDuration", () => {
19+
it("should call easing function with current progress to calculate current step duration", () => {
20+
// GIVEN
21+
const baseStepDuraton = 1_000_000;
22+
const currentProgress = 1.0;
23+
const easingFunction = jest.fn(() => 0);
24+
25+
// WHEN
26+
const result = calculateStepDuration(currentProgress, baseStepDuraton, easingFunction);
27+
28+
// THEN
29+
expect(result).toBe(baseStepDuraton);
30+
expect(easingFunction).toBeCalledTimes(1);
31+
expect(easingFunction).toBeCalledWith(currentProgress);
32+
})
33+
});
34+
35+
describe('linear', () => {
36+
it("should return a set of linear timesteps, 1000000 nanosecond per step.", () => {
37+
const expected = [1000000, 1000000, 1000000, 1000000, 1000000, 1000000];
38+
expect(calculateMovementTimesteps(6, 1000, linear)).toEqual(expected);
39+
});
40+
41+
it("should should return a set of linear timesteps, 2000000 nanoseconds per step.", () => {
42+
const expected = [2000000, 2000000, 2000000, 2000000, 2000000, 2000000];
43+
expect(calculateMovementTimesteps(6, 500, linear)).toEqual(expected);
44+
});
45+
});
46+
})
47+
;

Diff for: lib/mouse-movement.function.ts

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* {@link EasingFunction}s are used to modify movement behaviour.
3+
*
4+
* See https://easings.net/ for reference
5+
*/
6+
export interface EasingFunction {
7+
(progressPercentage: number): number;
8+
}
9+
10+
export const calculateBaseStepDuration = (speedInPixelsPerSecond: number) => (1 / speedInPixelsPerSecond) * 1_000_000_000;
11+
export const calculateStepDuration = (currentProgressPercentage: number, baseStepDuration: number, easingFunction: EasingFunction) => {
12+
let stepDuration = baseStepDuration;
13+
if (typeof easingFunction === "function") {
14+
stepDuration += baseStepDuration * easingFunction(currentProgressPercentage);
15+
}
16+
return stepDuration;
17+
}
18+
export const calculateMovementTimesteps = (
19+
amountOfSteps: number,
20+
speedInPixelsPerSecond: number,
21+
easingFunction: EasingFunction = linear
22+
): number[] => {
23+
const timeSteps = [];
24+
// Duration per movement step in nanoseconds
25+
let baseStepDuration = calculateBaseStepDuration(speedInPixelsPerSecond);
26+
baseStepDuration = (isFinite(baseStepDuration) && baseStepDuration > 0) ? baseStepDuration : 0;
27+
for (let idx = 0; idx < amountOfSteps; ++idx) {
28+
const stepDuration = calculateStepDuration(idx / amountOfSteps, baseStepDuration, easingFunction);
29+
timeSteps.push(stepDuration > 0 ? stepDuration : 0);
30+
}
31+
return timeSteps;
32+
};
33+
34+
export const linear: EasingFunction = (_: number): number => {
35+
return 0;
36+
};

Diff for: lib/movementtype.function.spec.ts

-13
This file was deleted.

Diff for: lib/movementtype.function.ts

-15
This file was deleted.

0 commit comments

Comments
 (0)