-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphics.ts
42 lines (36 loc) · 928 Bytes
/
graphics.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
export type Circle = {
radius: number;
x: number;
y: number;
}
export type Image = Circle[];
export function circle(radius: number, x: number, y: number): Image {
return [{ radius, x, y }];
}
export function stack(...images: Image[]): Image {
return images.reduce((imgs, img) => imgs.concat(img), []);
}
export function translate(dx: number, dy: number, image: Image): Image {
return image.map(({radius, x, y}) => ({radius, x: x + dx, y: y + dy}));
}
function renderCircle(
ctx: CanvasRenderingContext2D, width: number, height: number, {radius, x, y}: Circle
): void {
ctx.beginPath();
ctx.arc(
(width / 2) + x,
(height / 2) - y,
radius,
0,
2 * Math.PI,
false
);
ctx.fill();
}
export function render(
ctx: CanvasRenderingContext2D, width: number, height: number, circles: Image
): void {
for (const circle of circles) {
renderCircle(ctx, width, height, circle);
}
}