@chocbite/ts-lib-svg 1.0.1 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{index.d.ts → index.d.mts} +97 -69
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +378 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +20 -16
- package/dist/index.js +0 -312
|
@@ -1,28 +1,44 @@
|
|
|
1
|
+
//#region src/attributes.d.ts
|
|
1
2
|
declare class SVGAttributes<T extends SVGElement> {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
3
|
+
#private;
|
|
4
|
+
constructor(elem: T);
|
|
5
|
+
/**Returns the svg element */
|
|
6
|
+
get elem(): T;
|
|
7
|
+
/** Sets the stroke color of the SVG element */
|
|
8
|
+
stroke(stroke: string): this;
|
|
9
|
+
/** Sets the stroke color of the SVG element */
|
|
10
|
+
s(stroke: string): this;
|
|
11
|
+
/** Sets the fill color of the SVG element */
|
|
12
|
+
fill(fill: string): this;
|
|
13
|
+
/** Sets the fill color of the SVG element */
|
|
14
|
+
f(fill: string): this;
|
|
15
|
+
/** Sets the stroke width of the SVG element */
|
|
16
|
+
stroke_width(width: number): this;
|
|
17
|
+
/** Sets the stroke width of the SVG element */
|
|
18
|
+
sw(width: number): this;
|
|
19
|
+
/** Adds one or more class names to the SVG element */
|
|
20
|
+
class_list(...class_name: string[]): this;
|
|
21
|
+
/** Adds one or more class names to the SVG element */
|
|
22
|
+
cl(...class_name: string[]): this;
|
|
23
|
+
/** Sets an arbitrary attribute on the SVG element */
|
|
24
|
+
attribute(name: string, value: string): this;
|
|
25
|
+
/** Sets an arbitrary attribute on the SVG element */
|
|
26
|
+
a(name: string, value: string): this;
|
|
27
|
+
/** Applies a translation transform to the SVG element */
|
|
28
|
+
translate(x: number, y: number): this;
|
|
29
|
+
/** Applies a rotation transform to the SVG element */
|
|
30
|
+
rotate(angle: number, cx?: number, cy?: number): this;
|
|
31
|
+
/** Applies a scaling transform to the SVG element */
|
|
32
|
+
scale(sx: number, sy?: number): this;
|
|
33
|
+
/** Applies a skewX transform to the SVG element */
|
|
34
|
+
skewX(angle: number): this;
|
|
35
|
+
/** Applies a skewY transform to the SVG element */
|
|
36
|
+
skewY(angle: number): this;
|
|
37
|
+
/** Applies a custom transform to the SVG element */
|
|
38
|
+
transform(transform: string): this;
|
|
24
39
|
}
|
|
25
|
-
|
|
40
|
+
//#endregion
|
|
41
|
+
//#region src/primitives/circle.d.ts
|
|
26
42
|
/**This creates a svg circle
|
|
27
43
|
* @param center_x x coordinate of center
|
|
28
44
|
* @param center_y y coordinate of center
|
|
@@ -35,7 +51,8 @@ declare function circle(center_x: number, center_y: number, radius: number): SVG
|
|
|
35
51
|
* @param start_angle start angle in radians
|
|
36
52
|
* @param end_angle end anglein radians*/
|
|
37
53
|
declare function circle_arc(center_x: number, center_y: number, radius: number, start_angle: number, end_angle: number): SVGAttributes<SVGPathElement>;
|
|
38
|
-
|
|
54
|
+
//#endregion
|
|
55
|
+
//#region src/primitives/ellipse.d.ts
|
|
39
56
|
/**This creates a svg ellipse
|
|
40
57
|
* @param center_x x coordinate of center
|
|
41
58
|
* @param center_y y coordinate of center
|
|
@@ -50,20 +67,24 @@ declare function ellipse(center_x: number, center_y: number, radius_x: number, r
|
|
|
50
67
|
* @param start_angle start angle in radians
|
|
51
68
|
* @param end_angle end anglein radians*/
|
|
52
69
|
declare function ellipse_arc(center_x: number, center_y: number, radius_x: number, radius_y: number, start_angle: number, end_angle: number): SVGAttributes<SVGPathElement>;
|
|
53
|
-
|
|
70
|
+
//#endregion
|
|
71
|
+
//#region src/primitives/group.d.ts
|
|
54
72
|
/**This draws a triangle*/
|
|
55
73
|
declare function group(): SVGAttributes<SVGGElement>;
|
|
56
|
-
|
|
74
|
+
//#endregion
|
|
75
|
+
//#region src/primitives/line.d.ts
|
|
57
76
|
/**This creates a line element
|
|
58
77
|
* @param start_x start point on x axis
|
|
59
78
|
* @param start_y start point on y axis
|
|
60
79
|
* @param end_x end point on x axis
|
|
61
80
|
* @param end_y end point on y axis*/
|
|
62
81
|
declare function line(start_x: number, start_y: number, end_x: number, end_y: number): SVGAttributes<SVGLineElement>;
|
|
63
|
-
|
|
82
|
+
//#endregion
|
|
83
|
+
//#region src/primitives/path.d.ts
|
|
64
84
|
/**This creates a path element*/
|
|
65
85
|
declare function path(path: string): SVGAttributes<SVGPathElement>;
|
|
66
|
-
|
|
86
|
+
//#endregion
|
|
87
|
+
//#region src/primitives/rectangle.d.ts
|
|
67
88
|
/**This creates a rectangle with teh center as origin
|
|
68
89
|
* @param center_x x coordinate of center
|
|
69
90
|
* @param center_y y coordinate of center
|
|
@@ -78,39 +99,43 @@ declare function rectangle_from_center(center_x: number, center_y: number, width
|
|
|
78
99
|
* @param height height
|
|
79
100
|
* @param corner_radius radius of corner*/
|
|
80
101
|
declare function rectangle_from_corner(start_x: number, start_y: number, width: number, height: number, corner_radius: number): SVGAttributes<SVGRectElement>;
|
|
81
|
-
|
|
102
|
+
//#endregion
|
|
103
|
+
//#region src/primitives/shared.d.ts
|
|
82
104
|
type SVGElements = {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
105
|
+
ellipse: SVGEllipseElement;
|
|
106
|
+
circle: SVGCircleElement;
|
|
107
|
+
path: SVGPathElement;
|
|
108
|
+
line: SVGLineElement;
|
|
109
|
+
rect: SVGRectElement;
|
|
110
|
+
text: SVGTextElement;
|
|
111
|
+
g: SVGGElement;
|
|
112
|
+
svg: SVGSVGElement;
|
|
113
|
+
foreignObject: SVGForeignObjectElement;
|
|
92
114
|
};
|
|
93
115
|
declare function create_svg_element<K extends keyof SVGElements>(name: K): SVGAttributes<SVGElements[K]>;
|
|
94
|
-
|
|
116
|
+
//#endregion
|
|
117
|
+
//#region src/primitives/svg.d.ts
|
|
95
118
|
/** This returns an empty svg element
|
|
96
119
|
* @param width width of svg
|
|
97
120
|
* @param height height of svg
|
|
98
121
|
* @param viewbox viewbox of svg*/
|
|
99
122
|
declare function svg$1(width: number, height: number, viewbox?: string): SVGAttributes<SVGSVGElement>;
|
|
100
|
-
|
|
123
|
+
//#endregion
|
|
124
|
+
//#region src/util/anchorPoint.d.ts
|
|
101
125
|
declare const SVGAnchorPoint: {
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
126
|
+
readonly bottomLeft: 0;
|
|
127
|
+
readonly middleLeft: 1;
|
|
128
|
+
readonly topLeft: 2;
|
|
129
|
+
readonly topCenter: 3;
|
|
130
|
+
readonly topRight: 4;
|
|
131
|
+
readonly middleRight: 5;
|
|
132
|
+
readonly bottomRight: 6;
|
|
133
|
+
readonly bottomCenter: 7;
|
|
134
|
+
readonly middleCenter: 8;
|
|
111
135
|
};
|
|
112
136
|
type SVGAnchorPoint = (typeof SVGAnchorPoint)[keyof typeof SVGAnchorPoint];
|
|
113
|
-
|
|
137
|
+
//#endregion
|
|
138
|
+
//#region src/primitives/text.d.ts
|
|
114
139
|
/**Creates a text nodes for an svg
|
|
115
140
|
* @param x x coordinate of text
|
|
116
141
|
* @param y y coordinate of text
|
|
@@ -127,31 +152,34 @@ declare function text(x: number, y: number, text: string, size: number, anchor:
|
|
|
127
152
|
* @param size size of text in px
|
|
128
153
|
* @param anchor anchor point of */
|
|
129
154
|
declare function multi_line_text(x: number, y: number, width: number, height: number, text: string, size: number, anchor: SVGAnchorPoint): SVGAttributes<SVGForeignObjectElement>;
|
|
130
|
-
|
|
155
|
+
//#endregion
|
|
156
|
+
//#region src/primitives/triangle.d.ts
|
|
131
157
|
/**This draws a triangle
|
|
132
158
|
* @param center_x x coordinate of center
|
|
133
159
|
* @param center_y y coordinate of center
|
|
134
160
|
* @param width width
|
|
135
161
|
* @param height height*/
|
|
136
162
|
declare function isosceles_triangle(center_x: number, center_y: number, width: number, height: number): SVGAttributes<SVGPathElement>;
|
|
137
|
-
|
|
163
|
+
//#endregion
|
|
164
|
+
//#region src/index.d.ts
|
|
138
165
|
declare const svg: {
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
166
|
+
create: typeof create_svg_element;
|
|
167
|
+
circle: typeof circle;
|
|
168
|
+
circle_arc: typeof circle_arc;
|
|
169
|
+
ellipse: typeof ellipse;
|
|
170
|
+
ellipse_arc: typeof ellipse_arc;
|
|
171
|
+
group: typeof group;
|
|
172
|
+
line: typeof line;
|
|
173
|
+
path: typeof path;
|
|
174
|
+
rectangle_from_center: typeof rectangle_from_center;
|
|
175
|
+
rectangle_from_corner: typeof rectangle_from_corner;
|
|
176
|
+
svg: typeof svg$1;
|
|
177
|
+
text: typeof text;
|
|
178
|
+
multi_line_text: typeof multi_line_text;
|
|
179
|
+
isosceles_triangle: typeof isosceles_triangle;
|
|
180
|
+
angle_to_anchor_point: (angle: number) => 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7;
|
|
154
181
|
};
|
|
155
182
|
type SVGFunc = () => SVGSVGElement;
|
|
156
|
-
|
|
157
|
-
export { SVGAnchorPoint,
|
|
183
|
+
//#endregion
|
|
184
|
+
export { SVGAnchorPoint, SVGFunc, svg };
|
|
185
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/attributes.ts","../src/primitives/circle.ts","../src/primitives/ellipse.ts","../src/primitives/group.ts","../src/primitives/line.ts","../src/primitives/path.ts","../src/primitives/rectangle.ts","../src/primitives/shared.ts","../src/primitives/svg.ts","../src/util/anchorPoint.ts","../src/primitives/text.ts","../src/primitives/triangle.ts","../src/index.ts"],"mappings":";cAAa,aAAA,WAAwB,UAAA;EAAA;cACvB,IAAA,EAAM,CAAA;EADM;EAAA,IAMpB,IAAA,CAAA,GAAQ,CAAA;EALM;EAWlB,MAAA,CAAO,MAAA;EANM;EAWb,CAAA,CAAE,MAAA;EAjBuB;EAuBzB,IAAA,CAAK,IAAA;;EAKL,CAAA,CAAE,IAAA;EA3BgB;EAiClB,YAAA,CAAa,KAAA;EA5BT;EAiCJ,EAAA,CAAG,KAAA;EA3BH;EAiCA,UAAA,CAAA,GAAc,UAAA;EA5Bd;EAiCA,EAAA,CAAA,GAAM,UAAA;EA3BN;EAiCA,SAAA,CAAU,IAAA,UAAc,KAAA;EA5BxB;EAiCA,CAAA,CAAE,IAAA,UAAc,KAAA;EA3BhB;EAuCA,SAAA,CAAU,CAAA,UAAW,CAAA;EAlCrB;EAwCA,MAAA,CAAO,KAAA,UAAe,EAAA,WAAa,EAAA;EAlCnC;EA0CA,KAAA,CAAM,EAAA,UAAY,EAAA;EArClB;EA4CA,KAAA,CAAM,KAAA;EAtCN;EA4CA,KAAA,CAAM,KAAA;EA5CkB;EAkDxB,SAAA,CAAU,SAAA;AAAA;;;AA1GZ;;;;AAAA,iBCQgB,MAAA,CACd,QAAA,UACA,QAAA,UACA,MAAA,WACC,aAAA,CAAc,gBAAA;;;;;;;iBAcD,UAAA,CACd,QAAA,UACA,QAAA,UACA,MAAA,UACA,WAAA,UACA,SAAA,WACC,aAAA,CAAc,cAAA;;;ADhCjB;;;;;AAAA,iBESgB,OAAA,CACd,QAAA,UACA,QAAA,UACA,QAAA,UACA,QAAA,WACC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,UACA,MAAA,UACA,IAAA,UACA,IAAA,UACA,MAAA,EAAQ,cAAA,GACP,aAAA,CAAc,uBAAA;;;AVjFjB;;;;;AAAA,iBWQgB,kBAAA,CACd,QAAA,UACA,QAAA,UACA,KAAA,UACA,MAAA,WACC,aAAA,CAAc,cAAA;;;cCEJ,GAAA;;;;;;;;;;;;;;;;;KAoBD,OAAA,SAAgB,aAAA"}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,378 @@
|
|
|
1
|
+
import { degrees_to_radians } from "@chocbite/ts-lib-math";
|
|
2
|
+
//#region src/attributes.ts
|
|
3
|
+
var SVGAttributes = class {
|
|
4
|
+
constructor(elem) {
|
|
5
|
+
this.#elem = elem;
|
|
6
|
+
}
|
|
7
|
+
#elem;
|
|
8
|
+
/**Returns the svg element */
|
|
9
|
+
get elem() {
|
|
10
|
+
this.#apply_transforms();
|
|
11
|
+
return this.#elem;
|
|
12
|
+
}
|
|
13
|
+
/** Sets the stroke color of the SVG element */
|
|
14
|
+
stroke(stroke) {
|
|
15
|
+
this.#elem.setAttribute("stroke", stroke);
|
|
16
|
+
return this;
|
|
17
|
+
}
|
|
18
|
+
/** Sets the stroke color of the SVG element */
|
|
19
|
+
s(stroke) {
|
|
20
|
+
this.#elem.setAttribute("stroke", stroke);
|
|
21
|
+
return this;
|
|
22
|
+
}
|
|
23
|
+
/** Sets the fill color of the SVG element */
|
|
24
|
+
fill(fill) {
|
|
25
|
+
this.#elem.setAttribute("fill", fill);
|
|
26
|
+
return this;
|
|
27
|
+
}
|
|
28
|
+
/** Sets the fill color of the SVG element */
|
|
29
|
+
f(fill) {
|
|
30
|
+
this.#elem.setAttribute("fill", fill);
|
|
31
|
+
return this;
|
|
32
|
+
}
|
|
33
|
+
/** Sets the stroke width of the SVG element */
|
|
34
|
+
stroke_width(width) {
|
|
35
|
+
this.#elem.setAttribute("stroke-width", String(width));
|
|
36
|
+
return this;
|
|
37
|
+
}
|
|
38
|
+
/** Sets the stroke width of the SVG element */
|
|
39
|
+
sw(width) {
|
|
40
|
+
this.#elem.setAttribute("stroke-width", String(width));
|
|
41
|
+
return this;
|
|
42
|
+
}
|
|
43
|
+
/** Adds one or more class names to the SVG element */
|
|
44
|
+
class_list(...class_name) {
|
|
45
|
+
this.#elem.classList.add(...class_name);
|
|
46
|
+
return this;
|
|
47
|
+
}
|
|
48
|
+
/** Adds one or more class names to the SVG element */
|
|
49
|
+
cl(...class_name) {
|
|
50
|
+
this.#elem.classList.add(...class_name);
|
|
51
|
+
return this;
|
|
52
|
+
}
|
|
53
|
+
/** Sets an arbitrary attribute on the SVG element */
|
|
54
|
+
attribute(name, value) {
|
|
55
|
+
this.#elem.setAttribute(name, value);
|
|
56
|
+
return this;
|
|
57
|
+
}
|
|
58
|
+
/** Sets an arbitrary attribute on the SVG element */
|
|
59
|
+
a(name, value) {
|
|
60
|
+
this.#elem.setAttribute(name, value);
|
|
61
|
+
return this;
|
|
62
|
+
}
|
|
63
|
+
#transforms = [];
|
|
64
|
+
#apply_transforms() {
|
|
65
|
+
if (this.#transforms.length > 0) this.#elem.setAttribute("transform", this.#transforms.join(" "));
|
|
66
|
+
}
|
|
67
|
+
/** Applies a translation transform to the SVG element */
|
|
68
|
+
translate(x, y) {
|
|
69
|
+
this.#transforms.push(`translate(${x} ${y})`);
|
|
70
|
+
return this;
|
|
71
|
+
}
|
|
72
|
+
/** Applies a rotation transform to the SVG element */
|
|
73
|
+
rotate(angle, cx, cy) {
|
|
74
|
+
if (cx !== void 0 && cy !== void 0) this.#transforms.push(`rotate(${angle} ${cx} ${cy})`);
|
|
75
|
+
else this.#transforms.push(`rotate(${angle})`);
|
|
76
|
+
return this;
|
|
77
|
+
}
|
|
78
|
+
/** Applies a scaling transform to the SVG element */
|
|
79
|
+
scale(sx, sy) {
|
|
80
|
+
if (sy !== void 0) this.#transforms.push(`scale(${sx} ${sy})`);
|
|
81
|
+
else this.#transforms.push(`scale(${sx})`);
|
|
82
|
+
return this;
|
|
83
|
+
}
|
|
84
|
+
/** Applies a skewX transform to the SVG element */
|
|
85
|
+
skewX(angle) {
|
|
86
|
+
this.#transforms.push(`skewX(${angle})`);
|
|
87
|
+
return this;
|
|
88
|
+
}
|
|
89
|
+
/** Applies a skewY transform to the SVG element */
|
|
90
|
+
skewY(angle) {
|
|
91
|
+
this.#transforms.push(`skewY(${angle})`);
|
|
92
|
+
return this;
|
|
93
|
+
}
|
|
94
|
+
/** Applies a custom transform to the SVG element */
|
|
95
|
+
transform(transform) {
|
|
96
|
+
this.#transforms.push(transform);
|
|
97
|
+
return this;
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
//#endregion
|
|
101
|
+
//#region src/shared.ts
|
|
102
|
+
const NAME_SPACE = "http://www.w3.org/2000/svg";
|
|
103
|
+
//#endregion
|
|
104
|
+
//#region src/primitives/shared.ts
|
|
105
|
+
function create_svg_element(name) {
|
|
106
|
+
return new SVGAttributes(document.createElementNS(NAME_SPACE, name));
|
|
107
|
+
}
|
|
108
|
+
//#endregion
|
|
109
|
+
//#region src/primitives/ellipse.ts
|
|
110
|
+
/**This creates a svg ellipse
|
|
111
|
+
* @param center_x x coordinate of center
|
|
112
|
+
* @param center_y y coordinate of center
|
|
113
|
+
* @param radius_x x radius of circle
|
|
114
|
+
* @param radius_y y radius of circle*/
|
|
115
|
+
function ellipse(center_x, center_y, radius_x, radius_y) {
|
|
116
|
+
return create_svg_element("ellipse").a("cx", String(center_x)).a("cy", String(center_y)).a("rx", String(radius_x)).a("ry", String(radius_y));
|
|
117
|
+
}
|
|
118
|
+
/**This draws parts of a circle/ellipse, the circle direction is reversed
|
|
119
|
+
* @param center_x the center point on the x axis
|
|
120
|
+
* @param center_y the center point on the y axis
|
|
121
|
+
* @param radius_x radius in x axis
|
|
122
|
+
* @param radius_y radius in y axis
|
|
123
|
+
* @param start_angle start angle in radians
|
|
124
|
+
* @param end_angle end anglein radians*/
|
|
125
|
+
function ellipse_arc(center_x, center_y, radius_x, radius_y, start_angle, end_angle) {
|
|
126
|
+
const start_radian = degrees_to_radians(start_angle);
|
|
127
|
+
end_angle = degrees_to_radians(end_angle - start_angle);
|
|
128
|
+
const s_x = radius_x * Math.cos(start_radian) + center_x;
|
|
129
|
+
const s_y = radius_y * Math.sin(start_radian) + center_y;
|
|
130
|
+
const e_x = radius_x * Math.cos(start_radian + end_angle) + center_x;
|
|
131
|
+
const e_y = radius_y * Math.sin(start_radian + end_angle) + center_y;
|
|
132
|
+
const f_a = end_angle > Math.PI ? 1 : 0;
|
|
133
|
+
const f_s = end_angle > 0 ? 1 : 0;
|
|
134
|
+
return create_svg_element("path").a("d", `M ${s_x} ${s_y} A ${radius_x} ${radius_y} 0 ${f_a} ${f_s} ${e_x} ${e_y}`);
|
|
135
|
+
}
|
|
136
|
+
//#endregion
|
|
137
|
+
//#region src/primitives/circle.ts
|
|
138
|
+
/**This creates a svg circle
|
|
139
|
+
* @param center_x x coordinate of center
|
|
140
|
+
* @param center_y y coordinate of center
|
|
141
|
+
* @param radius radius of circle*/
|
|
142
|
+
function circle(center_x, center_y, radius) {
|
|
143
|
+
const circle = create_svg_element("circle");
|
|
144
|
+
circle.a("cx", String(center_x));
|
|
145
|
+
circle.a("cy", String(center_y));
|
|
146
|
+
circle.a("r", String(radius));
|
|
147
|
+
return circle;
|
|
148
|
+
}
|
|
149
|
+
/**This draws parts of a circle/ellipse, the circle direction is reversed
|
|
150
|
+
* @param center_x the center point on the x axis
|
|
151
|
+
* @param center_y the center point on the y axis
|
|
152
|
+
* @param radius radius in x axis
|
|
153
|
+
* @param start_angle start angle in radians
|
|
154
|
+
* @param end_angle end anglein radians*/
|
|
155
|
+
function circle_arc(center_x, center_y, radius, start_angle, end_angle) {
|
|
156
|
+
return ellipse_arc(center_x, center_y, radius, radius, start_angle, end_angle);
|
|
157
|
+
}
|
|
158
|
+
//#endregion
|
|
159
|
+
//#region src/primitives/group.ts
|
|
160
|
+
/**This draws a triangle*/
|
|
161
|
+
function group() {
|
|
162
|
+
return create_svg_element("g");
|
|
163
|
+
}
|
|
164
|
+
//#endregion
|
|
165
|
+
//#region src/primitives/line.ts
|
|
166
|
+
/**This creates a line element
|
|
167
|
+
* @param start_x start point on x axis
|
|
168
|
+
* @param start_y start point on y axis
|
|
169
|
+
* @param end_x end point on x axis
|
|
170
|
+
* @param end_y end point on y axis*/
|
|
171
|
+
function line(start_x, start_y, end_x, end_y) {
|
|
172
|
+
return create_svg_element("line").a("x1", String(start_x)).a("y1", String(start_y)).a("x2", String(end_x)).a("y2", String(end_y));
|
|
173
|
+
}
|
|
174
|
+
//#endregion
|
|
175
|
+
//#region src/primitives/path.ts
|
|
176
|
+
/**This creates a path element*/
|
|
177
|
+
function path(path) {
|
|
178
|
+
return create_svg_element("path").a("d", path);
|
|
179
|
+
}
|
|
180
|
+
//#endregion
|
|
181
|
+
//#region src/primitives/rectangle.ts
|
|
182
|
+
/**This creates a rectangle with teh center as origin
|
|
183
|
+
* @param center_x x coordinate of center
|
|
184
|
+
* @param center_y y coordinate of center
|
|
185
|
+
* @param width width
|
|
186
|
+
* @param height height
|
|
187
|
+
* @param corner_radius radius of corner*/
|
|
188
|
+
function rectangle_from_center(center_x, center_y, width, height, corner_radius) {
|
|
189
|
+
return create_svg_element("rect").a("x", String(center_x - width / 2)).a("y", String(center_y - height / 2)).a("width", String(width)).a("height", String(height)).a("rx", String(corner_radius));
|
|
190
|
+
}
|
|
191
|
+
/**This creates a rectangle with teh center as origin
|
|
192
|
+
* @param start_x x coordinate of center
|
|
193
|
+
* @param start_y y coordinate of center
|
|
194
|
+
* @param width width
|
|
195
|
+
* @param height height
|
|
196
|
+
* @param corner_radius radius of corner*/
|
|
197
|
+
function rectangle_from_corner(start_x, start_y, width, height, corner_radius) {
|
|
198
|
+
return create_svg_element("rect").a("x", String(start_x)).a("y", String(start_y)).a("width", String(width)).a("height", String(height)).a("rx", String(corner_radius));
|
|
199
|
+
}
|
|
200
|
+
//#endregion
|
|
201
|
+
//#region src/primitives/svg.ts
|
|
202
|
+
/** This returns an empty svg element
|
|
203
|
+
* @param width width of svg
|
|
204
|
+
* @param height height of svg
|
|
205
|
+
* @param viewbox viewbox of svg*/
|
|
206
|
+
function svg$1(width, height, viewbox = `0 0 ${width} ${height}`) {
|
|
207
|
+
return create_svg_element("svg").a("width", String(width)).a("height", String(height)).a("viewBox", String(viewbox));
|
|
208
|
+
}
|
|
209
|
+
//#endregion
|
|
210
|
+
//#region src/util/anchorPoint.ts
|
|
211
|
+
const SVGAnchorPoint = {
|
|
212
|
+
bottomLeft: 0,
|
|
213
|
+
middleLeft: 1,
|
|
214
|
+
topLeft: 2,
|
|
215
|
+
topCenter: 3,
|
|
216
|
+
topRight: 4,
|
|
217
|
+
middleRight: 5,
|
|
218
|
+
bottomRight: 6,
|
|
219
|
+
bottomCenter: 7,
|
|
220
|
+
middleCenter: 8
|
|
221
|
+
};
|
|
222
|
+
//#endregion
|
|
223
|
+
//#region src/primitives/text.ts
|
|
224
|
+
/**Creates a text nodes for an svg
|
|
225
|
+
* @param x x coordinate of text
|
|
226
|
+
* @param y y coordinate of text
|
|
227
|
+
* @param text text
|
|
228
|
+
* @param size size of text in px
|
|
229
|
+
* @param anchor anchor point of text*/
|
|
230
|
+
function text(x, y, text, size, anchor) {
|
|
231
|
+
const text_elem = create_svg_element("text").a("x", String(x)).a("y", String(y)).a("font-size", String(size));
|
|
232
|
+
text_elem.elem.innerHTML = text;
|
|
233
|
+
switch (anchor) {
|
|
234
|
+
case SVGAnchorPoint.bottomLeft:
|
|
235
|
+
case SVGAnchorPoint.middleLeft:
|
|
236
|
+
case SVGAnchorPoint.topLeft:
|
|
237
|
+
text_elem.a("text-anchor", "start");
|
|
238
|
+
break;
|
|
239
|
+
case SVGAnchorPoint.topCenter:
|
|
240
|
+
case SVGAnchorPoint.bottomCenter:
|
|
241
|
+
case SVGAnchorPoint.middleCenter:
|
|
242
|
+
text_elem.a("text-anchor", "middle");
|
|
243
|
+
break;
|
|
244
|
+
case SVGAnchorPoint.topRight:
|
|
245
|
+
case SVGAnchorPoint.middleRight:
|
|
246
|
+
case SVGAnchorPoint.bottomRight:
|
|
247
|
+
text_elem.a("text-anchor", "end");
|
|
248
|
+
break;
|
|
249
|
+
}
|
|
250
|
+
switch (anchor) {
|
|
251
|
+
case SVGAnchorPoint.bottomLeft:
|
|
252
|
+
case SVGAnchorPoint.bottomRight:
|
|
253
|
+
case SVGAnchorPoint.bottomCenter:
|
|
254
|
+
text_elem.a("dominant-baseline", "auto");
|
|
255
|
+
break;
|
|
256
|
+
case SVGAnchorPoint.middleLeft:
|
|
257
|
+
case SVGAnchorPoint.middleRight:
|
|
258
|
+
case SVGAnchorPoint.middleCenter:
|
|
259
|
+
text_elem.a("dominant-baseline", "central");
|
|
260
|
+
break;
|
|
261
|
+
case SVGAnchorPoint.topLeft:
|
|
262
|
+
case SVGAnchorPoint.topCenter:
|
|
263
|
+
case SVGAnchorPoint.topRight:
|
|
264
|
+
text_elem.a("dominant-baseline", "hanging");
|
|
265
|
+
break;
|
|
266
|
+
}
|
|
267
|
+
return text_elem;
|
|
268
|
+
}
|
|
269
|
+
/**Creates a text nodes for an svg
|
|
270
|
+
* @param x x coordinate of text
|
|
271
|
+
* @param y y coordinate of text
|
|
272
|
+
* @param width width of text
|
|
273
|
+
* @param height height of text
|
|
274
|
+
* @param text text
|
|
275
|
+
* @param size size of text in px
|
|
276
|
+
* @param anchor anchor point of */
|
|
277
|
+
function multi_line_text(x, y, width, height, text, size, anchor) {
|
|
278
|
+
const text_element = create_svg_element("foreignObject");
|
|
279
|
+
const text_div = text_element.elem.appendChild(document.createElement("div"));
|
|
280
|
+
text_element.a("width", String(width));
|
|
281
|
+
text_element.a("height", String(height));
|
|
282
|
+
text_element.a("x", String(x));
|
|
283
|
+
text_element.a("y", String(y));
|
|
284
|
+
text_div.style.fontSize = size + "px";
|
|
285
|
+
text_div.style.width = "100%";
|
|
286
|
+
text_div.style.height = "100%";
|
|
287
|
+
text_div.style.display = "flex";
|
|
288
|
+
text_div.innerHTML = text;
|
|
289
|
+
switch (anchor) {
|
|
290
|
+
case SVGAnchorPoint.bottomLeft:
|
|
291
|
+
case SVGAnchorPoint.middleLeft:
|
|
292
|
+
case SVGAnchorPoint.topLeft:
|
|
293
|
+
text_div.style.textAlign = "start";
|
|
294
|
+
text_div.style.justifyContent = "flex-start";
|
|
295
|
+
break;
|
|
296
|
+
case SVGAnchorPoint.topCenter:
|
|
297
|
+
case SVGAnchorPoint.bottomCenter:
|
|
298
|
+
case SVGAnchorPoint.middleCenter:
|
|
299
|
+
text_div.style.textAlign = "center";
|
|
300
|
+
text_div.style.justifyContent = "center";
|
|
301
|
+
break;
|
|
302
|
+
case SVGAnchorPoint.topRight:
|
|
303
|
+
case SVGAnchorPoint.middleRight:
|
|
304
|
+
case SVGAnchorPoint.bottomRight:
|
|
305
|
+
text_div.style.textAlign = "end";
|
|
306
|
+
text_div.style.justifyContent = "flex-end";
|
|
307
|
+
break;
|
|
308
|
+
}
|
|
309
|
+
switch (anchor) {
|
|
310
|
+
case SVGAnchorPoint.bottomLeft:
|
|
311
|
+
case SVGAnchorPoint.bottomRight:
|
|
312
|
+
case SVGAnchorPoint.bottomCenter:
|
|
313
|
+
text_div.style.alignItems = "flex-end";
|
|
314
|
+
break;
|
|
315
|
+
case SVGAnchorPoint.middleLeft:
|
|
316
|
+
case SVGAnchorPoint.middleRight:
|
|
317
|
+
case SVGAnchorPoint.middleCenter:
|
|
318
|
+
text_div.style.alignItems = "center";
|
|
319
|
+
break;
|
|
320
|
+
case SVGAnchorPoint.topLeft:
|
|
321
|
+
case SVGAnchorPoint.topCenter:
|
|
322
|
+
case SVGAnchorPoint.topRight:
|
|
323
|
+
text_div.style.alignItems = "flex-start";
|
|
324
|
+
break;
|
|
325
|
+
}
|
|
326
|
+
return text_element;
|
|
327
|
+
}
|
|
328
|
+
//#endregion
|
|
329
|
+
//#region src/primitives/triangle.ts
|
|
330
|
+
/**This draws a triangle
|
|
331
|
+
* @param center_x x coordinate of center
|
|
332
|
+
* @param center_y y coordinate of center
|
|
333
|
+
* @param width width
|
|
334
|
+
* @param height height*/
|
|
335
|
+
function isosceles_triangle(center_x, center_y, width, height) {
|
|
336
|
+
const half_w = width / 2;
|
|
337
|
+
const half_h = height / 2;
|
|
338
|
+
return create_svg_element("path").a("d", "M" + (center_x - half_w) + "," + (center_y + half_h) + " " + (center_x + half_w) + "," + (center_y + half_h) + " " + center_x + "," + (center_y - half_h) + "Z");
|
|
339
|
+
}
|
|
340
|
+
//#endregion
|
|
341
|
+
//#region src/util/angleToAnchorPoint.ts
|
|
342
|
+
/**Converts an angle to an anchor point
|
|
343
|
+
* @param angle angle in radians*/
|
|
344
|
+
const angle_to_anchor_point = (angle) => {
|
|
345
|
+
const sec = angle >= 0 ? angle % 6.283185307179586 : -(angle % 6.283185307179586);
|
|
346
|
+
if (sec > 5.93411945678072) return SVGAnchorPoint.middleRight;
|
|
347
|
+
else if (sec > 4.974188368183839) return SVGAnchorPoint.topRight;
|
|
348
|
+
else if (sec > 4.45058959258554) return SVGAnchorPoint.topCenter;
|
|
349
|
+
else if (sec > 3.490658503988659) return SVGAnchorPoint.topLeft;
|
|
350
|
+
else if (sec > 2.792526803190927) return SVGAnchorPoint.middleLeft;
|
|
351
|
+
else if (sec > 1.832595714594046) return SVGAnchorPoint.bottomLeft;
|
|
352
|
+
else if (sec > 1.308996938995747) return SVGAnchorPoint.bottomCenter;
|
|
353
|
+
else if (sec > .3490658503988659) return SVGAnchorPoint.bottomRight;
|
|
354
|
+
else return SVGAnchorPoint.middleRight;
|
|
355
|
+
};
|
|
356
|
+
//#endregion
|
|
357
|
+
//#region src/index.ts
|
|
358
|
+
const svg = {
|
|
359
|
+
create: create_svg_element,
|
|
360
|
+
circle,
|
|
361
|
+
circle_arc,
|
|
362
|
+
ellipse,
|
|
363
|
+
ellipse_arc,
|
|
364
|
+
group,
|
|
365
|
+
line,
|
|
366
|
+
path,
|
|
367
|
+
rectangle_from_center,
|
|
368
|
+
rectangle_from_corner,
|
|
369
|
+
svg: svg$1,
|
|
370
|
+
text,
|
|
371
|
+
multi_line_text,
|
|
372
|
+
isosceles_triangle,
|
|
373
|
+
angle_to_anchor_point
|
|
374
|
+
};
|
|
375
|
+
//#endregion
|
|
376
|
+
export { SVGAnchorPoint, svg };
|
|
377
|
+
|
|
378
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":["#elem","#apply_transforms","#transforms","svg","svgsvg"],"sources":["../src/attributes.ts","../src/shared.ts","../src/primitives/shared.ts","../src/primitives/ellipse.ts","../src/primitives/circle.ts","../src/primitives/group.ts","../src/primitives/line.ts","../src/primitives/path.ts","../src/primitives/rectangle.ts","../src/primitives/svg.ts","../src/util/anchorPoint.ts","../src/primitives/text.ts","../src/primitives/triangle.ts","../src/util/angleToAnchorPoint.ts","../src/index.ts"],"sourcesContent":["export class SVGAttributes<T extends SVGElement> {\n constructor(elem: T) {\n this.#elem = elem;\n }\n #elem: T;\n /**Returns the svg element */\n get elem(): T {\n this.#apply_transforms();\n return this.#elem;\n }\n\n /** Sets the stroke color of the SVG element */\n stroke(stroke: string): this {\n this.#elem.setAttribute(\"stroke\", stroke);\n return this;\n }\n /** Sets the stroke color of the SVG element */\n s(stroke: string): this {\n this.#elem.setAttribute(\"stroke\", stroke);\n return this;\n }\n\n /** Sets the fill color of the SVG element */\n fill(fill: string): this {\n this.#elem.setAttribute(\"fill\", fill);\n return this;\n }\n /** Sets the fill color of the SVG element */\n f(fill: string): this {\n this.#elem.setAttribute(\"fill\", fill);\n return this;\n }\n\n /** Sets the stroke width of the SVG element */\n stroke_width(width: number): this {\n this.#elem.setAttribute(\"stroke-width\", String(width));\n return this;\n }\n /** Sets the stroke width of the SVG element */\n sw(width: number): this {\n this.#elem.setAttribute(\"stroke-width\", String(width));\n return this;\n }\n\n /** Adds one or more class names to the SVG element */\n class_list(...class_name: string[]): this {\n this.#elem.classList.add(...class_name);\n return this;\n }\n /** Adds one or more class names to the SVG element */\n cl(...class_name: string[]): this {\n this.#elem.classList.add(...class_name);\n return this;\n }\n\n /** Sets an arbitrary attribute on the SVG element */\n attribute(name: string, value: string): this {\n this.#elem.setAttribute(name, value);\n return this;\n }\n /** Sets an arbitrary attribute on the SVG element */\n a(name: string, value: string): this {\n this.#elem.setAttribute(name, value);\n return this;\n }\n\n #transforms: string[] = [];\n #apply_transforms(): void {\n if (this.#transforms.length > 0)\n this.#elem.setAttribute(\"transform\", this.#transforms.join(\" \"));\n }\n\n /** Applies a translation transform to the SVG element */\n translate(x: number, y: number): this {\n this.#transforms.push(`translate(${x} ${y})`);\n return this;\n }\n\n /** Applies a rotation transform to the SVG element */\n rotate(angle: number, cx?: number, cy?: number): this {\n if (cx !== undefined && cy !== undefined)\n this.#transforms.push(`rotate(${angle} ${cx} ${cy})`);\n else this.#transforms.push(`rotate(${angle})`);\n return this;\n }\n\n /** Applies a scaling transform to the SVG element */\n scale(sx: number, sy?: number): this {\n if (sy !== undefined) this.#transforms.push(`scale(${sx} ${sy})`);\n else this.#transforms.push(`scale(${sx})`);\n return this;\n }\n\n /** Applies a skewX transform to the SVG element */\n skewX(angle: number): this {\n this.#transforms.push(`skewX(${angle})`);\n return this;\n }\n\n /** Applies a skewY transform to the SVG element */\n skewY(angle: number): this {\n this.#transforms.push(`skewY(${angle})`);\n return this;\n }\n\n /** Applies a custom transform to the SVG element */\n transform(transform: string): this {\n this.#transforms.push(transform);\n return this;\n }\n}\n","export const NAME_SPACE = \"http://www.w3.org/2000/svg\";\n","import { SVGAttributes } from \"../attributes\";\nimport { NAME_SPACE } from \"../shared\";\n\ntype SVGElements = {\n ellipse: SVGEllipseElement;\n circle: SVGCircleElement;\n path: SVGPathElement;\n line: SVGLineElement;\n rect: SVGRectElement;\n text: SVGTextElement;\n g: SVGGElement;\n svg: SVGSVGElement;\n foreignObject: SVGForeignObjectElement;\n};\n\nexport function create_svg_element<K extends keyof SVGElements>(\n name: K,\n): SVGAttributes<SVGElements[K]> {\n return new SVGAttributes(\n document.createElementNS(NAME_SPACE, name) as SVGElements[K],\n );\n}\n","import { degrees_to_radians } from \"@chocbite/ts-lib-math\";\nimport { SVGAttributes } from \"../attributes\";\nimport { create_svg_element } from \"./shared\";\n\n/**This creates a svg ellipse\n * @param center_x x coordinate of center\n * @param center_y y coordinate of center\n * @param radius_x x radius of circle\n * @param radius_y y radius of circle*/\nexport function ellipse(\n center_x: number,\n center_y: number,\n radius_x: number,\n radius_y: number,\n): SVGAttributes<SVGEllipseElement> {\n return create_svg_element(\"ellipse\")\n .a(\"cx\", String(center_x))\n .a(\"cy\", String(center_y))\n .a(\"rx\", String(radius_x))\n .a(\"ry\", String(radius_y));\n}\n\n/**This draws parts of a circle/ellipse, the circle direction is reversed\n * @param center_x the center point on the x axis\n * @param center_y the center point on the y axis\n * @param radius_x radius in x axis\n * @param radius_y radius in y axis\n * @param start_angle start angle in radians\n * @param end_angle end anglein radians*/\nexport function ellipse_arc(\n center_x: number,\n center_y: number,\n radius_x: number,\n radius_y: number,\n start_angle: number,\n end_angle: number,\n): SVGAttributes<SVGPathElement> {\n const start_radian = degrees_to_radians(start_angle);\n end_angle = degrees_to_radians(end_angle - start_angle);\n const s_x = radius_x * Math.cos(start_radian) + center_x;\n const s_y = radius_y * Math.sin(start_radian) + center_y;\n const e_x = radius_x * Math.cos(start_radian + end_angle) + center_x;\n const e_y = radius_y * Math.sin(start_radian + end_angle) + center_y;\n const f_a = end_angle > Math.PI ? 1 : 0;\n const f_s = end_angle > 0 ? 1 : 0;\n return create_svg_element(\"path\").a(\n \"d\",\n `M ${s_x} ${s_y} A ${radius_x} ${radius_y} 0 ${f_a} ${f_s} ${e_x} ${e_y}`,\n );\n}\n","import { SVGAttributes } from \"../attributes\";\nimport { ellipse_arc } from \"./ellipse\";\nimport { create_svg_element } from \"./shared\";\n\n/**This creates a svg circle\n * @param center_x x coordinate of center\n * @param center_y y coordinate of center\n * @param radius radius of circle*/\nexport function circle(\n center_x: number,\n center_y: number,\n radius: number,\n): SVGAttributes<SVGCircleElement> {\n const circle = create_svg_element(\"circle\");\n circle.a(\"cx\", String(center_x));\n circle.a(\"cy\", String(center_y));\n circle.a(\"r\", String(radius));\n return circle;\n}\n\n/**This draws parts of a circle/ellipse, the circle direction is reversed\n * @param center_x the center point on the x axis\n * @param center_y the center point on the y axis\n * @param radius radius in x axis\n * @param start_angle start angle in radians\n * @param end_angle end anglein radians*/\nexport function circle_arc(\n center_x: number,\n center_y: number,\n radius: number,\n start_angle: number,\n end_angle: number,\n): SVGAttributes<SVGPathElement> {\n return ellipse_arc(\n center_x,\n center_y,\n radius,\n radius,\n start_angle,\n end_angle,\n );\n}\n","import { SVGAttributes } from \"../attributes\";\nimport { create_svg_element } from \"./shared\";\n\n/**This draws a triangle*/\nexport function group(): SVGAttributes<SVGGElement> {\n return create_svg_element(\"g\");\n}\n","import { SVGAttributes } from \"../attributes\";\nimport { create_svg_element } from \"./shared\";\n\n/**This creates a line element\n * @param start_x start point on x axis\n * @param start_y start point on y axis\n * @param end_x end point on x axis\n * @param end_y end point on y axis*/\nexport function line(\n start_x: number,\n start_y: number,\n end_x: number,\n end_y: number,\n): SVGAttributes<SVGLineElement> {\n return create_svg_element(\"line\")\n .a(\"x1\", String(start_x))\n .a(\"y1\", String(start_y))\n .a(\"x2\", String(end_x))\n .a(\"y2\", String(end_y));\n}\n","import { SVGAttributes } from \"../attributes\";\nimport { create_svg_element } from \"./shared\";\n\n/**This creates a path element*/\nexport function path(path: string): SVGAttributes<SVGPathElement> {\n return create_svg_element(\"path\").a(\"d\", path);\n}\n\n/**This creates a line with a path element\n * @param start_x start point on x axis\n * @param start_y start point on y axis\n * @param end_x end point on x axis\n * @param end_y end point on y axis*/\nexport function path_line(\n start_x: number,\n start_y: number,\n end_x: number,\n end_y: number,\n): SVGAttributes<SVGPathElement> {\n return create_svg_element(\"path\").a(\n \"d\",\n `M ${start_x} ${start_y} L ${end_x} ${end_y}`,\n );\n}\n","import { SVGAttributes } from \"../attributes\";\nimport { create_svg_element } from \"./shared\";\n\n/**This creates a rectangle with teh center as origin\n * @param center_x x coordinate of center\n * @param center_y y coordinate of center\n * @param width width\n * @param height height\n * @param corner_radius radius of corner*/\nexport function rectangle_from_center(\n center_x: number,\n center_y: number,\n width: number,\n height: number,\n corner_radius: number,\n): SVGAttributes<SVGRectElement> {\n return create_svg_element(\"rect\")\n .a(\"x\", String(center_x - width / 2))\n .a(\"y\", String(center_y - height / 2))\n .a(\"width\", String(width))\n .a(\"height\", String(height))\n .a(\"rx\", String(corner_radius));\n}\n\n/**This creates a rectangle with teh center as origin\n * @param start_x x coordinate of center\n * @param start_y y coordinate of center\n * @param width width\n * @param height height\n * @param corner_radius radius of corner*/\nexport function rectangle_from_corner(\n start_x: number,\n start_y: number,\n width: number,\n height: number,\n corner_radius: number,\n): SVGAttributes<SVGRectElement> {\n return create_svg_element(\"rect\")\n .a(\"x\", String(start_x))\n .a(\"y\", String(start_y))\n .a(\"width\", String(width))\n .a(\"height\", String(height))\n .a(\"rx\", String(corner_radius));\n}\n","import { SVGAttributes } from \"../attributes\";\nimport { create_svg_element } from \"./shared\";\n\n/** This returns an empty svg element\n * @param width width of svg\n * @param height height of svg\n * @param viewbox viewbox of svg*/\nexport function svg(\n width: number,\n height: number,\n viewbox: string = `0 0 ${width} ${height}`,\n): SVGAttributes<SVGSVGElement> {\n return create_svg_element(\"svg\")\n .a(\"width\", String(width))\n .a(\"height\", String(height))\n .a(\"viewBox\", String(viewbox));\n}\n","//Enum describing the diffrent possible anchor points\nexport const SVGAnchorPoint = {\n bottomLeft: 0,\n middleLeft: 1,\n topLeft: 2,\n topCenter: 3,\n topRight: 4,\n middleRight: 5,\n bottomRight: 6,\n bottomCenter: 7,\n middleCenter: 8,\n} as const;\nexport type SVGAnchorPoint =\n (typeof SVGAnchorPoint)[keyof typeof SVGAnchorPoint];\n","import { SVGAttributes } from \"../attributes\";\nimport { SVGAnchorPoint } from \"../util/anchorPoint\";\nimport { create_svg_element } from \"./shared\";\n\n/**Creates a text nodes for an svg\n * @param x x coordinate of text\n * @param y y coordinate of text\n * @param text text\n * @param size size of text in px\n * @param anchor anchor point of text*/\nexport function text(\n x: number,\n y: number,\n text: string,\n size: number,\n anchor: SVGAnchorPoint,\n): SVGAttributes<SVGTextElement> {\n const text_elem = create_svg_element(\"text\")\n .a(\"x\", String(x))\n .a(\"y\", String(y))\n .a(\"font-size\", String(size));\n text_elem.elem.innerHTML = text;\n switch (anchor) {\n case SVGAnchorPoint.bottomLeft:\n case SVGAnchorPoint.middleLeft:\n case SVGAnchorPoint.topLeft: {\n text_elem.a(\"text-anchor\", \"start\");\n break;\n }\n case SVGAnchorPoint.topCenter:\n case SVGAnchorPoint.bottomCenter:\n case SVGAnchorPoint.middleCenter: {\n text_elem.a(\"text-anchor\", \"middle\");\n break;\n }\n case SVGAnchorPoint.topRight:\n case SVGAnchorPoint.middleRight:\n case SVGAnchorPoint.bottomRight: {\n text_elem.a(\"text-anchor\", \"end\");\n break;\n }\n }\n switch (anchor) {\n case SVGAnchorPoint.bottomLeft:\n case SVGAnchorPoint.bottomRight:\n case SVGAnchorPoint.bottomCenter: {\n text_elem.a(\"dominant-baseline\", \"auto\");\n break;\n }\n case SVGAnchorPoint.middleLeft:\n case SVGAnchorPoint.middleRight:\n case SVGAnchorPoint.middleCenter: {\n text_elem.a(\"dominant-baseline\", \"central\");\n break;\n }\n case SVGAnchorPoint.topLeft:\n case SVGAnchorPoint.topCenter:\n case SVGAnchorPoint.topRight: {\n text_elem.a(\"dominant-baseline\", \"hanging\");\n break;\n }\n }\n return text_elem;\n}\n\n/**Creates a text nodes for an svg\n * @param x x coordinate of text\n * @param y y coordinate of text\n * @param width width of text\n * @param height height of text\n * @param text text\n * @param size size of text in px\n * @param anchor anchor point of */\nexport function multi_line_text(\n x: number,\n y: number,\n width: number,\n height: number,\n text: string,\n size: number,\n anchor: SVGAnchorPoint,\n): SVGAttributes<SVGForeignObjectElement> {\n const text_element = create_svg_element(\"foreignObject\");\n const text_div = text_element.elem.appendChild(document.createElement(\"div\"));\n text_element.a(\"width\", String(width));\n text_element.a(\"height\", String(height));\n text_element.a(\"x\", String(x));\n text_element.a(\"y\", String(y));\n text_div.style.fontSize = size + \"px\";\n text_div.style.width = \"100%\";\n text_div.style.height = \"100%\";\n text_div.style.display = \"flex\";\n text_div.innerHTML = text;\n switch (anchor) {\n case SVGAnchorPoint.bottomLeft:\n case SVGAnchorPoint.middleLeft:\n case SVGAnchorPoint.topLeft: {\n text_div.style.textAlign = \"start\";\n text_div.style.justifyContent = \"flex-start\";\n break;\n }\n case SVGAnchorPoint.topCenter:\n case SVGAnchorPoint.bottomCenter:\n case SVGAnchorPoint.middleCenter: {\n text_div.style.textAlign = \"center\";\n text_div.style.justifyContent = \"center\";\n break;\n }\n case SVGAnchorPoint.topRight:\n case SVGAnchorPoint.middleRight:\n case SVGAnchorPoint.bottomRight: {\n text_div.style.textAlign = \"end\";\n text_div.style.justifyContent = \"flex-end\";\n break;\n }\n }\n switch (anchor) {\n case SVGAnchorPoint.bottomLeft:\n case SVGAnchorPoint.bottomRight:\n case SVGAnchorPoint.bottomCenter: {\n text_div.style.alignItems = \"flex-end\";\n break;\n }\n case SVGAnchorPoint.middleLeft:\n case SVGAnchorPoint.middleRight:\n case SVGAnchorPoint.middleCenter: {\n text_div.style.alignItems = \"center\";\n break;\n }\n case SVGAnchorPoint.topLeft:\n case SVGAnchorPoint.topCenter:\n case SVGAnchorPoint.topRight: {\n text_div.style.alignItems = \"flex-start\";\n break;\n }\n }\n return text_element;\n}\n","import { SVGAttributes } from \"../attributes\";\nimport { create_svg_element } from \"./shared\";\n\n/**This draws a triangle\n * @param center_x x coordinate of center\n * @param center_y y coordinate of center\n * @param width width\n * @param height height*/\nexport function isosceles_triangle(\n center_x: number,\n center_y: number,\n width: number,\n height: number,\n): SVGAttributes<SVGPathElement> {\n const half_w = width / 2;\n const half_h = height / 2;\n return create_svg_element(\"path\").a(\n \"d\",\n \"M\" +\n (center_x - half_w) +\n \",\" +\n (center_y + half_h) +\n \" \" +\n (center_x + half_w) +\n \",\" +\n (center_y + half_h) +\n \" \" +\n center_x +\n \",\" +\n (center_y - half_h) +\n \"Z\",\n );\n}\n","import { SVGAnchorPoint } from \"./anchorPoint\";\n\n/**Converts an angle to an anchor point\n * @param angle angle in radians*/\nexport const angle_to_anchor_point = (angle: number) => {\n const sec =\n angle >= 0 ? angle % 6.283185307179586 : -(angle % 6.283185307179586);\n if (sec > 5.93411945678072) {\n return SVGAnchorPoint.middleRight;\n } else if (sec > 4.974188368183839) {\n return SVGAnchorPoint.topRight;\n } else if (sec > 4.45058959258554) {\n return SVGAnchorPoint.topCenter;\n } else if (sec > 3.4906585039886591) {\n return SVGAnchorPoint.topLeft;\n } else if (sec > 2.792526803190927) {\n return SVGAnchorPoint.middleLeft;\n } else if (sec > 1.832595714594046) {\n return SVGAnchorPoint.bottomLeft;\n } else if (sec > 1.308996938995747) {\n return SVGAnchorPoint.bottomCenter;\n } else if (sec > 0.3490658503988659) {\n return SVGAnchorPoint.bottomRight;\n } else {\n return SVGAnchorPoint.middleRight;\n }\n};\n","import { circle, circle_arc } from \"./primitives/circle\";\nimport { ellipse, ellipse_arc } from \"./primitives/ellipse\";\nimport { group } from \"./primitives/group\";\nimport { line } from \"./primitives/line\";\nimport { path } from \"./primitives/path\";\nimport {\n rectangle_from_center,\n rectangle_from_corner,\n} from \"./primitives/rectangle\";\nimport { create_svg_element } from \"./primitives/shared\";\nimport { svg as svgsvg } from \"./primitives/svg\";\nimport { multi_line_text, text } from \"./primitives/text\";\nimport { isosceles_triangle } from \"./primitives/triangle\";\nimport { angle_to_anchor_point } from \"./util/angleToAnchorPoint\";\n\nexport const svg = {\n create: create_svg_element,\n //Primitives\n circle: circle,\n circle_arc: circle_arc,\n ellipse: ellipse,\n ellipse_arc: ellipse_arc,\n group: group,\n line: line,\n path: path,\n rectangle_from_center: rectangle_from_center,\n rectangle_from_corner: rectangle_from_corner,\n svg: svgsvg,\n text: text,\n multi_line_text: multi_line_text,\n isosceles_triangle: isosceles_triangle,\n //Utilities\n angle_to_anchor_point: angle_to_anchor_point,\n};\n\nexport type SVGFunc = () => SVGSVGElement;\nexport { SVGAnchorPoint } from \"./util/anchorPoint\";\n"],"mappings":";;AAAA,IAAa,gBAAb,MAAiD;CAC/C,YAAY,MAAS;AACnB,QAAA,OAAa;;CAEf;;CAEA,IAAI,OAAU;AACZ,QAAA,kBAAwB;AACxB,SAAO,MAAA;;;CAIT,OAAO,QAAsB;AAC3B,QAAA,KAAW,aAAa,UAAU,OAAO;AACzC,SAAO;;;CAGT,EAAE,QAAsB;AACtB,QAAA,KAAW,aAAa,UAAU,OAAO;AACzC,SAAO;;;CAIT,KAAK,MAAoB;AACvB,QAAA,KAAW,aAAa,QAAQ,KAAK;AACrC,SAAO;;;CAGT,EAAE,MAAoB;AACpB,QAAA,KAAW,aAAa,QAAQ,KAAK;AACrC,SAAO;;;CAIT,aAAa,OAAqB;AAChC,QAAA,KAAW,aAAa,gBAAgB,OAAO,MAAM,CAAC;AACtD,SAAO;;;CAGT,GAAG,OAAqB;AACtB,QAAA,KAAW,aAAa,gBAAgB,OAAO,MAAM,CAAC;AACtD,SAAO;;;CAIT,WAAW,GAAG,YAA4B;AACxC,QAAA,KAAW,UAAU,IAAI,GAAG,WAAW;AACvC,SAAO;;;CAGT,GAAG,GAAG,YAA4B;AAChC,QAAA,KAAW,UAAU,IAAI,GAAG,WAAW;AACvC,SAAO;;;CAIT,UAAU,MAAc,OAAqB;AAC3C,QAAA,KAAW,aAAa,MAAM,MAAM;AACpC,SAAO;;;CAGT,EAAE,MAAc,OAAqB;AACnC,QAAA,KAAW,aAAa,MAAM,MAAM;AACpC,SAAO;;CAGT,cAAwB,EAAE;CAC1B,oBAA0B;AACxB,MAAI,MAAA,WAAiB,SAAS,EAC5B,OAAA,KAAW,aAAa,aAAa,MAAA,WAAiB,KAAK,IAAI,CAAC;;;CAIpE,UAAU,GAAW,GAAiB;AACpC,QAAA,WAAiB,K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KAAK,eAAe;EACpB,KAAK,eAAe;EACpB,KAAK,eAAe;AAClB,YAAS,MAAM,aAAa;AAC5B;;AAGJ,QAAO;;;;;;;;;AChIT,SAAgB,mBACd,UACA,UACA,OACA,QAC+B;CAC/B,MAAM,SAAS,QAAQ;CACvB,MAAM,SAAS,SAAS;AACxB,QAAO,mBAAmB,OAAO,CAAC,EAChC,KACA,OACG,WAAW,UACZ,OACC,WAAW,UACZ,OACC,WAAW,UACZ,OACC,WAAW,UACZ,MACA,WACA,OACC,WAAW,UACZ,IACH;;;;;;AC3BH,MAAa,yBAAyB,UAAkB;CACtD,MAAM,MACJ,SAAS,IAAI,QAAQ,oBAAoB,EAAE,QAAQ;AACrD,KAAI,MAAM,iBACR,QAAO,eAAe;UACb,MAAM,kBACf,QAAO,eAAe;UACb,MAAM,iBACf,QAAO,eAAe;UACb,MAAM,kBACf,QAAO,eAAe;UACb,MAAM,kBACf,QAAO,eAAe;UACb,MAAM,kBACf,QAAO,eAAe;UACb,MAAM,kBACf,QAAO,eAAe;UACb,MAAM,kBACf,QAAO,eAAe;KAEtB,QAAO,eAAe;;;;ACT1B,MAAa,MAAM;CACjB,QAAQ;CAEA;CACI;CACH;CACI;CACN;CACD;CACA;CACiB;CACA;CACvB,KAAKC;CACC;CACW;CACG;CAEG;CACxB"}
|
package/package.json
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@chocbite/ts-lib-svg",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "Library to help with svg generation in typescript",
|
|
5
5
|
"author": "chocolateandmilkwin",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"type": "module",
|
|
8
8
|
"private": false,
|
|
9
|
-
"module": "./dist/index.
|
|
10
|
-
"types": "./dist/index.d.
|
|
9
|
+
"module": "./dist/index.mjs",
|
|
10
|
+
"types": "./dist/index.d.mts",
|
|
11
11
|
"publishConfig": {
|
|
12
12
|
"access": "public"
|
|
13
13
|
},
|
|
14
14
|
"exports": {
|
|
15
15
|
".": {
|
|
16
|
-
"import": "./dist/index.
|
|
17
|
-
"types": "./dist/index.d.
|
|
16
|
+
"import": "./dist/index.mjs",
|
|
17
|
+
"types": "./dist/index.d.mts"
|
|
18
18
|
}
|
|
19
19
|
},
|
|
20
20
|
"files": [
|
|
@@ -22,21 +22,25 @@
|
|
|
22
22
|
],
|
|
23
23
|
"scripts": {
|
|
24
24
|
"dev": "vite --port 9952",
|
|
25
|
-
"build": "
|
|
25
|
+
"build": "tsdown src/index.ts",
|
|
26
26
|
"lint": "eslint src/**/*.ts",
|
|
27
27
|
"test": "vitest run",
|
|
28
|
-
"
|
|
28
|
+
"check-trials": "tsc --noEmit",
|
|
29
|
+
"prepublishOnly": "npm run check-trials && npm run build",
|
|
30
|
+
"upgrades": "npx npm-check-updates --target semver",
|
|
31
|
+
"upgrade": "npx npm-check-updates --target semver -u && npm install"
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"typescript": "^6.0.2",
|
|
35
|
+
"tsdown": "^0.21.7",
|
|
36
|
+
"vite": "^8.0.3",
|
|
37
|
+
"vitest": "^4.1.2",
|
|
38
|
+
"eslint": "^10.2.0",
|
|
39
|
+
"typescript-eslint": "^8.58.0",
|
|
40
|
+
"@vitest/browser-playwright": "^4.1.2",
|
|
41
|
+
"@chocbite/ts-supplement-eslint": "^1.0.0"
|
|
29
42
|
},
|
|
30
43
|
"dependencies": {
|
|
31
44
|
"@chocbite/ts-lib-math": "1.0.0"
|
|
32
|
-
},
|
|
33
|
-
"devDependencies": {
|
|
34
|
-
"typescript": "^5.0.0",
|
|
35
|
-
"tsup": "^8.0.0",
|
|
36
|
-
"vite": "^7.0.0",
|
|
37
|
-
"vitest": "^4.0.0",
|
|
38
|
-
"eslint": "^9.0.0",
|
|
39
|
-
"typescript-eslint": "^8.0.0",
|
|
40
|
-
"@vitest/browser-playwright": "^4.0.0"
|
|
41
45
|
}
|
|
42
46
|
}
|
package/dist/index.js
DELETED
|
@@ -1,312 +0,0 @@
|
|
|
1
|
-
// src/primitives/ellipse.ts
|
|
2
|
-
import { degrees_to_radians } from "@chocbite/ts-lib-math";
|
|
3
|
-
|
|
4
|
-
// src/attributes.ts
|
|
5
|
-
var SVGAttributes = class {
|
|
6
|
-
elem;
|
|
7
|
-
constructor(elem) {
|
|
8
|
-
this.elem = elem;
|
|
9
|
-
}
|
|
10
|
-
/** Sets the stroke color of the SVG element */
|
|
11
|
-
stroke(stroke) {
|
|
12
|
-
this.elem.setAttribute("stroke", stroke);
|
|
13
|
-
return this;
|
|
14
|
-
}
|
|
15
|
-
/** Sets the stroke color of the SVG element */
|
|
16
|
-
s(stroke) {
|
|
17
|
-
this.elem.setAttribute("stroke", stroke);
|
|
18
|
-
return this;
|
|
19
|
-
}
|
|
20
|
-
/** Sets the fill color of the SVG element */
|
|
21
|
-
fill(fill) {
|
|
22
|
-
this.elem.setAttribute("fill", fill);
|
|
23
|
-
return this;
|
|
24
|
-
}
|
|
25
|
-
/** Sets the fill color of the SVG element */
|
|
26
|
-
f(fill) {
|
|
27
|
-
this.elem.setAttribute("fill", fill);
|
|
28
|
-
return this;
|
|
29
|
-
}
|
|
30
|
-
/** Sets the stroke width of the SVG element */
|
|
31
|
-
stroke_width(width) {
|
|
32
|
-
this.elem.setAttribute("stroke-width", String(width));
|
|
33
|
-
return this;
|
|
34
|
-
}
|
|
35
|
-
/** Sets the stroke width of the SVG element */
|
|
36
|
-
sw(width) {
|
|
37
|
-
this.elem.setAttribute("stroke-width", String(width));
|
|
38
|
-
return this;
|
|
39
|
-
}
|
|
40
|
-
/** Adds one or more class names to the SVG element */
|
|
41
|
-
class_list(...class_name) {
|
|
42
|
-
this.elem.classList.add(...class_name);
|
|
43
|
-
return this;
|
|
44
|
-
}
|
|
45
|
-
/** Adds one or more class names to the SVG element */
|
|
46
|
-
cl(...class_name) {
|
|
47
|
-
this.elem.classList.add(...class_name);
|
|
48
|
-
return this;
|
|
49
|
-
}
|
|
50
|
-
/** Sets an arbitrary attribute on the SVG element */
|
|
51
|
-
attribute(name, value) {
|
|
52
|
-
this.elem.setAttribute(name, value);
|
|
53
|
-
return this;
|
|
54
|
-
}
|
|
55
|
-
/** Sets an arbitrary attribute on the SVG element */
|
|
56
|
-
a(name, value) {
|
|
57
|
-
this.elem.setAttribute(name, value);
|
|
58
|
-
return this;
|
|
59
|
-
}
|
|
60
|
-
};
|
|
61
|
-
|
|
62
|
-
// src/shared.ts
|
|
63
|
-
var NAME_SPACE = "http://www.w3.org/2000/svg";
|
|
64
|
-
|
|
65
|
-
// src/primitives/shared.ts
|
|
66
|
-
function create_svg_element(name) {
|
|
67
|
-
return new SVGAttributes(
|
|
68
|
-
document.createElementNS(NAME_SPACE, name)
|
|
69
|
-
);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
// src/primitives/ellipse.ts
|
|
73
|
-
function ellipse(center_x, center_y, radius_x, radius_y) {
|
|
74
|
-
return create_svg_element("ellipse").a("cx", String(center_x)).a("cy", String(center_y)).a("rx", String(radius_x)).a("ry", String(radius_y));
|
|
75
|
-
}
|
|
76
|
-
function ellipse_arc(center_x, center_y, radius_x, radius_y, start_angle, end_angle) {
|
|
77
|
-
const start_radian = degrees_to_radians(start_angle);
|
|
78
|
-
end_angle = degrees_to_radians(end_angle - start_angle);
|
|
79
|
-
const s_x = radius_x * Math.cos(start_radian) + center_x;
|
|
80
|
-
const s_y = radius_y * Math.sin(start_radian) + center_y;
|
|
81
|
-
const e_x = radius_x * Math.cos(start_radian + end_angle) + center_x;
|
|
82
|
-
const e_y = radius_y * Math.sin(start_radian + end_angle) + center_y;
|
|
83
|
-
const f_a = end_angle > Math.PI ? 1 : 0;
|
|
84
|
-
const f_s = end_angle > 0 ? 1 : 0;
|
|
85
|
-
return create_svg_element("path").a(
|
|
86
|
-
"d",
|
|
87
|
-
`M ${s_x} ${s_y} A ${radius_x} ${radius_y} 0 ${f_a} ${f_s} ${e_x} ${e_y}`
|
|
88
|
-
);
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
// src/primitives/circle.ts
|
|
92
|
-
function circle(center_x, center_y, radius) {
|
|
93
|
-
const circle2 = create_svg_element("circle");
|
|
94
|
-
circle2.a("cx", String(center_x));
|
|
95
|
-
circle2.a("cy", String(center_y));
|
|
96
|
-
circle2.a("r", String(radius));
|
|
97
|
-
return circle2;
|
|
98
|
-
}
|
|
99
|
-
function circle_arc(center_x, center_y, radius, start_angle, end_angle) {
|
|
100
|
-
return ellipse_arc(
|
|
101
|
-
center_x,
|
|
102
|
-
center_y,
|
|
103
|
-
radius,
|
|
104
|
-
radius,
|
|
105
|
-
start_angle,
|
|
106
|
-
end_angle
|
|
107
|
-
);
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
// src/primitives/group.ts
|
|
111
|
-
function group() {
|
|
112
|
-
return create_svg_element("g");
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
// src/primitives/line.ts
|
|
116
|
-
function line(start_x, start_y, end_x, end_y) {
|
|
117
|
-
return create_svg_element("line").a("x1", String(start_x)).a("y1", String(start_y)).a("x2", String(end_x)).a("y2", String(end_y));
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
// src/primitives/path.ts
|
|
121
|
-
function path(path2) {
|
|
122
|
-
return create_svg_element("path").a("d", path2);
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
// src/primitives/rectangle.ts
|
|
126
|
-
function rectangle_from_center(center_x, center_y, width, height, corner_radius) {
|
|
127
|
-
return create_svg_element("rect").a("x", String(center_x - width / 2)).a("y", String(center_y - height / 2)).a("width", String(width)).a("height", String(height)).a("rx", String(corner_radius));
|
|
128
|
-
}
|
|
129
|
-
function rectangle_from_corner(start_x, start_y, width, height, corner_radius) {
|
|
130
|
-
return create_svg_element("rect").a("x", String(start_x)).a("y", String(start_y)).a("width", String(width)).a("height", String(height)).a("rx", String(corner_radius));
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
// src/primitives/svg.ts
|
|
134
|
-
function svg(width, height, viewbox = `0 0 ${width} ${height}`) {
|
|
135
|
-
return create_svg_element("svg").a("width", String(width)).a("height", String(height)).a("viewBox", String(viewbox));
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
// src/util/anchorPoint.ts
|
|
139
|
-
var SVGAnchorPoint = {
|
|
140
|
-
bottomLeft: 0,
|
|
141
|
-
middleLeft: 1,
|
|
142
|
-
topLeft: 2,
|
|
143
|
-
topCenter: 3,
|
|
144
|
-
topRight: 4,
|
|
145
|
-
middleRight: 5,
|
|
146
|
-
bottomRight: 6,
|
|
147
|
-
bottomCenter: 7,
|
|
148
|
-
middleCenter: 8
|
|
149
|
-
};
|
|
150
|
-
|
|
151
|
-
// src/primitives/text.ts
|
|
152
|
-
function text(x, y, text2, size, anchor) {
|
|
153
|
-
const text_elem = create_svg_element("text").a("x", String(x)).a("y", String(y)).a("font-size", String(size));
|
|
154
|
-
text_elem.elem.innerHTML = text2;
|
|
155
|
-
switch (anchor) {
|
|
156
|
-
case SVGAnchorPoint.bottomLeft:
|
|
157
|
-
case SVGAnchorPoint.middleLeft:
|
|
158
|
-
case SVGAnchorPoint.topLeft: {
|
|
159
|
-
text_elem.a("text-anchor", "start");
|
|
160
|
-
break;
|
|
161
|
-
}
|
|
162
|
-
case SVGAnchorPoint.topCenter:
|
|
163
|
-
case SVGAnchorPoint.bottomCenter:
|
|
164
|
-
case SVGAnchorPoint.middleCenter: {
|
|
165
|
-
text_elem.a("text-anchor", "middle");
|
|
166
|
-
break;
|
|
167
|
-
}
|
|
168
|
-
case SVGAnchorPoint.topRight:
|
|
169
|
-
case SVGAnchorPoint.middleRight:
|
|
170
|
-
case SVGAnchorPoint.bottomRight: {
|
|
171
|
-
text_elem.a("text-anchor", "end");
|
|
172
|
-
break;
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
switch (anchor) {
|
|
176
|
-
case SVGAnchorPoint.bottomLeft:
|
|
177
|
-
case SVGAnchorPoint.bottomRight:
|
|
178
|
-
case SVGAnchorPoint.bottomCenter: {
|
|
179
|
-
text_elem.a("dominant-baseline", "auto");
|
|
180
|
-
break;
|
|
181
|
-
}
|
|
182
|
-
case SVGAnchorPoint.middleLeft:
|
|
183
|
-
case SVGAnchorPoint.middleRight:
|
|
184
|
-
case SVGAnchorPoint.middleCenter: {
|
|
185
|
-
text_elem.a("dominant-baseline", "central");
|
|
186
|
-
break;
|
|
187
|
-
}
|
|
188
|
-
case SVGAnchorPoint.topLeft:
|
|
189
|
-
case SVGAnchorPoint.topCenter:
|
|
190
|
-
case SVGAnchorPoint.topRight: {
|
|
191
|
-
text_elem.a("dominant-baseline", "hanging");
|
|
192
|
-
break;
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
return text_elem;
|
|
196
|
-
}
|
|
197
|
-
function multi_line_text(x, y, width, height, text2, size, anchor) {
|
|
198
|
-
const text_element = create_svg_element("foreignObject");
|
|
199
|
-
const text_div = text_element.elem.appendChild(document.createElement("div"));
|
|
200
|
-
text_element.a("width", String(width));
|
|
201
|
-
text_element.a("height", String(height));
|
|
202
|
-
text_element.a("x", String(x));
|
|
203
|
-
text_element.a("y", String(y));
|
|
204
|
-
text_div.style.fontSize = size + "px";
|
|
205
|
-
text_div.style.width = "100%";
|
|
206
|
-
text_div.style.height = "100%";
|
|
207
|
-
text_div.style.display = "flex";
|
|
208
|
-
text_div.innerHTML = text2;
|
|
209
|
-
switch (anchor) {
|
|
210
|
-
case SVGAnchorPoint.bottomLeft:
|
|
211
|
-
case SVGAnchorPoint.middleLeft:
|
|
212
|
-
case SVGAnchorPoint.topLeft: {
|
|
213
|
-
text_div.style.textAlign = "start";
|
|
214
|
-
text_div.style.justifyContent = "flex-start";
|
|
215
|
-
break;
|
|
216
|
-
}
|
|
217
|
-
case SVGAnchorPoint.topCenter:
|
|
218
|
-
case SVGAnchorPoint.bottomCenter:
|
|
219
|
-
case SVGAnchorPoint.middleCenter: {
|
|
220
|
-
text_div.style.textAlign = "center";
|
|
221
|
-
text_div.style.justifyContent = "center";
|
|
222
|
-
break;
|
|
223
|
-
}
|
|
224
|
-
case SVGAnchorPoint.topRight:
|
|
225
|
-
case SVGAnchorPoint.middleRight:
|
|
226
|
-
case SVGAnchorPoint.bottomRight: {
|
|
227
|
-
text_div.style.textAlign = "end";
|
|
228
|
-
text_div.style.justifyContent = "flex-end";
|
|
229
|
-
break;
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
switch (anchor) {
|
|
233
|
-
case SVGAnchorPoint.bottomLeft:
|
|
234
|
-
case SVGAnchorPoint.bottomRight:
|
|
235
|
-
case SVGAnchorPoint.bottomCenter: {
|
|
236
|
-
text_div.style.alignItems = "flex-end";
|
|
237
|
-
break;
|
|
238
|
-
}
|
|
239
|
-
case SVGAnchorPoint.middleLeft:
|
|
240
|
-
case SVGAnchorPoint.middleRight:
|
|
241
|
-
case SVGAnchorPoint.middleCenter: {
|
|
242
|
-
text_div.style.alignItems = "center";
|
|
243
|
-
break;
|
|
244
|
-
}
|
|
245
|
-
case SVGAnchorPoint.topLeft:
|
|
246
|
-
case SVGAnchorPoint.topCenter:
|
|
247
|
-
case SVGAnchorPoint.topRight: {
|
|
248
|
-
text_div.style.alignItems = "flex-start";
|
|
249
|
-
break;
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
return text_element;
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
// src/primitives/triangle.ts
|
|
256
|
-
function isosceles_triangle(center_x, center_y, width, height) {
|
|
257
|
-
const half_w = width / 2;
|
|
258
|
-
const half_h = height / 2;
|
|
259
|
-
return create_svg_element("path").a(
|
|
260
|
-
"d",
|
|
261
|
-
"M" + (center_x - half_w) + "," + (center_y + half_h) + " " + (center_x + half_w) + "," + (center_y + half_h) + " " + center_x + "," + (center_y - half_h) + "Z"
|
|
262
|
-
);
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
// src/util/angleToAnchorPoint.ts
|
|
266
|
-
var angle_to_anchor_point = (angle) => {
|
|
267
|
-
const sec = angle >= 0 ? angle % 6.283185307179586 : -(angle % 6.283185307179586);
|
|
268
|
-
if (sec > 5.93411945678072) {
|
|
269
|
-
return SVGAnchorPoint.middleRight;
|
|
270
|
-
} else if (sec > 4.974188368183839) {
|
|
271
|
-
return SVGAnchorPoint.topRight;
|
|
272
|
-
} else if (sec > 4.45058959258554) {
|
|
273
|
-
return SVGAnchorPoint.topCenter;
|
|
274
|
-
} else if (sec > 3.490658503988659) {
|
|
275
|
-
return SVGAnchorPoint.topLeft;
|
|
276
|
-
} else if (sec > 2.792526803190927) {
|
|
277
|
-
return SVGAnchorPoint.middleLeft;
|
|
278
|
-
} else if (sec > 1.832595714594046) {
|
|
279
|
-
return SVGAnchorPoint.bottomLeft;
|
|
280
|
-
} else if (sec > 1.308996938995747) {
|
|
281
|
-
return SVGAnchorPoint.bottomCenter;
|
|
282
|
-
} else if (sec > 0.3490658503988659) {
|
|
283
|
-
return SVGAnchorPoint.bottomRight;
|
|
284
|
-
} else {
|
|
285
|
-
return SVGAnchorPoint.middleRight;
|
|
286
|
-
}
|
|
287
|
-
};
|
|
288
|
-
|
|
289
|
-
// src/index.ts
|
|
290
|
-
var svg2 = {
|
|
291
|
-
create: create_svg_element,
|
|
292
|
-
//Primitives
|
|
293
|
-
circle,
|
|
294
|
-
circle_arc,
|
|
295
|
-
ellipse,
|
|
296
|
-
ellipse_arc,
|
|
297
|
-
group,
|
|
298
|
-
line,
|
|
299
|
-
path,
|
|
300
|
-
rectangle_from_center,
|
|
301
|
-
rectangle_from_corner,
|
|
302
|
-
svg,
|
|
303
|
-
text,
|
|
304
|
-
multi_line_text,
|
|
305
|
-
isosceles_triangle,
|
|
306
|
-
//Utilities
|
|
307
|
-
angle_to_anchor_point
|
|
308
|
-
};
|
|
309
|
-
export {
|
|
310
|
-
SVGAnchorPoint,
|
|
311
|
-
svg2 as svg
|
|
312
|
-
};
|