Skip to content

Commit 58bf1eb

Browse files
committed
add curve splitting animation
1 parent 932953b commit 58bf1eb

File tree

2 files changed

+98
-43
lines changed

2 files changed

+98
-43
lines changed

demo/content.ts

Lines changed: 97 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -131,59 +131,84 @@ addCanvas(2, (ctx, width, height, animate) => {
131131
These handles can be thought of as defining the direction and momentum of the line.`;
132132
});
133133

134+
const drawDetails = (
135+
ctx: CanvasRenderingContext2D,
136+
percentage: number,
137+
a: Point,
138+
b: Point,
139+
styles: {
140+
details: () => void;
141+
handles: () => void;
142+
line?: () => void;
143+
point: () => void;
144+
},
145+
) => {
146+
const a0: Coord = a;
147+
const a1 = expandHandle(a, a.handleOut);
148+
const a2 = expandHandle(b, b.handleIn);
149+
const a3: Coord = b;
150+
151+
const b0 = splitLine(percentage, a0, a1);
152+
const b1 = splitLine(percentage, a1, a2);
153+
const b2 = splitLine(percentage, a2, a3);
154+
const c0 = splitLine(percentage, b0, b1);
155+
const c1 = splitLine(percentage, b1, b2);
156+
const d0 = splitLine(percentage, c0, c1);
157+
158+
tempStyles(ctx, styles.details, () => {
159+
drawLine(ctx, a0, a1, 1);
160+
drawLine(ctx, a1, a2, 1);
161+
drawLine(ctx, a2, a3, 1);
162+
drawLine(ctx, b0, b1, 1);
163+
drawLine(ctx, b1, b2, 1);
164+
165+
drawPoint(ctx, a0, 1.3, "a0");
166+
drawPoint(ctx, a1, 1.3, "a1");
167+
drawPoint(ctx, a2, 1.3, "a2");
168+
drawPoint(ctx, a3, 1.3, "a3");
169+
drawPoint(ctx, b0, 1.3, "b0");
170+
drawPoint(ctx, b1, 1.3, "b1");
171+
drawPoint(ctx, b2, 1.3, "b2");
172+
});
173+
174+
if (styles.line !== undefined) {
175+
forceStyles(ctx, () => {
176+
styles.line!();
177+
drawOpen(ctx, a, b, false)
178+
});
179+
} else {
180+
drawOpen(ctx, a, b, false)
181+
}
182+
183+
tempStyles(ctx, styles.handles, () => {
184+
drawLine(ctx, c0, c1, 1);
185+
drawPoint(ctx, c0, 1.3, "c0");
186+
drawPoint(ctx, c1, 1.3, "c1");
187+
});
188+
189+
tempStyles(ctx, styles.point, () => {
190+
drawPoint(ctx, d0, 3);
191+
});
192+
};
193+
134194
addCanvas(2, (ctx, width, height, animate) => {
135195
const period = Math.PI * Math.E * 1000;
136196
const start = point(width * 0.3, height * 0.8, 0, 0, -105, width * 0.32);
137197
const end = point(width * 0.7, height * 0.8, -75, width * 0.25, 0, 0);
138198

139-
const a0: Coord = start;
140-
const a1 = expandHandle(start, start.handleOut);
141-
const a2 = expandHandle(end, end.handleIn);
142-
const a3: Coord = end;
143-
144199
animate((frameTime) => {
145200
const percentage = calcBouncePercentage(period, timingFunctions.ease, frameTime);
146-
147-
const b0 = splitLine(percentage, a0, a1);
148-
const b1 = splitLine(percentage, a1, a2);
149-
const b2 = splitLine(percentage, a2, a3);
150-
const c0 = splitLine(percentage, b0, b1);
151-
const c1 = splitLine(percentage, b1, b2);
152-
const d0 = splitLine(percentage, c0, c1);
153-
154-
tempStyles(
155-
ctx,
156-
() => {
201+
drawDetails(ctx, percentage, start, end, {
202+
details: () => {
157203
ctx.fillStyle = colors.secondary;
158204
ctx.strokeStyle = colors.secondary;
159205
},
160-
() => {
161-
drawLine(ctx, a0, a1, 1);
162-
drawLine(ctx, a1, a2, 1);
163-
drawLine(ctx, a2, a3, 1);
164-
drawLine(ctx, b0, b1, 1);
165-
drawLine(ctx, b1, b2, 1);
166-
drawLine(ctx, c0, c1, 1);
167-
168-
drawPoint(ctx, a0, 1.3, "a0");
169-
drawPoint(ctx, a1, 1.3, "a1");
170-
drawPoint(ctx, a2, 1.3, "a2");
171-
drawPoint(ctx, a3, 1.3, "a3");
172-
drawPoint(ctx, b0, 1.3, "b0");
173-
drawPoint(ctx, b1, 1.3, "b1");
174-
drawPoint(ctx, b2, 1.3, "b2");
175-
drawPoint(ctx, c0, 1.3, "c0");
176-
drawPoint(ctx, c1, 1.3, "c1");
206+
handles: () => {
207+
ctx.fillStyle = colors.secondary;
208+
ctx.strokeStyle = colors.secondary;
177209
},
178-
);
179-
180-
drawOpen(ctx, start, end, false);
181-
182-
tempStyles(
183-
ctx,
184-
() => (ctx.fillStyle = colors.highlight),
185-
() => drawPoint(ctx, d0, 3),
186-
);
210+
point: () => (ctx.fillStyle = colors.highlight),
211+
});
187212
});
188213

189214
return `The curve can be drawn geometrically by recursively splitting points by a percentage
@@ -509,3 +534,33 @@ addCanvas(
509534
possible shifts.`;
510535
},
511536
);
537+
538+
addCanvas(
539+
1.3,
540+
(ctx, width, height, animate) => {
541+
const period = Math.PI ** Math.E * 1000;
542+
const start = point(width * 0.1, height * 0.6, 0, 0, -45, width * 0.5);
543+
const end = point(width * 0.9, height * 0.6, 160, width * 0.3, 0, 0);
544+
545+
animate((frameTime) => {
546+
const percentage = calcBouncePercentage(period, timingFunctions.ease, frameTime);
547+
drawDetails(ctx, percentage, start, end, {
548+
details: () => {
549+
ctx.fillStyle = colors.secondary;
550+
ctx.strokeStyle = colors.secondary;
551+
},
552+
handles: () => {
553+
ctx.fillStyle = colors.highlight;
554+
ctx.strokeStyle = colors.highlight;
555+
},
556+
line: () => {
557+
ctx.fillStyle = colors.secondary;
558+
ctx.strokeStyle = colors.secondary;
559+
ctx.lineWidth = sizes().pt;
560+
},
561+
point: () => (ctx.fillStyle = colors.highlight),
562+
});
563+
});
564+
},
565+
() => {},
566+
);

demo/internal/debug.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// If debug is initially set to false it will not be toggleable.
2-
export let debug = false;
2+
export let debug = true;
33

44
const debugListeners: ((debug: boolean) => void)[] = [];
55
export const onDebugStateChange = (fn: (debug: boolean) => void) => {

0 commit comments

Comments
 (0)