Skip to content

Commit 3c0cbd4

Browse files
committed
add debug path to example
1 parent bea6899 commit 3c0cbd4

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

demo/example.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {CanvasKeyframe, canvasPath, wigglePreset} from "../public/animate";
2+
import { drawDebugClosed, drawPoint } from "./internal/canvas";
23
import {colors} from "./internal/layout";
34

45
// Fetch reference to example container.
@@ -29,6 +30,20 @@ const animation = canvasPath();
2930
const renderFrame = () => {
3031
ctx.clearRect(0, 0, size, size);
3132
ctx.fillStyle = colors.highlight;
33+
ctx.strokeStyle = colors.highlight;
34+
35+
// Debug
36+
if (false) {
37+
const p = (animation as any).renderPoints();
38+
39+
drawDebugClosed(ctx, p, 60);
40+
if (p.length) drawPoint(ctx, p[0], 400);
41+
42+
const fps = 5;
43+
setTimeout(() => requestAnimationFrame(renderFrame), 1000/fps);
44+
return;
45+
}
46+
3247
ctx.fill(animation.renderFrame());
3348
requestAnimationFrame(renderFrame);
3449
};

demo/internal/canvas.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,33 @@ export const drawClosed = (ctx: CanvasRenderingContext2D, points: Point[], handl
116116
});
117117
};
118118

119+
export const drawDebugClosed = (ctx: CanvasRenderingContext2D, points: Point[], size: number) => {
120+
forPoints(points, ({curr, next: nextFn}) => {
121+
const next = nextFn();
122+
const currHandle = expandHandle(curr, curr.handleOut);
123+
const nextHandle = expandHandle(next, next.handleIn);
124+
125+
drawLine(ctx, curr, currHandle, size);
126+
drawLine(ctx, next, nextHandle, size, 2);
127+
drawPoint(ctx, currHandle, size * 1.4);
128+
drawPoint(ctx, nextHandle, size * 1.4);
129+
const curve = new Path2D();
130+
curve.moveTo(curr.x, curr.y);
131+
curve.bezierCurveTo(
132+
currHandle.x,
133+
currHandle.y,
134+
nextHandle.x,
135+
nextHandle.y,
136+
next.x,
137+
next.y,
138+
);
139+
ctx.lineWidth = sizes().pt * size*2;
140+
ctx.stroke(curve);
141+
drawPoint(ctx, curr, size * 1.1);
142+
drawPoint(ctx, next, size * 1.1);
143+
});
144+
}
145+
119146
export const drawOpen = (
120147
ctx: CanvasRenderingContext2D,
121148
start: Point,

internal/animate/prepare.ts

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

15+
// Iterate through point ordering possibilities to find an option with the least
16+
// distance between points. Also reverse the list to try and optimize.
1517
const optimizeOrder = (a: Point[], b: Point[]): Point[] => {
1618
const count = a.length;
1719

@@ -39,6 +41,8 @@ const optimizeOrder = (a: Point[], b: Point[]): Point[] => {
3941
return shift(minOffset, minOffsetBase);
4042
};
4143

44+
// Modify the input shape to be the exact same path visually, but with
45+
// additional points so that the total number of points is "count".
4246
export const divide = (count: number, points: Point[]): Point[] => {
4347
if (points.length < 3) throw new Error("not enough points");
4448
if (count < points.length) throw new Error("cannot remove points");
@@ -93,6 +97,8 @@ const fixAnglesSelf = (points: Point[]): Point[] => {
9397
});
9498
};
9599

100+
// Split the input lengths into smaller segments to add the target amount of
101+
// lengths while minimizing the standard deviation of the list of lengths.
96102
const divideLengths = (lengths: number[], add: number): number[] => {
97103
const divisors = lengths.map(() => 1);
98104
const sizes = lengths.slice();

0 commit comments

Comments
 (0)