svelte-asciiart 0.0.3 → 0.0.5
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/README.md +1 -0
- package/dist/AsciiArt.svelte +9 -0
- package/dist/AsciiArt.svelte.d.ts +2 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/utils.d.ts +15 -1
- package/dist/utils.js +22 -21
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -39,6 +39,7 @@ npm install svelte-asciiart
|
|
|
39
39
|
| `margin` | `number \| [number, number] \| [number, number, number, number]` | `0` | Margin around the frame in grid cells (top/right/bottom/left) |
|
|
40
40
|
| `frameClass` | `string` | `''` | CSS class for the frame `<rect>` |
|
|
41
41
|
| `svg` | `SVGSVGElement \| null` | bindable | Optionally bind the underlying `<svg>` element |
|
|
42
|
+
| `baseSize` | `number` | `50` | Pixels per viewBox unit for intrinsic SVG size (for exports) |
|
|
42
43
|
| `...rest` | `SVGAttributes<SVGSVGElement>` | - | All other SVG attributes are forwarded to the `<svg>` element |
|
|
43
44
|
|
|
44
45
|
## Grid Mode
|
package/dist/AsciiArt.svelte
CHANGED
|
@@ -15,6 +15,8 @@
|
|
|
15
15
|
margin?: Margin;
|
|
16
16
|
frameClass?: string;
|
|
17
17
|
svg?: SVGSVGElement | null;
|
|
18
|
+
/** Pixels per viewBox unit for intrinsic size. Default: 50 */
|
|
19
|
+
baseSize?: number;
|
|
18
20
|
}
|
|
19
21
|
|
|
20
22
|
let {
|
|
@@ -29,6 +31,7 @@
|
|
|
29
31
|
margin = 0,
|
|
30
32
|
frameClass = '',
|
|
31
33
|
svg = $bindable(),
|
|
34
|
+
baseSize = 50,
|
|
32
35
|
...rest
|
|
33
36
|
}: Props = $props();
|
|
34
37
|
|
|
@@ -79,12 +82,18 @@
|
|
|
79
82
|
const offsetY = $derived(parsedMargin.top * cellHeight);
|
|
80
83
|
|
|
81
84
|
const viewBox = $derived(`0 0 ${fmt(viewBoxWidth)} ${fmt(viewBoxHeight)}`);
|
|
85
|
+
|
|
86
|
+
// Intrinsic size for export (actual rendering uses style for responsiveness)
|
|
87
|
+
const intrinsicWidth = $derived(fmt(viewBoxWidth * baseSize));
|
|
88
|
+
const intrinsicHeight = $derived(fmt(viewBoxHeight * baseSize));
|
|
82
89
|
</script>
|
|
83
90
|
|
|
84
91
|
<svg
|
|
85
92
|
bind:this={svg}
|
|
86
93
|
{...rest}
|
|
87
94
|
{viewBox}
|
|
95
|
+
width={intrinsicWidth}
|
|
96
|
+
height={intrinsicHeight}
|
|
88
97
|
overflow="hidden"
|
|
89
98
|
preserveAspectRatio="xMinYMin meet"
|
|
90
99
|
xmlns="http://www.w3.org/2000/svg"
|
|
@@ -11,6 +11,8 @@ interface Props extends SVGAttributes<SVGSVGElement> {
|
|
|
11
11
|
margin?: Margin;
|
|
12
12
|
frameClass?: string;
|
|
13
13
|
svg?: SVGSVGElement | null;
|
|
14
|
+
/** Pixels per viewBox unit for intrinsic size. Default: 50 */
|
|
15
|
+
baseSize?: number;
|
|
14
16
|
}
|
|
15
17
|
declare const AsciiArt: import("svelte").Component<Props, {}, "svg">;
|
|
16
18
|
type AsciiArt = ReturnType<typeof AsciiArt>;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export { default as AsciiArt } from './AsciiArt.svelte';
|
|
2
|
-
export { exportSvg, exportSvgToPng, type ExportSvgOptions, type ExportPngOptions } from './utils.js';
|
|
2
|
+
export { exportSvg, exportSvgToPng, svgStringToPng, type ExportSvgOptions, type ExportPngOptions, type SvgStringToPngOptions } from './utils.js';
|
package/dist/index.js
CHANGED
package/dist/utils.d.ts
CHANGED
|
@@ -10,11 +10,25 @@ export interface ExportSvgOptions {
|
|
|
10
10
|
*/
|
|
11
11
|
export declare function exportSvg(svgEl: SVGSVGElement, options?: ExportSvgOptions): string;
|
|
12
12
|
export interface ExportPngOptions extends ExportSvgOptions {
|
|
13
|
-
/** Scale factor for the PNG. Default:
|
|
13
|
+
/** Scale factor for the PNG. Default: 1 */
|
|
14
14
|
scale?: number;
|
|
15
15
|
/** Output format. Default: 'dataUrl' */
|
|
16
16
|
output?: 'dataUrl' | 'blob';
|
|
17
17
|
}
|
|
18
|
+
export interface SvgStringToPngOptions {
|
|
19
|
+
/** Scale factor for the PNG. Default: 1 */
|
|
20
|
+
scale?: number;
|
|
21
|
+
/** Output format. Default: 'dataUrl' */
|
|
22
|
+
output?: 'dataUrl' | 'blob';
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Convert an SVG string to PNG.
|
|
26
|
+
* Uses the browser's computed dimensions from the loaded image.
|
|
27
|
+
*/
|
|
28
|
+
export declare function svgStringToPng(svgString: string, options: SvgStringToPngOptions & {
|
|
29
|
+
output: 'blob';
|
|
30
|
+
}): Promise<Blob>;
|
|
31
|
+
export declare function svgStringToPng(svgString: string, options?: SvgStringToPngOptions): Promise<string>;
|
|
18
32
|
/**
|
|
19
33
|
* Export an SVG element to PNG.
|
|
20
34
|
* Returns a data URL or Blob depending on options.
|
package/dist/utils.js
CHANGED
|
@@ -102,33 +102,29 @@ export function exportSvg(svgEl, options = {}) {
|
|
|
102
102
|
}
|
|
103
103
|
return new XMLSerializer().serializeToString(clone);
|
|
104
104
|
}
|
|
105
|
-
export async function
|
|
106
|
-
const { scale =
|
|
107
|
-
// Get the exported SVG with embedded styles
|
|
108
|
-
const svgString = exportSvg(svgEl, svgOptions);
|
|
109
|
-
// Parse viewBox to get dimensions
|
|
110
|
-
const viewBox = svgEl.getAttribute('viewBox');
|
|
111
|
-
if (!viewBox)
|
|
112
|
-
throw new Error('SVG must have a viewBox attribute');
|
|
113
|
-
const [, , vbWidth, vbHeight] = viewBox.split(/\s+/).map(Number);
|
|
114
|
-
// Use a reasonable base size (e.g., 100px per viewBox unit, then scale)
|
|
115
|
-
const baseSize = 100;
|
|
116
|
-
const width = Math.round(vbWidth * baseSize * scale);
|
|
117
|
-
const height = Math.round(vbHeight * baseSize * scale);
|
|
118
|
-
// Create canvas
|
|
119
|
-
const canvas = document.createElement('canvas');
|
|
120
|
-
canvas.width = width;
|
|
121
|
-
canvas.height = height;
|
|
122
|
-
const ctx = canvas.getContext('2d');
|
|
123
|
-
if (!ctx)
|
|
124
|
-
throw new Error('Could not get canvas 2d context');
|
|
125
|
-
// Create image from SVG
|
|
105
|
+
export async function svgStringToPng(svgString, options = {}) {
|
|
106
|
+
const { scale = 1, output = 'dataUrl' } = options;
|
|
126
107
|
const img = new Image();
|
|
127
108
|
const svgBlob = new Blob([svgString], { type: 'image/svg+xml;charset=utf-8' });
|
|
128
109
|
const url = URL.createObjectURL(svgBlob);
|
|
129
110
|
return new Promise((resolve, reject) => {
|
|
130
111
|
img.onload = () => {
|
|
131
112
|
URL.revokeObjectURL(url);
|
|
113
|
+
// Use browser's computed dimensions
|
|
114
|
+
const width = Math.round(img.naturalWidth * scale);
|
|
115
|
+
const height = Math.round(img.naturalHeight * scale);
|
|
116
|
+
if (width === 0 || height === 0) {
|
|
117
|
+
reject(new Error('SVG has zero dimensions - ensure it has width/height or viewBox'));
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
const canvas = document.createElement('canvas');
|
|
121
|
+
canvas.width = width;
|
|
122
|
+
canvas.height = height;
|
|
123
|
+
const ctx = canvas.getContext('2d');
|
|
124
|
+
if (!ctx) {
|
|
125
|
+
reject(new Error('Could not get canvas 2d context'));
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
132
128
|
ctx.drawImage(img, 0, 0, width, height);
|
|
133
129
|
if (output === 'blob') {
|
|
134
130
|
canvas.toBlob((blob) => {
|
|
@@ -149,3 +145,8 @@ export async function exportSvgToPng(svgEl, options = {}) {
|
|
|
149
145
|
img.src = url;
|
|
150
146
|
});
|
|
151
147
|
}
|
|
148
|
+
export async function exportSvgToPng(svgEl, options = {}) {
|
|
149
|
+
const { scale, output, ...svgOptions } = options;
|
|
150
|
+
const svgString = exportSvg(svgEl, svgOptions);
|
|
151
|
+
return svgStringToPng(svgString, { scale, output });
|
|
152
|
+
}
|