Skip to content

Commit 34629b0

Browse files
committed
(#130) Updated implementation
1 parent 6002651 commit 34629b0

7 files changed

+299
-640
lines changed

Diff for: index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export {Button} from "./lib/button.enum";
3131
export {centerOf, randomPointIn} from "./lib/location.function";
3232
export {LocationParameters} from "./lib/locationparameters.class";
3333
export {OptionalSearchParameters} from "./lib/optionalsearchparameters.class";
34-
export {linear} from "./lib/movementtype.function";
34+
export {EasingFunction, linear} from "./lib/mouse-movement.function";
3535
export {Point} from "./lib/point.class";
3636
export {Region} from "./lib/region.class";
3737
export {Window} from "./lib/window.class";

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

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

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

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
12+
export const calculateMovementTimesteps = (
13+
amountOfSteps: number,
14+
speedInPixelsPerSecond: number,
15+
easingFunction: EasingFunction = linear
16+
): number[] => {
17+
// Duration per movement step in nanoseconds
18+
let baseStepDuration = calculateBaseStepDuration(speedInPixelsPerSecond);
19+
baseStepDuration = (isFinite(baseStepDuration) && baseStepDuration > 0) ? baseStepDuration : 0;
20+
21+
return Array(amountOfSteps)
22+
.fill(baseStepDuration)
23+
.map((duration: number, idx: number) => {
24+
if (typeof easingFunction === "function") {
25+
const stepDuration = duration * (1 + easingFunction(idx / amountOfSteps));
26+
return stepDuration > 0 ? stepDuration : 0;
27+
}
28+
return duration;
29+
});
30+
};
31+
32+
export const linear: EasingFunction = (_: number): number => {
33+
return 0;
34+
};

0 commit comments

Comments
 (0)