Skip to content

Commit 101fabb

Browse files
committed
allow percentages outside regular range
1 parent 0996c34 commit 101fabb

File tree

6 files changed

+20
-16
lines changed

6 files changed

+20
-16
lines changed

internal/animate/ease.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
import {InterpolationFunc} from "./types";
22

3-
export const linear: InterpolationFunc = (progress: number) => {
4-
return progress;
3+
export const linear: InterpolationFunc = (percentage: number) => {
4+
return percentage;
55
};
66

7-
export const ease: InterpolationFunc = (progress: number) => {
8-
return 0.5 + 0.5 * Math.sin(Math.PI * (progress + 1.5));
7+
export const ease: InterpolationFunc = (percentage: number) => {
8+
return 0.5 + 0.5 * Math.sin(Math.PI * (percentage + 1.5));
99
};
1010

11-
export const easeStart: InterpolationFunc = (progress: number) => {
12-
return progress ** 2;
11+
export const easeStart: InterpolationFunc = (percentage: number) => {
12+
return percentage ** 2;
1313
};
1414

15-
export const easeEnd: InterpolationFunc = (progress: number) => {
16-
return 1 - (progress - 1) ** 2;
15+
export const easeEnd: InterpolationFunc = (percentage: number) => {
16+
return 1 - (percentage - 1) ** 2;
17+
};
18+
19+
export const easeOutElastic: InterpolationFunc = (percentage: number) => {
20+
const p = 0.1;
21+
return Math.pow(2, -10 * percentage) * Math.sin(((percentage - p / 4) * (2 * Math.PI)) / p) + 1;
1722
};

internal/animate/interpolate.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import {Shape} from "../types";
22
import {split, splitLine, mod} from "../util";
33

4-
// TODO allow percentage > 1
54
// OPT? loop
5+
// TODO smooth between
66

77
const interpolateAngle = (percentage: number, a: number, b: number): number => {
88
const tau = Math.PI * 2;
@@ -20,17 +20,18 @@ const interpolateAngle = (percentage: number, a: number, b: number): number => {
2020

2121
const interpolateBetween = (percentage: number, a: Shape, b: Shape): Shape => {
2222
if (a.length !== b.length) throw new Error("shapes have different number of points");
23+
const clamped = Math.min(1, Math.max(0, percentage));
2324
const shape: Shape = [];
2425
for (let i = 0; i < a.length; i++) {
2526
shape.push({
2627
...splitLine(percentage, a[i], b[i]),
2728
handleIn: {
2829
angle: interpolateAngle(percentage, a[i].handleIn.angle, b[i].handleIn.angle),
29-
length: split(percentage, a[i].handleIn.length, b[i].handleIn.length),
30+
length: split(clamped, a[i].handleIn.length, b[i].handleIn.length),
3031
},
3132
handleOut: {
3233
angle: interpolateAngle(percentage, a[i].handleOut.angle, b[i].handleOut.angle),
33-
length: split(percentage, a[i].handleOut.length, b[i].handleOut.length),
34+
length: split(clamped, a[i].handleOut.length, b[i].handleOut.length),
3435
},
3536
});
3637
}
@@ -41,6 +42,6 @@ export const interpolateBetweenLoop = (percentage: number, a: Shape, b: Shape):
4142
if (percentage < 0.5) {
4243
return interpolateBetween(2 * percentage, a, b);
4344
} else {
44-
return interpolateBetween(2 * percentage - 1, b, a);
45+
return interpolateBetween(-2 * percentage + 2, a, b);
4546
}
4647
};

internal/animate/testing/script.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ const testPrepShapesC = (percentage: number) => {
137137
point(0.45, 0.5, 0, 0, 0, 0),
138138
point(0.5, 0.5, 0, 0, 0, 0),
139139
];
140-
drawShape(ctx, debug, interpolateBetweenLoop(percentage, ...prepShapes(a, b)));
140+
drawShape(ctx, debug, interpolateBetweenLoop(percentage, ...prepShapes(b, a)));
141141
};
142142

143143
const testPrepShapesD = (percentage: number) => {

internal/animate/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export interface InterpolationFunc {
2-
(progress: number): number;
2+
(percentage: number): number;
33
}

internal/types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ export interface Handle {
1111
length: number;
1212
}
1313

14-
// TODO? use four coord instead of angles
1514
export interface Point extends Coord {
1615
// Cubic bezier handles.
1716
handleIn: Handle;

internal/util.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ const collapseHandle = (point: Coord, handle: Coord): Handle => ({
6161
length: Math.sqrt((handle.x - point.x) ** 2 + (handle.y - point.y) ** 2),
6262
});
6363

64-
// TODO make more precise
6564
export const length = (a: Point, b: Point): number => {
6665
const aHandle = expandHandle(a, a.handleOut);
6766
const bHandle = expandHandle(b, b.handleIn);

0 commit comments

Comments
 (0)