Skip to content

Commit 196f4f2

Browse files
committed
add initial design of animation api
1 parent 9d2b360 commit 196f4f2

File tree

3 files changed

+46
-7
lines changed

3 files changed

+46
-7
lines changed

internal/animate/prepare.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,22 @@ import {
1212
} from "../util";
1313
import {Point} from "../types";
1414

15-
// TODO OPT extract optimization logic
1615
const optimizeOrder = (a: Point[], b: Point[]): Point[] => {
1716
const count = a.length;
1817

19-
let minSum = Infinity;
18+
let minTotal = Infinity;
2019
let minOffset = 0;
2120
let minOffsetBase: Point[] = [];
2221

2322
const setMinOffset = (points: Point[]) => {
2423
for (let i = 0; i < count; i++) {
25-
let sum = 0;
24+
let total = 0;
2625
for (let j = 0; j < count; j++) {
27-
sum += (100 * distance(a[j], points[mod(j + i, count)])) ** 1 / 2;
28-
if (sum > minSum) break;
26+
total += (100 * distance(a[j], points[mod(j + i, count)])) ** 2;
27+
if (total > minTotal) break;
2928
}
30-
if (sum <= minSum) {
31-
minSum = sum;
29+
if (total <= minTotal) {
30+
minTotal = total;
3231
minOffset = i;
3332
minOffsetBase = points;
3433
}

internal/animate/testing/script.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,21 @@ const blob = (seed: string, count: number, scale: number, offset: Coord): Point[
233233
};
234234

235235
const loopBetween = (percentage: number, a: Point[], b: Point[]): Point[] => {
236+
// Draw before/after shapes + point path.
237+
ctx.save();
238+
ctx.strokeStyle = "#ffaaaa";
239+
drawClosed(ctx, false, a);
240+
ctx.strokeStyle = "#aaaaff";
241+
drawClosed(ctx, false, b);
242+
ctx.strokeStyle = "#33ff33";
243+
for (let i = 0; i < a.length; i++) {
244+
ctx.beginPath();
245+
ctx.moveTo(a[i].x, a[i].y);
246+
ctx.lineTo(b[i].x, b[i].y);
247+
ctx.stroke();
248+
}
249+
ctx.restore();
250+
236251
if (percentage < 0.5) {
237252
return interpolateBetweenSmooth(1, 2 * percentage, a, b);
238253
} else {

public/animate.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
// API
3+
interface Keyframe {
4+
duration: number;
5+
easing: "ease" | "linear" | "bounce"; // ...
6+
blobOptions: BlobOptions;
7+
}
8+
interface CanvasKeyframe extends Keyframe {
9+
canvasOptions: CanvasOptions;
10+
}
11+
interface Animation<K extends Keyframe, T = void> {
12+
start(blobOptions: BlobOptions, otherOptions: T) => Animation<T>;
13+
// TODO how to handle interrupts
14+
// TODO remove keyframe type
15+
wait(delay: number) => Promise<Animation<T>>;
16+
next(keyframe: K) => Promise<Animation<T>>;
17+
}
18+
export const canvasPath(callback: (path: Path2D) => void) => Animation<CanvasKeyframe, CanvasOptions>;
19+
20+
// example
21+
const animation = animate.canvasPath(callback);
22+
animation.start(blobOptions, canvasOptions);
23+
animation.next(keyframe);
24+
25+
*/

0 commit comments

Comments
 (0)