Skip to content

Commit 313d676

Browse files
committed
extract detail logic to more generic helper
1 parent 58bf1eb commit 313d676

File tree

1 file changed

+92
-76
lines changed

1 file changed

+92
-76
lines changed

demo/content.ts

Lines changed: 92 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,22 @@ const centeredBlob = (options: BlobOptions, center: Coord): Point[] => {
4646
});
4747
};
4848

49+
const calcFullDetails = (percentage: number, a: Point, b: Point) => {
50+
const a0: Coord = a;
51+
const a1 = expandHandle(a, a.handleOut);
52+
const a2 = expandHandle(b, b.handleIn);
53+
const a3: Coord = b;
54+
55+
const b0 = splitLine(percentage, a0, a1);
56+
const b1 = splitLine(percentage, a1, a2);
57+
const b2 = splitLine(percentage, a2, a3);
58+
const c0 = splitLine(percentage, b0, b1);
59+
const c1 = splitLine(percentage, b1, b2);
60+
const d0 = splitLine(percentage, c0, c1);
61+
62+
return {a0, a1, a2, a3, b0, b1, b2, c0, c1, d0};
63+
};
64+
4965
addTitle(4, "What are vector graphics?");
5066

5167
addCanvas(
@@ -131,84 +147,48 @@ addCanvas(2, (ctx, width, height, animate) => {
131147
These handles can be thought of as defining the direction and momentum of the line.`;
132148
});
133149

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-
194150
addCanvas(2, (ctx, width, height, animate) => {
195151
const period = Math.PI * Math.E * 1000;
196152
const start = point(width * 0.3, height * 0.8, 0, 0, -105, width * 0.32);
197153
const end = point(width * 0.7, height * 0.8, -75, width * 0.25, 0, 0);
198154

199155
animate((frameTime) => {
200156
const percentage = calcBouncePercentage(period, timingFunctions.ease, frameTime);
201-
drawDetails(ctx, percentage, start, end, {
202-
details: () => {
157+
const d = calcFullDetails(percentage, start, end);
158+
159+
tempStyles(
160+
ctx,
161+
() => {
203162
ctx.fillStyle = colors.secondary;
204163
ctx.strokeStyle = colors.secondary;
205164
},
206-
handles: () => {
207-
ctx.fillStyle = colors.secondary;
208-
ctx.strokeStyle = colors.secondary;
165+
() => {
166+
drawLine(ctx, d.a0, d.a1, 1);
167+
drawLine(ctx, d.a1, d.a2, 1);
168+
drawLine(ctx, d.a2, d.a3, 1);
169+
drawLine(ctx, d.b0, d.b1, 1);
170+
drawLine(ctx, d.b1, d.b2, 1);
171+
drawLine(ctx, d.c0, d.c1, 1);
172+
173+
drawPoint(ctx, d.a0, 1.3, "a0");
174+
drawPoint(ctx, d.a1, 1.3, "a1");
175+
drawPoint(ctx, d.a2, 1.3, "a2");
176+
drawPoint(ctx, d.a3, 1.3, "a3");
177+
drawPoint(ctx, d.b0, 1.3, "b0");
178+
drawPoint(ctx, d.b1, 1.3, "b1");
179+
drawPoint(ctx, d.b2, 1.3, "b2");
180+
drawPoint(ctx, d.c0, 1.3, "c0");
181+
drawPoint(ctx, d.c1, 1.3, "c1");
209182
},
210-
point: () => (ctx.fillStyle = colors.highlight),
211-
});
183+
);
184+
185+
tempStyles(
186+
ctx,
187+
() => (ctx.fillStyle = colors.highlight),
188+
() => drawPoint(ctx, d.d0, 3),
189+
);
190+
191+
drawOpen(ctx, start, end, false);
212192
});
213193

214194
return `The curve can be drawn geometrically by recursively splitting points by a percentage
@@ -544,22 +524,58 @@ addCanvas(
544524

545525
animate((frameTime) => {
546526
const percentage = calcBouncePercentage(period, timingFunctions.ease, frameTime);
547-
drawDetails(ctx, percentage, start, end, {
548-
details: () => {
527+
const d = calcFullDetails(percentage, start, end);
528+
529+
tempStyles(
530+
ctx,
531+
() => {
549532
ctx.fillStyle = colors.secondary;
550533
ctx.strokeStyle = colors.secondary;
551534
},
552-
handles: () => {
535+
() => {
536+
drawLine(ctx, d.a0, d.a1, 1);
537+
drawLine(ctx, d.a1, d.a2, 1);
538+
drawLine(ctx, d.a2, d.a3, 1);
539+
drawLine(ctx, d.b0, d.b1, 1);
540+
drawLine(ctx, d.b1, d.b2, 1);
541+
542+
drawPoint(ctx, d.a0, 1.3);
543+
drawPoint(ctx, d.a1, 1.3);
544+
drawPoint(ctx, d.a2, 1.3);
545+
drawPoint(ctx, d.a3, 1.3);
546+
drawPoint(ctx, d.b0, 1.3);
547+
drawPoint(ctx, d.b1, 1.3);
548+
drawPoint(ctx, d.b2, 1.3);
549+
},
550+
);
551+
552+
forceStyles(ctx, () => {
553+
const {pt} = sizes();
554+
ctx.fillStyle = colors.secondary;
555+
ctx.strokeStyle = colors.secondary;
556+
ctx.lineWidth = pt;
557+
558+
drawOpen(ctx, start, end, false);
559+
});
560+
561+
tempStyles(
562+
ctx,
563+
() => {
553564
ctx.fillStyle = colors.highlight;
554565
ctx.strokeStyle = colors.highlight;
555566
},
556-
line: () => {
557-
ctx.fillStyle = colors.secondary;
558-
ctx.strokeStyle = colors.secondary;
559-
ctx.lineWidth = sizes().pt;
567+
() => {
568+
drawLine(ctx, d.c0, d.c1, 1);
569+
drawPoint(ctx, d.c0, 1.3);
570+
drawPoint(ctx, d.c1, 1.3);
560571
},
561-
point: () => (ctx.fillStyle = colors.highlight),
562-
});
572+
);
573+
574+
tempStyles(
575+
ctx,
576+
() => (ctx.fillStyle = colors.highlight),
577+
() => drawPoint(ctx, d.d0, 2),
578+
);
563579
});
564580
},
565581
() => {},

0 commit comments

Comments
 (0)