Skip to content

Commit dca2719

Browse files
committed
(#425) Scale result of easing function by base speed to make its contribution noticeable
1 parent 7a00aa1 commit dca2719

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ describe("MovementType", () => {
6464
const mouseSpeed = 1000;
6565
const easingFunction: EasingFunction = (p: number) => {
6666
if (p < 0.5) {
67-
return -0.5 * mouseSpeed;
67+
return -0.5;
6868
}
6969
return 0;
7070
};

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

+6-3
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,22 @@ export interface EasingFunction {
77
(progressPercentage: number): number;
88
}
99

10-
export const calculateStepDuration = (speedInPixelsPerSecond: number) => (1 / speedInPixelsPerSecond) * 1_000_000_000;
10+
const nanoSecondsPerSecond = 1_000_000_000;
11+
12+
export const calculateStepDuration = (speedInPixelsPerSecond: number) => (1 / speedInPixelsPerSecond) * nanoSecondsPerSecond;
1113

1214
export const calculateMovementTimesteps = (
1315
amountOfSteps: number,
1416
speedInPixelsPerSecond: number,
1517
easingFunction: EasingFunction = linear
1618
): number[] => {
19+
const isEasingFunction = typeof easingFunction === "function";
1720
return Array(amountOfSteps)
1821
.fill(speedInPixelsPerSecond)
1922
.map((speed: number, idx: number) => {
2023
let speedInPixels = speed;
21-
if (typeof easingFunction === "function") {
22-
speedInPixels += easingFunction(idx / amountOfSteps);
24+
if (isEasingFunction) {
25+
speedInPixels += easingFunction(idx / amountOfSteps) * speedInPixels;
2326
}
2427
const stepDuration = calculateStepDuration(speedInPixels);
2528
return (isFinite(stepDuration) && stepDuration > 0) ? stepDuration : 0;

0 commit comments

Comments
 (0)