isodoodle 0.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/LICENSE +21 -0
- package/README.md +123 -0
- package/dist/index.d.ts +101 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +193 -0
- package/dist/index.js.map +1 -0
- package/package.json +33 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 owen-lacey
|
|
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,123 @@
|
|
|
1
|
+
# Isodoodle
|
|
2
|
+
|
|
3
|
+
A minimal TypeScript library for drawing SVGs on an isometric grid.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install isodoodle
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { isodoodle } from 'isodoodle';
|
|
15
|
+
|
|
16
|
+
// Draw an isometric diamond
|
|
17
|
+
const svg = isodoodle({ scale: 20, stroke: '#333' })
|
|
18
|
+
.ne(2).se(2).sw(2).nw(2).close()
|
|
19
|
+
.toSvgString();
|
|
20
|
+
|
|
21
|
+
// Insert into DOM
|
|
22
|
+
document.body.innerHTML = svg;
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## API
|
|
26
|
+
|
|
27
|
+
### Factory Function
|
|
28
|
+
|
|
29
|
+
```typescript
|
|
30
|
+
isodoodle(options?: IsoDoodleOptions): IsoDoodle
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Creates a new IsoDoodle instance.
|
|
34
|
+
|
|
35
|
+
### Options
|
|
36
|
+
|
|
37
|
+
| Option | Type | Default | Description |
|
|
38
|
+
|--------|------|---------|-------------|
|
|
39
|
+
| `scale` | number | 20 | Pixels per unit |
|
|
40
|
+
| `stroke` | string | '#000' | Stroke color |
|
|
41
|
+
| `strokeWidth` | number | 1 | Stroke width |
|
|
42
|
+
| `fill` | string | 'none' | Fill color |
|
|
43
|
+
| `width` | number | auto | SVG width |
|
|
44
|
+
| `height` | number | auto | SVG height |
|
|
45
|
+
| `padding` | number | 10 | Padding around drawing |
|
|
46
|
+
|
|
47
|
+
### Direction Methods
|
|
48
|
+
|
|
49
|
+
All direction methods accept an optional `distance` parameter (default: 1).
|
|
50
|
+
|
|
51
|
+
- `.n(distance?)` - Move North (up)
|
|
52
|
+
- `.ne(distance?)` - Move Northeast
|
|
53
|
+
- `.se(distance?)` - Move Southeast
|
|
54
|
+
- `.s(distance?)` - Move South (down)
|
|
55
|
+
- `.sw(distance?)` - Move Southwest
|
|
56
|
+
- `.nw(distance?)` - Move Northwest
|
|
57
|
+
|
|
58
|
+
### Path Control
|
|
59
|
+
|
|
60
|
+
- `.moveTo(x, y)` - Jump to position without drawing (in scaled units)
|
|
61
|
+
- `.close()` - Close current path
|
|
62
|
+
- `.path(options?)` - Start new path with optional style overrides
|
|
63
|
+
|
|
64
|
+
### Output Methods
|
|
65
|
+
|
|
66
|
+
- `.toSvgString()` - Returns SVG markup string
|
|
67
|
+
- `.toElement()` - Returns SVGSVGElement (browser only)
|
|
68
|
+
- `.toPathData()` - Returns just the path `d` attribute
|
|
69
|
+
|
|
70
|
+
## Examples
|
|
71
|
+
|
|
72
|
+
### Isometric Cube
|
|
73
|
+
|
|
74
|
+
```typescript
|
|
75
|
+
const cube = isodoodle({ scale: 30, stroke: '#333' })
|
|
76
|
+
// Top face
|
|
77
|
+
.moveTo(0, 0)
|
|
78
|
+
.ne(2).se(2).sw(2).nw(2).close()
|
|
79
|
+
// Left face
|
|
80
|
+
.path({ fill: '#ccc' })
|
|
81
|
+
.moveTo(0, 0)
|
|
82
|
+
.nw(2).s(2).se(2).n(2).close()
|
|
83
|
+
// Right face
|
|
84
|
+
.path({ fill: '#999' })
|
|
85
|
+
.moveTo(0, 0)
|
|
86
|
+
.ne(2).s(2).sw(2).n(2).close()
|
|
87
|
+
.toSvgString();
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### Multiple Shapes
|
|
91
|
+
|
|
92
|
+
```typescript
|
|
93
|
+
const shapes = isodoodle({ scale: 20 })
|
|
94
|
+
.ne(1).se(1).sw(1).nw(1).close()
|
|
95
|
+
.path({ stroke: 'red' })
|
|
96
|
+
.moveTo(3, 0)
|
|
97
|
+
.ne(1).se(1).sw(1).nw(1).close()
|
|
98
|
+
.toSvgString();
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## Isometric Geometry
|
|
102
|
+
|
|
103
|
+
The library uses standard isometric projection where the X and Y axes are at 30° angles from horizontal:
|
|
104
|
+
|
|
105
|
+
```
|
|
106
|
+
N
|
|
107
|
+
|
|
|
108
|
+
NW | NE
|
|
109
|
+
\ | /
|
|
110
|
+
\ | /
|
|
111
|
+
\|/
|
|
112
|
+
-------.-------
|
|
113
|
+
/|\
|
|
114
|
+
/ | \
|
|
115
|
+
/ | \
|
|
116
|
+
SW | SE
|
|
117
|
+
|
|
|
118
|
+
S
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
## License
|
|
122
|
+
|
|
123
|
+
MIT
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Isodoodle - A minimal library for drawing SVGs on an isometric grid
|
|
3
|
+
*/
|
|
4
|
+
/** Unit vectors for each direction in screen coordinates (Y positive downward) */
|
|
5
|
+
export declare const DIRECTIONS: {
|
|
6
|
+
readonly N: {
|
|
7
|
+
readonly x: 0;
|
|
8
|
+
readonly y: -1;
|
|
9
|
+
};
|
|
10
|
+
readonly S: {
|
|
11
|
+
readonly x: 0;
|
|
12
|
+
readonly y: 1;
|
|
13
|
+
};
|
|
14
|
+
readonly NE: {
|
|
15
|
+
readonly x: number;
|
|
16
|
+
readonly y: -0.5;
|
|
17
|
+
};
|
|
18
|
+
readonly SE: {
|
|
19
|
+
readonly x: number;
|
|
20
|
+
readonly y: 0.5;
|
|
21
|
+
};
|
|
22
|
+
readonly SW: {
|
|
23
|
+
readonly x: number;
|
|
24
|
+
readonly y: 0.5;
|
|
25
|
+
};
|
|
26
|
+
readonly NW: {
|
|
27
|
+
readonly x: number;
|
|
28
|
+
readonly y: -0.5;
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
export type Direction = keyof typeof DIRECTIONS;
|
|
32
|
+
export interface Point {
|
|
33
|
+
x: number;
|
|
34
|
+
y: number;
|
|
35
|
+
}
|
|
36
|
+
export interface IsoDoodleOptions {
|
|
37
|
+
/** Pixels per unit (default: 20) */
|
|
38
|
+
scale?: number;
|
|
39
|
+
/** Stroke color (default: '#000') */
|
|
40
|
+
stroke?: string;
|
|
41
|
+
/** Stroke width (default: 1) */
|
|
42
|
+
strokeWidth?: number;
|
|
43
|
+
/** Fill color (default: 'none') */
|
|
44
|
+
fill?: string;
|
|
45
|
+
/** SVG width (auto if omitted) */
|
|
46
|
+
width?: number;
|
|
47
|
+
/** SVG height (auto if omitted) */
|
|
48
|
+
height?: number;
|
|
49
|
+
/** Padding around the drawing (default: 10) */
|
|
50
|
+
padding?: number;
|
|
51
|
+
}
|
|
52
|
+
export interface PathOptions {
|
|
53
|
+
stroke?: string;
|
|
54
|
+
strokeWidth?: number;
|
|
55
|
+
fill?: string;
|
|
56
|
+
}
|
|
57
|
+
export declare class IsoDoodle {
|
|
58
|
+
private options;
|
|
59
|
+
private currentX;
|
|
60
|
+
private currentY;
|
|
61
|
+
private paths;
|
|
62
|
+
private currentPath;
|
|
63
|
+
private minX;
|
|
64
|
+
private maxX;
|
|
65
|
+
private minY;
|
|
66
|
+
private maxY;
|
|
67
|
+
constructor(options?: IsoDoodleOptions);
|
|
68
|
+
private step;
|
|
69
|
+
private updateBounds;
|
|
70
|
+
/** Move North (up) */
|
|
71
|
+
n(distance?: number): this;
|
|
72
|
+
/** Move South (down) */
|
|
73
|
+
s(distance?: number): this;
|
|
74
|
+
/** Move Northeast */
|
|
75
|
+
ne(distance?: number): this;
|
|
76
|
+
/** Move Southeast */
|
|
77
|
+
se(distance?: number): this;
|
|
78
|
+
/** Move Southwest */
|
|
79
|
+
sw(distance?: number): this;
|
|
80
|
+
/** Move Northwest */
|
|
81
|
+
nw(distance?: number): this;
|
|
82
|
+
/** Jump to absolute position without drawing using isometric coordinates (n, ne, nw) */
|
|
83
|
+
moveTo(n: number, ne: number, nw: number): this;
|
|
84
|
+
/** Move relative to current position without drawing using isometric coordinates (n, ne, nw) */
|
|
85
|
+
move(n: number, ne: number, nw: number): this;
|
|
86
|
+
/** Close current path */
|
|
87
|
+
close(): this;
|
|
88
|
+
/** Start a new path with optional style overrides */
|
|
89
|
+
path(options?: PathOptions): this;
|
|
90
|
+
private buildPathD;
|
|
91
|
+
/** Returns just the path `d` attribute for the current path */
|
|
92
|
+
toPathData(): string;
|
|
93
|
+
/** Returns SVG markup string */
|
|
94
|
+
toSvgString(): string;
|
|
95
|
+
/** Returns SVGSVGElement (browser only) */
|
|
96
|
+
toElement(): SVGSVGElement;
|
|
97
|
+
}
|
|
98
|
+
/** Factory function to create an IsoDoodle instance */
|
|
99
|
+
export declare function isodoodle(options?: IsoDoodleOptions): IsoDoodle;
|
|
100
|
+
export default isodoodle;
|
|
101
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAIH,kFAAkF;AAClF,eAAO,MAAM,UAAU;;;;;;;;;;;;;;;;;;;;;;;;;CAOb,CAAC;AAEX,MAAM,MAAM,SAAS,GAAG,MAAM,OAAO,UAAU,CAAC;AAEhD,MAAM,WAAW,KAAK;IACpB,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,EAAE,MAAM,CAAC;CACX;AAED,MAAM,WAAW,gBAAgB;IAC/B,oCAAoC;IACpC,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,qCAAqC;IACrC,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,gCAAgC;IAChC,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,mCAAmC;IACnC,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,kCAAkC;IAClC,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,mCAAmC;IACnC,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,+CAA+C;IAC/C,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED,MAAM,WAAW,WAAW;IAC1B,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,IAAI,CAAC,EAAE,MAAM,CAAC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ACvD,wBAAgB,SAAS,CAAC,OAAO,CAAC,EAAE,gBAAgB,GAAG,SAAS,CAE/D;AAED,eAAe,SAAS,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Isodoodle - A minimal library for drawing SVGs on an isometric grid
|
|
3
|
+
*/
|
|
4
|
+
const SQRT3_2 = Math.sqrt(3) / 2; // ≈ 0.866
|
|
5
|
+
/** Unit vectors for each direction in screen coordinates (Y positive downward) */
|
|
6
|
+
export const DIRECTIONS = {
|
|
7
|
+
N: { x: 0, y: -1 },
|
|
8
|
+
S: { x: 0, y: 1 },
|
|
9
|
+
NE: { x: SQRT3_2, y: -0.5 },
|
|
10
|
+
SE: { x: SQRT3_2, y: 0.5 },
|
|
11
|
+
SW: { x: -SQRT3_2, y: 0.5 },
|
|
12
|
+
NW: { x: -SQRT3_2, y: -0.5 },
|
|
13
|
+
};
|
|
14
|
+
export class IsoDoodle {
|
|
15
|
+
options;
|
|
16
|
+
currentX = 0;
|
|
17
|
+
currentY = 0;
|
|
18
|
+
paths = [];
|
|
19
|
+
currentPath;
|
|
20
|
+
minX = 0;
|
|
21
|
+
maxX = 0;
|
|
22
|
+
minY = 0;
|
|
23
|
+
maxY = 0;
|
|
24
|
+
constructor(options = {}) {
|
|
25
|
+
this.options = {
|
|
26
|
+
scale: options.scale ?? 20,
|
|
27
|
+
stroke: options.stroke ?? '#000',
|
|
28
|
+
strokeWidth: options.strokeWidth ?? 1,
|
|
29
|
+
fill: options.fill ?? 'none',
|
|
30
|
+
padding: options.padding ?? 10,
|
|
31
|
+
width: options.width,
|
|
32
|
+
height: options.height,
|
|
33
|
+
};
|
|
34
|
+
this.currentPath = {
|
|
35
|
+
segments: [],
|
|
36
|
+
options: {
|
|
37
|
+
stroke: this.options.stroke,
|
|
38
|
+
strokeWidth: this.options.strokeWidth,
|
|
39
|
+
fill: this.options.fill,
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
step(direction, distance, draw) {
|
|
44
|
+
const dir = DIRECTIONS[direction];
|
|
45
|
+
const dx = dir.x * distance * this.options.scale;
|
|
46
|
+
const dy = dir.y * distance * this.options.scale;
|
|
47
|
+
// Start a new path with M if this is the first segment
|
|
48
|
+
if (this.currentPath.segments.length === 0) {
|
|
49
|
+
this.currentPath.segments.push({ type: 'M', x: this.currentX, y: this.currentY });
|
|
50
|
+
}
|
|
51
|
+
this.currentX += dx;
|
|
52
|
+
this.currentY += dy;
|
|
53
|
+
if (draw) {
|
|
54
|
+
this.currentPath.segments.push({ type: 'L', x: this.currentX, y: this.currentY });
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
this.currentPath.segments.push({ type: 'M', x: this.currentX, y: this.currentY });
|
|
58
|
+
}
|
|
59
|
+
this.updateBounds();
|
|
60
|
+
return this;
|
|
61
|
+
}
|
|
62
|
+
updateBounds() {
|
|
63
|
+
this.minX = Math.min(this.minX, this.currentX);
|
|
64
|
+
this.maxX = Math.max(this.maxX, this.currentX);
|
|
65
|
+
this.minY = Math.min(this.minY, this.currentY);
|
|
66
|
+
this.maxY = Math.max(this.maxY, this.currentY);
|
|
67
|
+
}
|
|
68
|
+
/** Move North (up) */
|
|
69
|
+
n(distance = 1) {
|
|
70
|
+
return this.step('N', distance, true);
|
|
71
|
+
}
|
|
72
|
+
/** Move South (down) */
|
|
73
|
+
s(distance = 1) {
|
|
74
|
+
return this.step('S', distance, true);
|
|
75
|
+
}
|
|
76
|
+
/** Move Northeast */
|
|
77
|
+
ne(distance = 1) {
|
|
78
|
+
return this.step('NE', distance, true);
|
|
79
|
+
}
|
|
80
|
+
/** Move Southeast */
|
|
81
|
+
se(distance = 1) {
|
|
82
|
+
return this.step('SE', distance, true);
|
|
83
|
+
}
|
|
84
|
+
/** Move Southwest */
|
|
85
|
+
sw(distance = 1) {
|
|
86
|
+
return this.step('SW', distance, true);
|
|
87
|
+
}
|
|
88
|
+
/** Move Northwest */
|
|
89
|
+
nw(distance = 1) {
|
|
90
|
+
return this.step('NW', distance, true);
|
|
91
|
+
}
|
|
92
|
+
/** Jump to absolute position without drawing using isometric coordinates (n, ne, nw) */
|
|
93
|
+
moveTo(n, ne, nw) {
|
|
94
|
+
// Calculate screen position by combining direction vectors
|
|
95
|
+
// N: (0, -1), NE: (SQRT3_2, -0.5), NW: (-SQRT3_2, -0.5)
|
|
96
|
+
const x = (ne - nw) * SQRT3_2 * this.options.scale;
|
|
97
|
+
const y = (-n - 0.5 * (ne + nw)) * this.options.scale;
|
|
98
|
+
this.currentX = x;
|
|
99
|
+
this.currentY = y;
|
|
100
|
+
this.currentPath.segments.push({ type: 'M', x: this.currentX, y: this.currentY });
|
|
101
|
+
this.updateBounds();
|
|
102
|
+
return this;
|
|
103
|
+
}
|
|
104
|
+
/** Move relative to current position without drawing using isometric coordinates (n, ne, nw) */
|
|
105
|
+
move(n, ne, nw) {
|
|
106
|
+
// Calculate offset by combining direction vectors
|
|
107
|
+
const dx = (ne - nw) * SQRT3_2 * this.options.scale;
|
|
108
|
+
const dy = (-n - 0.5 * (ne + nw)) * this.options.scale;
|
|
109
|
+
this.currentX += dx;
|
|
110
|
+
this.currentY += dy;
|
|
111
|
+
this.currentPath.segments.push({ type: 'M', x: this.currentX, y: this.currentY });
|
|
112
|
+
this.updateBounds();
|
|
113
|
+
return this;
|
|
114
|
+
}
|
|
115
|
+
/** Close current path */
|
|
116
|
+
close() {
|
|
117
|
+
this.currentPath.segments.push({ type: 'Z' });
|
|
118
|
+
return this;
|
|
119
|
+
}
|
|
120
|
+
/** Start a new path with optional style overrides */
|
|
121
|
+
path(options) {
|
|
122
|
+
// Save current path if it has content
|
|
123
|
+
if (this.currentPath.segments.length > 0) {
|
|
124
|
+
this.paths.push(this.currentPath);
|
|
125
|
+
}
|
|
126
|
+
this.currentPath = {
|
|
127
|
+
segments: [],
|
|
128
|
+
options: {
|
|
129
|
+
stroke: options?.stroke ?? this.options.stroke,
|
|
130
|
+
strokeWidth: options?.strokeWidth ?? this.options.strokeWidth,
|
|
131
|
+
fill: options?.fill ?? this.options.fill,
|
|
132
|
+
},
|
|
133
|
+
};
|
|
134
|
+
return this;
|
|
135
|
+
}
|
|
136
|
+
buildPathD(segments) {
|
|
137
|
+
return segments
|
|
138
|
+
.map((seg) => {
|
|
139
|
+
switch (seg.type) {
|
|
140
|
+
case 'M':
|
|
141
|
+
return `M ${seg.x.toFixed(2)} ${seg.y.toFixed(2)}`;
|
|
142
|
+
case 'L':
|
|
143
|
+
return `L ${seg.x.toFixed(2)} ${seg.y.toFixed(2)}`;
|
|
144
|
+
case 'Z':
|
|
145
|
+
return 'Z';
|
|
146
|
+
}
|
|
147
|
+
})
|
|
148
|
+
.join(' ');
|
|
149
|
+
}
|
|
150
|
+
/** Returns just the path `d` attribute for the current path */
|
|
151
|
+
toPathData() {
|
|
152
|
+
return this.buildPathD(this.currentPath.segments);
|
|
153
|
+
}
|
|
154
|
+
/** Returns SVG markup string */
|
|
155
|
+
toSvgString() {
|
|
156
|
+
const padding = this.options.padding;
|
|
157
|
+
const width = this.options.width ?? this.maxX - this.minX + padding * 2;
|
|
158
|
+
const height = this.options.height ?? this.maxY - this.minY + padding * 2;
|
|
159
|
+
// Calculate offset to ensure all content is visible
|
|
160
|
+
const offsetX = -this.minX + padding;
|
|
161
|
+
const offsetY = -this.minY + padding;
|
|
162
|
+
// Collect all paths including current
|
|
163
|
+
const allPaths = [...this.paths];
|
|
164
|
+
if (this.currentPath.segments.length > 0) {
|
|
165
|
+
allPaths.push(this.currentPath);
|
|
166
|
+
}
|
|
167
|
+
const pathElements = allPaths
|
|
168
|
+
.map((p) => {
|
|
169
|
+
const d = this.buildPathD(p.segments.map((seg) => ({
|
|
170
|
+
...seg,
|
|
171
|
+
x: seg.x !== undefined ? seg.x + offsetX : undefined,
|
|
172
|
+
y: seg.y !== undefined ? seg.y + offsetY : undefined,
|
|
173
|
+
})));
|
|
174
|
+
return ` <path d="${d}" stroke="${p.options.stroke}" stroke-width="${p.options.strokeWidth}" fill="${p.options.fill}" />`;
|
|
175
|
+
})
|
|
176
|
+
.join('\n');
|
|
177
|
+
return `<svg xmlns="http://www.w3.org/2000/svg" width="${width}" height="${height}" viewBox="0 0 ${width} ${height}">
|
|
178
|
+
${pathElements}
|
|
179
|
+
</svg>`;
|
|
180
|
+
}
|
|
181
|
+
/** Returns SVGSVGElement (browser only) */
|
|
182
|
+
toElement() {
|
|
183
|
+
const parser = new DOMParser();
|
|
184
|
+
const doc = parser.parseFromString(this.toSvgString(), 'image/svg+xml');
|
|
185
|
+
return doc.documentElement;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
/** Factory function to create an IsoDoodle instance */
|
|
189
|
+
export function isodoodle(options) {
|
|
190
|
+
return new IsoDoodle(options);
|
|
191
|
+
}
|
|
192
|
+
export default isodoodle;
|
|
193
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,MAAM,OAAO,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,UAAU;AAE5C,kFAAkF;AAClF,MAAM,CAAC,MAAM,UAAU,GAAG;IACxB,CAAC,EAAE,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,EAAE;IAClB,CAAC,EAAE,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE;IACjB,EAAE,EAAE,EAAE,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,CAAC,GAAG,EAAE;IAC3B,EAAE,EAAE,EAAE,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,GAAG,EAAE;IAC1B,EAAE,EAAE,EAAE,CAAC,EAAE,CAAC,OAAO,EAAE,CAAC,EAAE,GAAG,EAAE;IAC3B,EAAE,EAAE,EAAE,CAAC,EAAE,CAAC,OA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AAmB,CAAC,CAAC,OAAO,CAAC,WAAW,WAAW,CAAC,CAAC,OAAO,CAAC,IAAI,MAAM,CAAC;QAC7H,CAAC,CAAC;aACD,IAAI,CAAC,IAAI,CAAC,CAAC;QAEd,OAAO,kDAAkD,KAAK,aAAa,MAAM,kBAAkB,KAAK,IAAI,MAAM;EACpH,YAAY;OACP,CAAC;IACN,CAAC;IAED,2CAA2C;IAC3C,SAAS;QACP,MAAM,MAAM,GAAG,IAAI,SAAS,EAAE,CAAC;QAC/B,MAAM,GAAG,GAAG,MAAM,CAAC,eAAe,CAAC,IAAI,CAAC,WAAW,EAAE,EAAE,eAAe,CAAC,CAAC;QACxE,OAAO,GAAG,CAAC,eAA2C,CAAC;IACzD,CAAC;CACF;AAED,uDAAuD;AACvD,MAAM,UAAU,SAAS,CAAC,OAA0B;IAClD,OAAO,IAAI,SAAS,CAAC,OAAO,CAAC,CAAC;AAChC,CAAC;AAED,eAAe,SAAS,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "isodoodle",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "A minimal TypeScript library for drawing SVGs on an isometric grid",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist"
|
|
16
|
+
],
|
|
17
|
+
"scripts": {
|
|
18
|
+
"build": "tsc",
|
|
19
|
+
"prepublishOnly": "npm run build"
|
|
20
|
+
},
|
|
21
|
+
"keywords": [
|
|
22
|
+
"svg",
|
|
23
|
+
"isometric",
|
|
24
|
+
"drawing",
|
|
25
|
+
"graphics",
|
|
26
|
+
"typescript"
|
|
27
|
+
],
|
|
28
|
+
"author": "owen-lacey",
|
|
29
|
+
"license": "MIT",
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"typescript": "^5.3.0"
|
|
32
|
+
}
|
|
33
|
+
}
|