locusing 0.1.2
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/LICENSE +21 -0
- package/README.md +147 -0
- package/dist/chunk-3XW6GBD6.mjs +445 -0
- package/dist/chunk-HNU2F3GX.mjs +586 -0
- package/dist/chunk-TQQRYA5H.mjs +58 -0
- package/dist/chunk-Y4M467JV.mjs +607 -0
- package/dist/diagram-Djxa_kGR.d.mts +1945 -0
- package/dist/diagram-Djxa_kGR.d.ts +1945 -0
- package/dist/export.d.mts +194 -0
- package/dist/export.d.ts +194 -0
- package/dist/export.js +528 -0
- package/dist/export.mjs +305 -0
- package/dist/index.d.mts +6883 -0
- package/dist/index.d.ts +6883 -0
- package/dist/index.global.js +79540 -0
- package/dist/index.js +15785 -0
- package/dist/index.mjs +14350 -0
- package/dist/transform-4WYIJSLH.mjs +23 -0
- package/dist/transform-7YKZIQ3I.mjs +22 -0
- package/package.json +117 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Yao Shen
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
# Locusing
|
|
2
|
+
|
|
3
|
+
A declarative graphics and animation library for mathematical visualization.
|
|
4
|
+
|
|
5
|
+
Locusing combines the elegance of [Manim](https://www.manim.community/) with the interactivity of [Diagramatics](https://github.com/ray-x/diagramatics), bringing beautiful mathematical animations to the web.
|
|
6
|
+
|
|
7
|
+
## Features
|
|
8
|
+
|
|
9
|
+
- **Declarative API** - Immutable, composable diagram primitives
|
|
10
|
+
- **Manim-style Animations** - 30+ built-in animations (Create, FadeIn, Transform, etc.)
|
|
11
|
+
- **Interactive Controls** - Sliders, draggable points, and constraints
|
|
12
|
+
- **Hand-drawn Style** - Rough.js integration for sketch-like graphics
|
|
13
|
+
- **Mathematical Tools** - Vectors, matrices, Bezier curves, coordinate systems
|
|
14
|
+
- **Geometry Constructions** - Compass-and-straightedge style constructions
|
|
15
|
+
|
|
16
|
+
## Packages
|
|
17
|
+
|
|
18
|
+
| Package | Description |
|
|
19
|
+
|:--------|:------------|
|
|
20
|
+
| `@wangyaoshen/core` | Core rendering engine with SVG and Rough.js support |
|
|
21
|
+
| `@wangyaoshen/animate` | Manim-style animation system powered by GSAP |
|
|
22
|
+
| `@wangyaoshen/math` | Mathematical utilities (vectors, matrices, curves) |
|
|
23
|
+
| `@wangyaoshen/geometry` | Coordinate systems and geometric constructions |
|
|
24
|
+
| `@wangyaoshen/interactive` | Interactive controls and constraints |
|
|
25
|
+
|
|
26
|
+
## Quick Start
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
# Install packages
|
|
30
|
+
pnpm add @wangyaoshen/core @wangyaoshen/animate @wangyaoshen/math
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
### Static Diagram
|
|
34
|
+
|
|
35
|
+
```typescript
|
|
36
|
+
import { square, circle, text, combine, renderToSVG } from '@wangyaoshen/core';
|
|
37
|
+
|
|
38
|
+
// Create shapes
|
|
39
|
+
const sq = square(100).fill('#EA580C');
|
|
40
|
+
const label = text('Hello').translate(0, 80);
|
|
41
|
+
|
|
42
|
+
// Combine and render
|
|
43
|
+
const diagram = combine(sq, label);
|
|
44
|
+
renderToSVG(diagram, document.getElementById('canvas'));
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### Animated Scene
|
|
48
|
+
|
|
49
|
+
```typescript
|
|
50
|
+
import { Scene, Create, FadeOut, Wait } from '@wangyaoshen/animate';
|
|
51
|
+
import { square, circle } from '@wangyaoshen/core';
|
|
52
|
+
|
|
53
|
+
class MyScene extends Scene {
|
|
54
|
+
construct() {
|
|
55
|
+
const sq = square(100).fill('#EA580C');
|
|
56
|
+
const c = circle(50).fill('#58C4DD');
|
|
57
|
+
|
|
58
|
+
this.play(Create(sq));
|
|
59
|
+
this.play(Create(c, { delay: 0.5 }));
|
|
60
|
+
this.wait(1);
|
|
61
|
+
this.play(FadeOut(sq), FadeOut(c));
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const scene = new MyScene(document.getElementById('canvas'));
|
|
66
|
+
scene.render();
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### Interactive Diagram
|
|
70
|
+
|
|
71
|
+
```typescript
|
|
72
|
+
import { Interactive } from '@wangyaoshen/interactive';
|
|
73
|
+
import { circle } from '@wangyaoshen/core';
|
|
74
|
+
|
|
75
|
+
const interactive = new Interactive(container);
|
|
76
|
+
|
|
77
|
+
interactive.slider({
|
|
78
|
+
min: 10,
|
|
79
|
+
max: 100,
|
|
80
|
+
value: 50,
|
|
81
|
+
onChange: (radius) => {
|
|
82
|
+
const c = circle(radius).fill('#EA580C');
|
|
83
|
+
interactive.update(c);
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
## Animation Library
|
|
89
|
+
|
|
90
|
+
### Creation Animations
|
|
91
|
+
- `Create` - Draw stroke animation
|
|
92
|
+
- `Write` - Text writing effect
|
|
93
|
+
- `GrowFromCenter` - Scale from center
|
|
94
|
+
- `DrawBorderThenFill` - Draw border then fill
|
|
95
|
+
|
|
96
|
+
### Fade Animations
|
|
97
|
+
- `FadeIn` / `FadeOut`
|
|
98
|
+
- `FadeInFrom` / `FadeOutTo`
|
|
99
|
+
|
|
100
|
+
### Transform Animations
|
|
101
|
+
- `Transform` - Morph between shapes
|
|
102
|
+
- `ReplacementTransform` - Replace with transform
|
|
103
|
+
- `Morphing` - SVG path morphing (requires GSAP MorphSVG)
|
|
104
|
+
|
|
105
|
+
### Movement Animations
|
|
106
|
+
- `Shift` - Relative movement
|
|
107
|
+
- `MoveTo` - Absolute movement
|
|
108
|
+
- `MoveAlongPath` - Follow a path
|
|
109
|
+
- `Rotate` / `Scale`
|
|
110
|
+
|
|
111
|
+
### Indication Animations
|
|
112
|
+
- `Indicate` - Highlight effect
|
|
113
|
+
- `Circumscribe` - Circle around
|
|
114
|
+
- `Wiggle` - Shake effect
|
|
115
|
+
- `Flash` - Flash effect
|
|
116
|
+
|
|
117
|
+
### Composition
|
|
118
|
+
- `Succession` - Play in sequence
|
|
119
|
+
- `LaggedStart` - Staggered start times
|
|
120
|
+
|
|
121
|
+
## Development
|
|
122
|
+
|
|
123
|
+
```bash
|
|
124
|
+
# Install dependencies
|
|
125
|
+
pnpm install
|
|
126
|
+
|
|
127
|
+
# Run playground
|
|
128
|
+
pnpm dev
|
|
129
|
+
|
|
130
|
+
# Build all packages
|
|
131
|
+
pnpm build
|
|
132
|
+
|
|
133
|
+
# Run tests
|
|
134
|
+
pnpm test
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
## Credits
|
|
138
|
+
|
|
139
|
+
- [GSAP](https://gsap.com/) - Animation engine
|
|
140
|
+
- [Rough.js](https://roughjs.com/) - Hand-drawn style graphics
|
|
141
|
+
- [KaTeX](https://katex.org/) - LaTeX rendering (playground)
|
|
142
|
+
- [Manim](https://www.manim.community/) - Animation API inspiration
|
|
143
|
+
- [Diagramatics](https://github.com/ray-x/diagramatics) - Interactive diagram inspiration
|
|
144
|
+
|
|
145
|
+
## License
|
|
146
|
+
|
|
147
|
+
MIT License - see [LICENSE](LICENSE) for details.
|
|
@@ -0,0 +1,445 @@
|
|
|
1
|
+
import {
|
|
2
|
+
__esm
|
|
3
|
+
} from "./chunk-TQQRYA5H.mjs";
|
|
4
|
+
|
|
5
|
+
// src/core/render-svg.ts
|
|
6
|
+
function ensureKatexStyles() {
|
|
7
|
+
if (katexStyleInjected || typeof document === "undefined") return;
|
|
8
|
+
if (document.querySelector('link[href*="katex"]') || document.querySelector("style[data-katex]")) {
|
|
9
|
+
katexStyleInjected = true;
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
const link = document.createElement("link");
|
|
13
|
+
link.rel = "stylesheet";
|
|
14
|
+
link.href = "https://cdn.jsdelivr.net/npm/katex@0.16.27/dist/katex.min.css";
|
|
15
|
+
link.crossOrigin = "anonymous";
|
|
16
|
+
document.head.appendChild(link);
|
|
17
|
+
katexStyleInjected = true;
|
|
18
|
+
}
|
|
19
|
+
function renderToSVG(diagram, container, options = {}) {
|
|
20
|
+
const { width = 800, height = 600, background, frame } = options;
|
|
21
|
+
ensureKatexStyles();
|
|
22
|
+
let svg;
|
|
23
|
+
if (container instanceof SVGSVGElement) {
|
|
24
|
+
svg = container;
|
|
25
|
+
} else {
|
|
26
|
+
svg = document.createElementNS(SVG_NS, "svg");
|
|
27
|
+
container.appendChild(svg);
|
|
28
|
+
}
|
|
29
|
+
svg.setAttribute("width", String(width));
|
|
30
|
+
svg.setAttribute("height", String(height));
|
|
31
|
+
while (svg.firstChild) {
|
|
32
|
+
svg.removeChild(svg.firstChild);
|
|
33
|
+
}
|
|
34
|
+
const frameConfig = frame ? typeof frame === "boolean" ? DEFAULT_FRAME : frame : null;
|
|
35
|
+
if (frameConfig) {
|
|
36
|
+
const { width: fw, height: fh } = frameConfig;
|
|
37
|
+
svg.setAttribute("viewBox", `${-fw / 2} ${-fh / 2} ${fw} ${fh}`);
|
|
38
|
+
if (background) {
|
|
39
|
+
const rect = document.createElementNS(SVG_NS, "rect");
|
|
40
|
+
rect.setAttribute("x", String(-fw / 2));
|
|
41
|
+
rect.setAttribute("y", String(-fh / 2));
|
|
42
|
+
rect.setAttribute("width", String(fw));
|
|
43
|
+
rect.setAttribute("height", String(fh));
|
|
44
|
+
rect.setAttribute("fill", background);
|
|
45
|
+
svg.appendChild(rect);
|
|
46
|
+
}
|
|
47
|
+
const defs = document.createElementNS(SVG_NS, "defs");
|
|
48
|
+
svg.appendChild(defs);
|
|
49
|
+
const flipGroup = document.createElementNS(SVG_NS, "g");
|
|
50
|
+
flipGroup.setAttribute("transform", "scale(1, -1)");
|
|
51
|
+
const element = renderShape(diagram.shape, true);
|
|
52
|
+
if (element) {
|
|
53
|
+
flipGroup.appendChild(element);
|
|
54
|
+
}
|
|
55
|
+
svg.appendChild(flipGroup);
|
|
56
|
+
} else {
|
|
57
|
+
svg.setAttribute("viewBox", `0 0 ${width} ${height}`);
|
|
58
|
+
if (background) {
|
|
59
|
+
const rect = document.createElementNS(SVG_NS, "rect");
|
|
60
|
+
rect.setAttribute("width", "100%");
|
|
61
|
+
rect.setAttribute("height", "100%");
|
|
62
|
+
rect.setAttribute("fill", background);
|
|
63
|
+
svg.appendChild(rect);
|
|
64
|
+
}
|
|
65
|
+
const defs = document.createElementNS(SVG_NS, "defs");
|
|
66
|
+
svg.appendChild(defs);
|
|
67
|
+
const element = renderShape(diagram.shape, false);
|
|
68
|
+
if (element) {
|
|
69
|
+
svg.appendChild(element);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return svg;
|
|
73
|
+
}
|
|
74
|
+
function toSVGString(diagram, options = {}) {
|
|
75
|
+
const { width = 800, height = 600, background, frame } = options;
|
|
76
|
+
const frameConfig = frame ? typeof frame === "boolean" ? DEFAULT_FRAME : frame : null;
|
|
77
|
+
if (frameConfig) {
|
|
78
|
+
const { width: fw, height: fh } = frameConfig;
|
|
79
|
+
const viewBox = `${-fw / 2} ${-fh / 2} ${fw} ${fh}`;
|
|
80
|
+
let content2 = "";
|
|
81
|
+
if (background) {
|
|
82
|
+
content2 += `<rect x="${-fw / 2}" y="${-fh / 2}" width="${fw}" height="${fh}" fill="${background}"/>`;
|
|
83
|
+
}
|
|
84
|
+
content2 += `<g transform="scale(1,-1)">${shapeToSVGString(diagram.shape)}</g>`;
|
|
85
|
+
return `<svg xmlns="${SVG_NS}" width="${width}" height="${height}" viewBox="${viewBox}">${content2}</svg>`;
|
|
86
|
+
}
|
|
87
|
+
let content = "";
|
|
88
|
+
if (background) {
|
|
89
|
+
content += `<rect width="100%" height="100%" fill="${background}"/>`;
|
|
90
|
+
}
|
|
91
|
+
content += shapeToSVGString(diagram.shape);
|
|
92
|
+
return `<svg xmlns="${SVG_NS}" width="${width}" height="${height}" viewBox="0 0 ${width} ${height}">${content}</svg>`;
|
|
93
|
+
}
|
|
94
|
+
function renderTexShape(shape, frameMode = false) {
|
|
95
|
+
if (frameMode) {
|
|
96
|
+
const g = document.createElementNS(SVG_NS, "g");
|
|
97
|
+
const matrix = shape.transform;
|
|
98
|
+
const a = matrix[0], c = matrix[1], tx = matrix[2];
|
|
99
|
+
const b = matrix[3], d = matrix[4], ty = matrix[5];
|
|
100
|
+
const fx = a * shape.x + c * shape.y + tx;
|
|
101
|
+
const fy = b * shape.x + d * shape.y + ty;
|
|
102
|
+
const scale = 16 / 800;
|
|
103
|
+
g.setAttribute("transform", `translate(${fx}, ${-fy}) scale(${scale}, ${-scale})`);
|
|
104
|
+
const foreignObject = document.createElementNS(SVG_NS, "foreignObject");
|
|
105
|
+
const pixelWidth = 1e3;
|
|
106
|
+
const pixelHeight = 200;
|
|
107
|
+
foreignObject.setAttribute("x", String(-pixelWidth / 2));
|
|
108
|
+
foreignObject.setAttribute("y", String(-pixelHeight / 2));
|
|
109
|
+
foreignObject.setAttribute("width", String(pixelWidth));
|
|
110
|
+
foreignObject.setAttribute("height", String(pixelHeight));
|
|
111
|
+
foreignObject.setAttribute("overflow", "visible");
|
|
112
|
+
const div = document.createElementNS(XHTML_NS, "div");
|
|
113
|
+
div.setAttribute("xmlns", XHTML_NS);
|
|
114
|
+
div.style.fontSize = `${shape.fontSize}px`;
|
|
115
|
+
div.style.color = shape.color;
|
|
116
|
+
div.style.display = "flex";
|
|
117
|
+
div.style.justifyContent = "center";
|
|
118
|
+
div.style.alignItems = "center";
|
|
119
|
+
div.style.width = `${pixelWidth}px`;
|
|
120
|
+
div.style.height = `${pixelHeight}px`;
|
|
121
|
+
div.style.whiteSpace = "nowrap";
|
|
122
|
+
div.innerHTML = shape.html;
|
|
123
|
+
foreignObject.appendChild(div);
|
|
124
|
+
g.appendChild(foreignObject);
|
|
125
|
+
return g;
|
|
126
|
+
} else {
|
|
127
|
+
const foreignObject = document.createElementNS(SVG_NS, "foreignObject");
|
|
128
|
+
foreignObject.setAttribute("x", String(shape.x));
|
|
129
|
+
foreignObject.setAttribute("y", String(shape.y));
|
|
130
|
+
foreignObject.setAttribute("width", "1000");
|
|
131
|
+
foreignObject.setAttribute("height", "200");
|
|
132
|
+
foreignObject.setAttribute("overflow", "visible");
|
|
133
|
+
const div = document.createElementNS(XHTML_NS, "div");
|
|
134
|
+
div.setAttribute("xmlns", XHTML_NS);
|
|
135
|
+
div.style.fontSize = `${shape.fontSize}px`;
|
|
136
|
+
div.style.color = shape.color;
|
|
137
|
+
div.style.display = "inline-block";
|
|
138
|
+
div.style.whiteSpace = "nowrap";
|
|
139
|
+
div.innerHTML = shape.html;
|
|
140
|
+
foreignObject.appendChild(div);
|
|
141
|
+
return foreignObject;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
function renderShape(shape, frameMode = false) {
|
|
145
|
+
let element = null;
|
|
146
|
+
switch (shape.type) {
|
|
147
|
+
case "rect":
|
|
148
|
+
element = document.createElementNS(SVG_NS, "rect");
|
|
149
|
+
element.setAttribute("x", String(shape.x));
|
|
150
|
+
element.setAttribute("y", String(shape.y));
|
|
151
|
+
element.setAttribute("width", String(shape.width));
|
|
152
|
+
element.setAttribute("height", String(shape.height));
|
|
153
|
+
if (shape.rx) element.setAttribute("rx", String(shape.rx));
|
|
154
|
+
if (shape.ry) element.setAttribute("ry", String(shape.ry));
|
|
155
|
+
break;
|
|
156
|
+
case "circle":
|
|
157
|
+
element = document.createElementNS(SVG_NS, "circle");
|
|
158
|
+
element.setAttribute("cx", String(shape.cx));
|
|
159
|
+
element.setAttribute("cy", String(shape.cy));
|
|
160
|
+
element.setAttribute("r", String(shape.r));
|
|
161
|
+
break;
|
|
162
|
+
case "ellipse":
|
|
163
|
+
element = document.createElementNS(SVG_NS, "ellipse");
|
|
164
|
+
element.setAttribute("cx", String(shape.cx));
|
|
165
|
+
element.setAttribute("cy", String(shape.cy));
|
|
166
|
+
element.setAttribute("rx", String(shape.rx));
|
|
167
|
+
element.setAttribute("ry", String(shape.ry));
|
|
168
|
+
break;
|
|
169
|
+
case "line":
|
|
170
|
+
element = document.createElementNS(SVG_NS, "line");
|
|
171
|
+
element.setAttribute("x1", String(shape.x1));
|
|
172
|
+
element.setAttribute("y1", String(shape.y1));
|
|
173
|
+
element.setAttribute("x2", String(shape.x2));
|
|
174
|
+
element.setAttribute("y2", String(shape.y2));
|
|
175
|
+
break;
|
|
176
|
+
case "polyline":
|
|
177
|
+
element = document.createElementNS(SVG_NS, "polyline");
|
|
178
|
+
element.setAttribute("points", shape.points.map((p) => `${p[0]},${p[1]}`).join(" "));
|
|
179
|
+
break;
|
|
180
|
+
case "polygon":
|
|
181
|
+
element = document.createElementNS(SVG_NS, "polygon");
|
|
182
|
+
element.setAttribute("points", shape.points.map((p) => `${p[0]},${p[1]}`).join(" "));
|
|
183
|
+
break;
|
|
184
|
+
case "path":
|
|
185
|
+
element = document.createElementNS(SVG_NS, "path");
|
|
186
|
+
element.setAttribute("d", commandsToPath(shape.commands, frameMode));
|
|
187
|
+
break;
|
|
188
|
+
case "arc":
|
|
189
|
+
element = document.createElementNS(SVG_NS, "path");
|
|
190
|
+
element.setAttribute("d", arcToPath(shape.cx, shape.cy, shape.r, shape.startAngle, shape.endAngle));
|
|
191
|
+
break;
|
|
192
|
+
case "text":
|
|
193
|
+
element = document.createElementNS(SVG_NS, "text");
|
|
194
|
+
element.setAttribute("x", String(shape.x));
|
|
195
|
+
element.setAttribute("y", String(shape.y));
|
|
196
|
+
element.textContent = shape.content;
|
|
197
|
+
applyTextStyle(element, shape.style);
|
|
198
|
+
if (frameMode) {
|
|
199
|
+
const matrix = shape.transform;
|
|
200
|
+
const a = matrix[0], c = matrix[1], tx = matrix[2];
|
|
201
|
+
const b = matrix[3], d = matrix[4], ty = matrix[5];
|
|
202
|
+
const fx = a * shape.x + c * shape.y + tx;
|
|
203
|
+
const fy = b * shape.x + d * shape.y + ty;
|
|
204
|
+
element.setAttribute(
|
|
205
|
+
"transform",
|
|
206
|
+
`translate(${fx}, ${fy}) scale(1, -1) translate(${-fx}, ${-fy}) matrix(${a},${b},${c},${d},${tx},${ty})`
|
|
207
|
+
);
|
|
208
|
+
}
|
|
209
|
+
break;
|
|
210
|
+
case "tex":
|
|
211
|
+
element = renderTexShape(shape, frameMode);
|
|
212
|
+
break;
|
|
213
|
+
case "group":
|
|
214
|
+
element = document.createElementNS(SVG_NS, "g");
|
|
215
|
+
for (const child of shape.children) {
|
|
216
|
+
const childElement = renderShape(child, frameMode);
|
|
217
|
+
if (childElement) {
|
|
218
|
+
element.appendChild(childElement);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
break;
|
|
222
|
+
case "svg":
|
|
223
|
+
element = document.createElementNS(SVG_NS, "g");
|
|
224
|
+
const svgShape = shape;
|
|
225
|
+
const parser = new DOMParser();
|
|
226
|
+
const svgDoc = parser.parseFromString(svgShape.content, "image/svg+xml");
|
|
227
|
+
const svgElement = svgDoc.querySelector("svg");
|
|
228
|
+
if (svgElement) {
|
|
229
|
+
const scaleX = shape.width / svgShape.originalWidth;
|
|
230
|
+
const scaleY = shape.height / svgShape.originalHeight;
|
|
231
|
+
element.setAttribute("transform", `scale(${scaleX}, ${scaleY})`);
|
|
232
|
+
for (const child of Array.from(svgElement.childNodes)) {
|
|
233
|
+
element.appendChild(document.importNode(child, true));
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
break;
|
|
237
|
+
case "image":
|
|
238
|
+
element = document.createElementNS(SVG_NS, "image");
|
|
239
|
+
const imgShape = shape;
|
|
240
|
+
element.setAttribute("href", imgShape.src);
|
|
241
|
+
element.setAttribute("x", String(-shape.width / 2));
|
|
242
|
+
element.setAttribute("y", String(-shape.height / 2));
|
|
243
|
+
element.setAttribute("width", String(shape.width));
|
|
244
|
+
element.setAttribute("height", String(shape.height));
|
|
245
|
+
element.setAttribute("preserveAspectRatio", "xMidYMid meet");
|
|
246
|
+
break;
|
|
247
|
+
}
|
|
248
|
+
if (element) {
|
|
249
|
+
element.setAttribute("id", shape.id);
|
|
250
|
+
applyStyle(element, shape.style);
|
|
251
|
+
if (!(frameMode && (shape.type === "text" || shape.type === "tex"))) {
|
|
252
|
+
applyTransform(element, shape.transform);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
return element;
|
|
256
|
+
}
|
|
257
|
+
function shapeToSVGString(shape) {
|
|
258
|
+
const attrs = [`id="${shape.id}"`];
|
|
259
|
+
const style = styleToString(shape.style);
|
|
260
|
+
const transform = transformToString(shape.transform);
|
|
261
|
+
if (style) attrs.push(style);
|
|
262
|
+
if (transform) attrs.push(`transform="${transform}"`);
|
|
263
|
+
switch (shape.type) {
|
|
264
|
+
case "rect":
|
|
265
|
+
attrs.push(`x="${shape.x}" y="${shape.y}" width="${shape.width}" height="${shape.height}"`);
|
|
266
|
+
if (shape.rx) attrs.push(`rx="${shape.rx}"`);
|
|
267
|
+
if (shape.ry) attrs.push(`ry="${shape.ry}"`);
|
|
268
|
+
return `<rect ${attrs.join(" ")}/>`;
|
|
269
|
+
case "circle":
|
|
270
|
+
attrs.push(`cx="${shape.cx}" cy="${shape.cy}" r="${shape.r}"`);
|
|
271
|
+
return `<circle ${attrs.join(" ")}/>`;
|
|
272
|
+
case "ellipse":
|
|
273
|
+
attrs.push(`cx="${shape.cx}" cy="${shape.cy}" rx="${shape.rx}" ry="${shape.ry}"`);
|
|
274
|
+
return `<ellipse ${attrs.join(" ")}/>`;
|
|
275
|
+
case "line":
|
|
276
|
+
attrs.push(`x1="${shape.x1}" y1="${shape.y1}" x2="${shape.x2}" y2="${shape.y2}"`);
|
|
277
|
+
return `<line ${attrs.join(" ")}/>`;
|
|
278
|
+
case "polyline":
|
|
279
|
+
attrs.push(`points="${shape.points.map((p) => `${p[0]},${p[1]}`).join(" ")}"`);
|
|
280
|
+
return `<polyline ${attrs.join(" ")}/>`;
|
|
281
|
+
case "polygon":
|
|
282
|
+
attrs.push(`points="${shape.points.map((p) => `${p[0]},${p[1]}`).join(" ")}"`);
|
|
283
|
+
return `<polygon ${attrs.join(" ")}/>`;
|
|
284
|
+
case "path":
|
|
285
|
+
attrs.push(`d="${commandsToPath(shape.commands)}"`);
|
|
286
|
+
return `<path ${attrs.join(" ")}/>`;
|
|
287
|
+
case "arc":
|
|
288
|
+
attrs.push(`d="${arcToPath(shape.cx, shape.cy, shape.r, shape.startAngle, shape.endAngle)}"`);
|
|
289
|
+
return `<path ${attrs.join(" ")}/>`;
|
|
290
|
+
case "text":
|
|
291
|
+
const textStyle = textStyleToString(shape.style);
|
|
292
|
+
if (textStyle) attrs.push(textStyle);
|
|
293
|
+
attrs.push(`x="${shape.x}" y="${shape.y}"`);
|
|
294
|
+
return `<text ${attrs.join(" ")}>${escapeXML(shape.content)}</text>`;
|
|
295
|
+
case "tex":
|
|
296
|
+
const texShape = shape;
|
|
297
|
+
return `<foreignObject x="${texShape.x}" y="${texShape.y}" width="1000" height="200" overflow="visible"><div xmlns="${XHTML_NS}" style="font-size:${texShape.fontSize}px;color:${texShape.color};display:inline-block;white-space:nowrap">${texShape.html}</div></foreignObject>`;
|
|
298
|
+
case "group":
|
|
299
|
+
const children = shape.children.map(shapeToSVGString).join("");
|
|
300
|
+
return `<g ${attrs.join(" ")}>${children}</g>`;
|
|
301
|
+
case "svg":
|
|
302
|
+
const svgShapeStr = shape;
|
|
303
|
+
const scaleXStr = shape.width / svgShapeStr.originalWidth;
|
|
304
|
+
const scaleYStr = shape.height / svgShapeStr.originalHeight;
|
|
305
|
+
return `<g ${attrs.join(" ")} transform="scale(${scaleXStr}, ${scaleYStr})">${svgShapeStr.content}</g>`;
|
|
306
|
+
case "image":
|
|
307
|
+
const imgShapeStr = shape;
|
|
308
|
+
attrs.push(`href="${imgShapeStr.src}"`);
|
|
309
|
+
attrs.push(`x="${-shape.width / 2}" y="${-shape.height / 2}"`);
|
|
310
|
+
attrs.push(`width="${shape.width}" height="${shape.height}"`);
|
|
311
|
+
attrs.push('preserveAspectRatio="xMidYMid meet"');
|
|
312
|
+
return `<image ${attrs.join(" ")}/>`;
|
|
313
|
+
default:
|
|
314
|
+
return "";
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
function applyStyle(element, style) {
|
|
318
|
+
if (style.fill) element.setAttribute("fill", style.fill);
|
|
319
|
+
if (style.fillOpacity !== void 0) element.setAttribute("fill-opacity", String(style.fillOpacity));
|
|
320
|
+
if (style.stroke) element.setAttribute("stroke", style.stroke);
|
|
321
|
+
if (style.strokeWidth !== void 0) element.setAttribute("stroke-width", String(style.strokeWidth));
|
|
322
|
+
if (style.strokeOpacity !== void 0) element.setAttribute("stroke-opacity", String(style.strokeOpacity));
|
|
323
|
+
if (style.strokeDasharray) element.setAttribute("stroke-dasharray", style.strokeDasharray);
|
|
324
|
+
if (style.strokeLinecap) element.setAttribute("stroke-linecap", style.strokeLinecap);
|
|
325
|
+
if (style.strokeLinejoin) element.setAttribute("stroke-linejoin", style.strokeLinejoin);
|
|
326
|
+
if (style.opacity !== void 0) element.setAttribute("opacity", String(style.opacity));
|
|
327
|
+
}
|
|
328
|
+
function applyTextStyle(element, style) {
|
|
329
|
+
if (style.fontSize) element.setAttribute("font-size", String(style.fontSize));
|
|
330
|
+
if (style.fontFamily) element.setAttribute("font-family", style.fontFamily);
|
|
331
|
+
if (style.fontWeight) element.setAttribute("font-weight", String(style.fontWeight));
|
|
332
|
+
if (style.textAnchor) element.setAttribute("text-anchor", style.textAnchor);
|
|
333
|
+
if (style.dominantBaseline) element.setAttribute("dominant-baseline", style.dominantBaseline);
|
|
334
|
+
}
|
|
335
|
+
function applyTransform(element, matrix) {
|
|
336
|
+
const [a, b, c, d, e, f] = [
|
|
337
|
+
matrix[0],
|
|
338
|
+
matrix[3],
|
|
339
|
+
matrix[1],
|
|
340
|
+
matrix[4],
|
|
341
|
+
matrix[2],
|
|
342
|
+
matrix[5]
|
|
343
|
+
];
|
|
344
|
+
if (a === 1 && b === 0 && c === 0 && d === 1 && e === 0 && f === 0) {
|
|
345
|
+
return;
|
|
346
|
+
}
|
|
347
|
+
element.setAttribute("transform", `matrix(${a},${b},${c},${d},${e},${f})`);
|
|
348
|
+
}
|
|
349
|
+
function styleToString(style) {
|
|
350
|
+
const parts = [];
|
|
351
|
+
if (style.fill) parts.push(`fill="${style.fill}"`);
|
|
352
|
+
if (style.fillOpacity !== void 0) parts.push(`fill-opacity="${style.fillOpacity}"`);
|
|
353
|
+
if (style.stroke) parts.push(`stroke="${style.stroke}"`);
|
|
354
|
+
if (style.strokeWidth !== void 0) parts.push(`stroke-width="${style.strokeWidth}"`);
|
|
355
|
+
if (style.strokeOpacity !== void 0) parts.push(`stroke-opacity="${style.strokeOpacity}"`);
|
|
356
|
+
if (style.strokeDasharray) parts.push(`stroke-dasharray="${style.strokeDasharray}"`);
|
|
357
|
+
if (style.strokeLinecap) parts.push(`stroke-linecap="${style.strokeLinecap}"`);
|
|
358
|
+
if (style.strokeLinejoin) parts.push(`stroke-linejoin="${style.strokeLinejoin}"`);
|
|
359
|
+
if (style.opacity !== void 0) parts.push(`opacity="${style.opacity}"`);
|
|
360
|
+
return parts.join(" ");
|
|
361
|
+
}
|
|
362
|
+
function textStyleToString(style) {
|
|
363
|
+
const parts = [];
|
|
364
|
+
if (style.fontSize) parts.push(`font-size="${style.fontSize}"`);
|
|
365
|
+
if (style.fontFamily) parts.push(`font-family="${style.fontFamily}"`);
|
|
366
|
+
if (style.fontWeight) parts.push(`font-weight="${style.fontWeight}"`);
|
|
367
|
+
if (style.textAnchor) parts.push(`text-anchor="${style.textAnchor}"`);
|
|
368
|
+
if (style.dominantBaseline) parts.push(`dominant-baseline="${style.dominantBaseline}"`);
|
|
369
|
+
return parts.join(" ");
|
|
370
|
+
}
|
|
371
|
+
function transformToString(matrix) {
|
|
372
|
+
const [a, b, c, d, e, f] = [
|
|
373
|
+
matrix[0],
|
|
374
|
+
matrix[3],
|
|
375
|
+
matrix[1],
|
|
376
|
+
matrix[4],
|
|
377
|
+
matrix[2],
|
|
378
|
+
matrix[5]
|
|
379
|
+
];
|
|
380
|
+
if (a === 1 && b === 0 && c === 0 && d === 1 && e === 0 && f === 0) {
|
|
381
|
+
return "";
|
|
382
|
+
}
|
|
383
|
+
return `matrix(${a},${b},${c},${d},${e},${f})`;
|
|
384
|
+
}
|
|
385
|
+
function commandsToPath(commands, _frameMode = false) {
|
|
386
|
+
return commands.map((cmd) => {
|
|
387
|
+
switch (cmd.type) {
|
|
388
|
+
case "M":
|
|
389
|
+
return `M${cmd.x},${cmd.y}`;
|
|
390
|
+
case "L":
|
|
391
|
+
return `L${cmd.x},${cmd.y}`;
|
|
392
|
+
case "H":
|
|
393
|
+
return `H${cmd.x}`;
|
|
394
|
+
case "V":
|
|
395
|
+
return `V${cmd.y}`;
|
|
396
|
+
case "C":
|
|
397
|
+
return `C${cmd.x1},${cmd.y1} ${cmd.x2},${cmd.y2} ${cmd.x},${cmd.y}`;
|
|
398
|
+
case "S":
|
|
399
|
+
return `S${cmd.x2},${cmd.y2} ${cmd.x},${cmd.y}`;
|
|
400
|
+
case "Q":
|
|
401
|
+
return `Q${cmd.x1},${cmd.y1} ${cmd.x},${cmd.y}`;
|
|
402
|
+
case "T":
|
|
403
|
+
return `T${cmd.x},${cmd.y}`;
|
|
404
|
+
case "A": {
|
|
405
|
+
return `A${cmd.rx},${cmd.ry} ${cmd.angle} ${cmd.largeArc ? 1 : 0},${cmd.sweep ? 1 : 0} ${cmd.x},${cmd.y}`;
|
|
406
|
+
}
|
|
407
|
+
case "Z":
|
|
408
|
+
return "Z";
|
|
409
|
+
}
|
|
410
|
+
}).join(" ");
|
|
411
|
+
}
|
|
412
|
+
function arcToPath(cx, cy, r, startAngle, endAngle) {
|
|
413
|
+
const startX = cx + r * Math.cos(startAngle);
|
|
414
|
+
const startY = cy + r * Math.sin(startAngle);
|
|
415
|
+
const endX = cx + r * Math.cos(endAngle);
|
|
416
|
+
const endY = cy + r * Math.sin(endAngle);
|
|
417
|
+
const largeArc = Math.abs(endAngle - startAngle) > Math.PI ? 1 : 0;
|
|
418
|
+
const sweep = endAngle > startAngle ? 1 : 0;
|
|
419
|
+
return `M${startX},${startY} A${r},${r} 0 ${largeArc},${sweep} ${endX},${endY}`;
|
|
420
|
+
}
|
|
421
|
+
function escapeXML(str) {
|
|
422
|
+
if (typeof str !== "string") {
|
|
423
|
+
str = String(str != null ? str : "");
|
|
424
|
+
}
|
|
425
|
+
return str.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
|
|
426
|
+
}
|
|
427
|
+
var DEFAULT_FRAME, XHTML_NS, katexStyleInjected, SVG_NS;
|
|
428
|
+
var init_render_svg = __esm({
|
|
429
|
+
"src/core/render-svg.ts"() {
|
|
430
|
+
"use strict";
|
|
431
|
+
DEFAULT_FRAME = {
|
|
432
|
+
width: 16,
|
|
433
|
+
height: 9
|
|
434
|
+
};
|
|
435
|
+
XHTML_NS = "http://www.w3.org/1999/xhtml";
|
|
436
|
+
katexStyleInjected = false;
|
|
437
|
+
SVG_NS = "http://www.w3.org/2000/svg";
|
|
438
|
+
}
|
|
439
|
+
});
|
|
440
|
+
|
|
441
|
+
export {
|
|
442
|
+
renderToSVG,
|
|
443
|
+
toSVGString,
|
|
444
|
+
init_render_svg
|
|
445
|
+
};
|