Skip to content

Commit a36f02d

Browse files
committed
pass context to drawing code
1 parent c31bf5b commit a36f02d

File tree

3 files changed

+74
-87
lines changed

3 files changed

+74
-87
lines changed

animate/canvas/draw.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import {Coord, Point} from "../types";
2+
import {expandHandle} from "../util";
3+
4+
const pointSize = 2;
5+
const infoSpacing = 20;
6+
7+
export function clear(ctx: CanvasRenderingContext2D) {
8+
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
9+
}
10+
11+
export function drawInfo(ctx: CanvasRenderingContext2D, pos: number, label: string, value: any) {
12+
ctx.fillText(`${label}: ${value}`, infoSpacing, (pos + 1) * infoSpacing);
13+
}
14+
15+
function drawLine(ctx: CanvasRenderingContext2D, a: Coord, b: Coord, style: string) {
16+
const backupStrokeStyle = ctx.strokeStyle;
17+
ctx.beginPath();
18+
ctx.moveTo(a.x, a.y);
19+
ctx.lineTo(b.x, b.y);
20+
ctx.strokeStyle = style;
21+
ctx.stroke();
22+
ctx.strokeStyle = backupStrokeStyle;
23+
}
24+
25+
function drawPoint(ctx: CanvasRenderingContext2D, p: Coord, style: string) {
26+
const backupFillStyle = ctx.fillStyle;
27+
ctx.beginPath();
28+
ctx.arc(p.x, p.y, pointSize, 0, 2 * Math.PI);
29+
ctx.fillStyle = style;
30+
ctx.fill();
31+
ctx.fillStyle = backupFillStyle;
32+
}
33+
34+
export function drawShape(ctx: CanvasRenderingContext2D, debug: boolean, points: Point[]) {
35+
if (points.length < 2) throw new Error("not enough points");
36+
37+
for (let i = 0; i < points.length; i++) {
38+
// Compute coordinates of handles.
39+
const curr = points[i];
40+
const next = points[(i + 1) % points.length];
41+
const currHandle = expandHandle(curr, curr.handleOut);
42+
const nextHandle = expandHandle(next, next.handleIn);
43+
44+
if (debug) {
45+
drawPoint(ctx, curr, "");
46+
drawLine(ctx, curr, currHandle, "#ccc");
47+
drawLine(ctx, next, nextHandle, "#b6b");
48+
}
49+
50+
// Draw curve between curr and next points.
51+
ctx.beginPath();
52+
ctx.moveTo(curr.x, curr.y);
53+
ctx.bezierCurveTo(currHandle.x, currHandle.y, nextHandle.x, nextHandle.y, next.x, next.y);
54+
ctx.stroke();
55+
}
56+
}

animate/draw.ts

Lines changed: 0 additions & 78 deletions
This file was deleted.

animate/test.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import blobs from "..";
22

3-
import {canvasClear, canvasSize, drawInfo, drawShape} from "./draw";
3+
import {clear, drawInfo, drawShape} from "./canvas/draw";
44
import {interpolateBetweenLoop} from "./interpolate";
55
import {approxCurveLength, divideShape, prepShapes, splitCurveAt, splitCurveBy} from "./prep";
66
import {Coord, Point} from "./types";
@@ -10,6 +10,14 @@ const animationStart = 0.3;
1010
const debug = true;
1111
const size = 1000;
1212

13+
const canvas = document.createElement("canvas");
14+
document.body.appendChild(canvas);
15+
canvas.height = size;
16+
canvas.width = size;
17+
const temp = canvas.getContext("2d");
18+
if (temp === null) throw new Error("context is null");
19+
const ctx = temp;
20+
1321
const rad = (deg: number) => {
1422
return (deg / 360) * 2 * Math.PI;
1523
};
@@ -46,15 +54,16 @@ const testSplitAt = (percentage: number) => {
4654
const next = points[(i + 1) % points.length];
4755
length += approxCurveLength(curr, next);
4856
}
49-
drawInfo("split at lengths sum", length);
57+
drawInfo(ctx, 1, "split at lengths sum", length);
5058

51-
drawShape(debug, points);
59+
drawShape(ctx, debug, points);
5260
};
5361

5462
const testSplitBy = () => {
5563
const count = 10;
5664
for (let i = 0; i < count; i++) {
5765
drawShape(
66+
ctx,
5867
debug,
5968
splitCurveBy(
6069
i + 1,
@@ -69,6 +78,7 @@ const testDivideShape = () => {
6978
const count = 10;
7079
for (let i = 0; i < count; i++) {
7180
drawShape(
81+
ctx,
7282
debug,
7383
divideShape(i + 3, [
7484
point(0.3, 0.2 + i * 0.05, -10, 0.04, -45, 0.02),
@@ -92,13 +102,13 @@ const testInterpolateBetween = (percentage: number) => {
92102
point(0.35, 0.82, 360 * 10, 0, 180, 0),
93103
point(0.3, 0.77, 90, 0, -90, 0),
94104
];
95-
drawShape(debug, interpolateBetweenLoop(percentage, a, b));
105+
drawShape(ctx, debug, interpolateBetweenLoop(percentage, a, b));
96106
};
97107

98108
const testPrepShapesA = (percentage: number) => {
99109
const a = genBlob("a", 0.6, 0.6, 0.3, {x: 0.5, y: 0.2});
100110
const b = genBlob("b", 1, 0.6, 0.3, {x: 0.5, y: 0.2});
101-
drawShape(debug, interpolateBetweenLoop(percentage, ...prepShapes(a, b)));
111+
drawShape(ctx, debug, interpolateBetweenLoop(percentage, ...prepShapes(a, b)));
102112
};
103113

104114
const testPrepShapesB = (percentage: number) => {
@@ -109,7 +119,7 @@ const testPrepShapesB = (percentage: number) => {
109119
point(0.75, 0.7, 0, 0, 0, 0),
110120
point(0.55, 0.7, 0, 0, 0, 0),
111121
];
112-
drawShape(debug, interpolateBetweenLoop(percentage, ...prepShapes(a, b)));
122+
drawShape(ctx, debug, interpolateBetweenLoop(percentage, ...prepShapes(a, b)));
113123
};
114124

115125
const genBlob = (
@@ -147,10 +157,9 @@ const genBlob = (
147157
let percentage = animationStart;
148158

149159
const renderFrame = () => {
150-
canvasClear();
151-
canvasSize(size, size);
160+
clear(ctx);
152161

153-
drawInfo("percentage", percentage);
162+
drawInfo(ctx, 0, "percentage", percentage);
154163
testSplitAt(percentage);
155164
testSplitBy();
156165
testDivideShape();

0 commit comments

Comments
 (0)