-
Notifications
You must be signed in to change notification settings - Fork 3
/
figures.ts
99 lines (93 loc) · 2.97 KB
/
figures.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import {getGlobalOptions} from './global_options.ts';
import {Path} from './path.ts';
import {Piece} from './pieces.ts';
import {Point, pointsToString} from './point.ts';
import {OrArrayRest, flattenPoints} from './util.ts';
import {PartialViewBox, viewBoxFromPartial} from './view_box.ts';
export function circle({
center = [0, 0],
radius = 1,
}: {
center?: Point,
radius?: number,
} = {}) {
return Piece.createElement({
tagName: "circle",
attributes: {cx: center[0], cy: center[1], r: radius},
});
}
export function ellipse({
center = [0, 0],
radiusX = 1,
radiusY = 1,
}: {
center?: Point,
radiusX?: number,
radiusY?: number,
} = {}) {
return Piece.createElement({
tagName: "ellipse",
attributes: {cx: center[0], cy: center[1], rx: radiusX, ry: radiusY},
});
}
export function rectangle({
cornerRadius = 0,
cornerRadiusX = cornerRadius,
cornerRadiusY = cornerRadius,
...viewBox
}: PartialViewBox & {
cornerRadius?: number,
cornerRadiusX?: number,
cornerRadiusY?: number,
} = {}) {
const {minX, minY, width, height} = viewBoxFromPartial(viewBox);
if (getGlobalOptions().quirks?.roundedCornersRectangleLimitedLightBurn)
if (cornerRadiusX !== cornerRadiusY ||
2 * cornerRadiusX > width || 2 * cornerRadiusY > height) {
const radiusX = Math.min(cornerRadiusX, width / 2);
const radiusY = Math.min(cornerRadiusY, height / 2);
const flatWid = width - 2 * radiusX;
const flatHei = height - 2 * radiusY;
const arc = {radiusX, radiusY, clockwise: true};
return Path.create([minX, minY + radiusY])
.relativeArc({...arc, target: [radiusX, -radiusY]})
.relativeHorizontal(flatWid)
.relativeArc({...arc, target: [radiusX, radiusY]})
.relativeVertical(flatHei)
.relativeArc({...arc, target: [-radiusX, radiusY]})
.relativeHorizontal(-flatWid)
.relativeArc({...arc, target: [-radiusX, -radiusY]})
.closePath();
}
return Piece.createElement({
tagName: "rect",
attributes: {
x: minX, y: minY, width, height,
...cornerRadiusX ? {rx: cornerRadiusX} : {},
...cornerRadiusY ? {ry: cornerRadiusY} : {},
},
});
}
export function line(originTo: Point): Piece;
export function line(from: Point, to: Point): Piece;
export function line(...params: [Point] | [Point, Point]) {
const [from, to] = params.length == 1 ? [[0, 0], params[0]] : params;
const line = Piece.createElement({
tagName: "line",
attributes: {x1: from[0], y1: from[1], x2: to[0], y2: to[1]},
});
return getGlobalOptions().quirks?.lineTransformBrokenLightBurn ?
line.wrapInG() : line;
}
export function polygon(...points: OrArrayRest<Point>) {
return Piece.createElement({
tagName: "polygon",
attributes: {points: pointsToString(flattenPoints(points))},
});
}
export function polyLine(...points: OrArrayRest<Point>) {
return Piece.createElement({
tagName: "polyline",
attributes: {points: pointsToString(flattenPoints(points))},
});
}