-
Notifications
You must be signed in to change notification settings - Fork 1
/
shapes.js
48 lines (45 loc) · 1.2 KB
/
shapes.js
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
43
44
45
46
47
48
import { TWO_PI } from "./constants";
export function circle(context, x, y, radius, options = {}) {
const { strokeStyle = "black", fillStyle = "hotpink" } = options;
context.save();
context.beginPath();
context.arc(x, y, radius, 0, TWO_PI, false);
if (strokeStyle) {
context.strokeStyle = strokeStyle;
context.stroke();
}
if (fillStyle) {
context.fillStyle = fillStyle;
context.fill();
}
context.closePath();
context.restore();
}
export function line(context, x1, y1, x2, y2, options = {}) {
const { strokeStyle = "black", lineWidth = 1 } = options;
context.save();
context.strokeStyle = strokeStyle;
context.lineWidth = lineWidth;
context.beginPath();
context.moveTo(x1, y1);
context.lineTo(x2, y2);
context.stroke();
context.closePath();
context.restore();
}
export function rectangle(context, x1, y1, x2, y2, options = {}) {
const { strokeStyle, fillStyle = "black" } = options;
context.save();
context.beginPath();
context.rect(x1, y1, x2, y2);
if (strokeStyle) {
context.strokeStyle = strokeStyle;
context.stroke();
}
if (fillStyle) {
context.fillStyle = fillStyle;
context.fill();
}
context.closePath();
context.restore();
}