@slithy/prim-interface 0.4.3 → 0.5.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/README.md CHANGED
@@ -113,9 +113,9 @@ interface StepResult {
113
113
 
114
114
  Higher-level (rasterize SVG at save time for crisp output):
115
115
 
116
- - **`saveRasterFromVector(svgString, width, height, format?, quality?, options?)`** — downloads PNG or JPEG rasterized from the SVG
117
- - **`copyRasterFromVector(svgString, width, height)`** — copies a PNG rasterized from the SVG to the clipboard
118
- - **`shareFromVector(svgString, width, height, options?)`** — shares a PNG rasterized from the SVG via the Web Share API
116
+ - **`saveRasterFromVector(svgString, width, height, format?, quality?, options?, background?)`** — downloads PNG or JPEG rasterized from the SVG. `background` fills the canvas before drawing: JPEG defaults to `'white'` (prevents black transparent areas); PNG defaults to transparent. Pass a `Background` value to override.
117
+ - **`copyRasterFromVector(svgString, width, height)`** — copies a transparent PNG rasterized from the SVG to the clipboard
118
+ - **`shareFromVector(svgString, width, height, options?)`** — shares a transparent PNG rasterized from the SVG via the Web Share API
119
119
 
120
120
  Lower-level (operate directly on a canvas or SVG string):
121
121
 
@@ -154,4 +154,12 @@ const { raster, svg, svgString } = replayOutput(serializedData);
154
154
 
155
155
  **Functions:** `stepPerf`, `replayOutput`
156
156
 
157
- **Types:** `RGB`, `SerializedOutput`, `StepData`, `ReplayResult`, `ShapeInterface`, `Bbox`
157
+ **Types:** `RGB`, `SerializedOutput`, `StepData`, `ReplayResult`, `ShapeInterface`, `Bbox`, `Background`
158
+
159
+ `Background` controls the canvas fill for `saveRasterFromVector`:
160
+
161
+ ```ts
162
+ type Background = string | [png: string, jpeg: string]
163
+ ```
164
+
165
+ Pass a CSS color string to use the same background for all formats, or a tuple to set PNG and JPEG fills independently.
package/dist/index.d.ts CHANGED
@@ -51,8 +51,9 @@ interface SaveOptions {
51
51
  suffix?: string;
52
52
  filename?: string;
53
53
  }
54
+ type Background = string | [png: string, jpeg: string];
54
55
  declare function saveRaster(canvas: HTMLCanvasElement, format?: 'png' | 'jpeg', quality?: number, options?: SaveOptions): void;
55
- declare function saveRasterFromVector(svgString: string, width: number, height: number, format?: 'png' | 'jpeg', quality?: number, options?: SaveOptions): Promise<void>;
56
+ declare function saveRasterFromVector(svgString: string, width: number, height: number, format?: 'png' | 'jpeg', quality?: number, options?: SaveOptions, background?: Background): Promise<void>;
56
57
  declare function copyRasterFromVector(svgString: string, width: number, height: number): Promise<void>;
57
58
  declare function shareFromVector(svgString: string, width: number, height: number, options?: SaveOptions): Promise<void>;
58
59
  declare function saveVector(svgString: string, options?: SaveOptions): void;
@@ -60,4 +61,4 @@ declare function copyVector(svgString: string): Promise<void>;
60
61
  declare function copyRaster(canvas: HTMLCanvasElement): Promise<void>;
61
62
  declare function share(canvas: HTMLCanvasElement, options?: SaveOptions): Promise<void>;
62
63
 
63
- export { type Config, type JobHandle, type RunCallbacks, type SaveOptions, type StepResult, copyRaster, copyRasterFromVector, copyVector, run, runWorker, saveRaster, saveRasterFromVector, saveVector, share, shareFromVector };
64
+ export { type Background, type Config, type JobHandle, type RunCallbacks, type SaveOptions, type StepResult, copyRaster, copyRasterFromVector, copyVector, run, runWorker, saveRaster, saveRasterFromVector, saveVector, share, shareFromVector };
package/dist/index.js CHANGED
@@ -161,6 +161,11 @@ function runWorker(source, cfg, callbacks = {}) {
161
161
  }
162
162
 
163
163
  // src/exit.ts
164
+ function resolveBackground(background, format) {
165
+ if (background === void 0) return format === "jpeg" ? "white" : void 0;
166
+ if (Array.isArray(background)) return format === "png" ? background[0] : background[1];
167
+ return background;
168
+ }
164
169
  function buildFilename(ext, options) {
165
170
  const suffix = options?.suffix ?? "prim";
166
171
  const name = options?.filename ?? "output";
@@ -172,7 +177,7 @@ function triggerDownload(url, filename) {
172
177
  a.download = filename;
173
178
  a.click();
174
179
  }
175
- function svgToCanvas(svgString, width, height) {
180
+ function svgToCanvas(svgString, width, height, background) {
176
181
  return new Promise((resolve, reject) => {
177
182
  const sized = svgString.replace(/(<svg[^>]*\s)width="[^"]*"/, `$1width="${width}"`).replace(/(<svg[^>]*\s)height="[^"]*"/, `$1height="${height}"`);
178
183
  const blob = new Blob([sized], { type: "image/svg+xml" });
@@ -182,7 +187,12 @@ function svgToCanvas(svgString, width, height) {
182
187
  const canvas = document.createElement("canvas");
183
188
  canvas.width = width;
184
189
  canvas.height = height;
185
- canvas.getContext("2d").drawImage(img, 0, 0, width, height);
190
+ const ctx = canvas.getContext("2d");
191
+ if (background !== void 0) {
192
+ ctx.fillStyle = background;
193
+ ctx.fillRect(0, 0, width, height);
194
+ }
195
+ ctx.drawImage(img, 0, 0, width, height);
186
196
  URL.revokeObjectURL(url);
187
197
  resolve(canvas);
188
198
  };
@@ -199,8 +209,8 @@ function saveRaster(canvas, format = "png", quality = 0.92, options) {
199
209
  const dataUrl = canvas.toDataURL(mimeType, quality);
200
210
  triggerDownload(dataUrl, buildFilename(ext, options));
201
211
  }
202
- async function saveRasterFromVector(svgString, width, height, format = "png", quality = 0.92, options) {
203
- const canvas = await svgToCanvas(svgString, width, height);
212
+ async function saveRasterFromVector(svgString, width, height, format = "png", quality = 0.92, options, background) {
213
+ const canvas = await svgToCanvas(svgString, width, height, resolveBackground(background, format));
204
214
  saveRaster(canvas, format, quality, options);
205
215
  }
206
216
  async function copyRasterFromVector(svgString, width, height) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@slithy/prim-interface",
3
- "version": "0.4.3",
3
+ "version": "0.5.0",
4
4
  "description": "Browser-facing API for primitive-based image reconstruction.",
5
5
  "type": "module",
6
6
  "exports": {