@slithy/prim-interface 0.2.6 → 0.2.7

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
@@ -39,6 +39,7 @@ interface Config {
39
39
  allowUpscale?: boolean // allow output larger than source image (default: false)
40
40
  pixelRatio?: number // device pixel ratio for the raster canvas (default: 1)
41
41
  shapeTypes: ShapeConstructor[]
42
+ shapeWeights?: number[] // per-shape selection weights (same length as shapeTypes)
42
43
  fill: 'auto' | string
43
44
  }
44
45
  ```
@@ -49,6 +50,8 @@ interface Config {
49
50
 
50
51
  **`pixelRatio`** — pass `window.devicePixelRatio` to produce a HiDPI raster canvas. The canvas pixel dimensions are `viewSize × pixelRatio`; set its CSS size to `viewSize` for a 1:1 device pixel mapping. Does not affect the SVG output or the optimizer; only the raster display canvas.
51
52
 
53
+ **`shapeWeights`** — optional array of weights, one per entry in `shapeTypes`, controlling how often each shape type is used. Weights are relative (e.g. `[3, 1]` means 75% / 25%). When provided, the optimizer pre-allocates an exact number of slots per shape type (proportional to the weights) and shuffles them, guaranteeing the final distribution matches — rather than just biasing random selection.
54
+
52
55
  ### Callbacks
53
56
 
54
57
  **`onStart(raster, svg)`** — called once after the source image loads. `raster` is the display canvas; `svg` is the live SVG element. Both update in place as shapes are added.
@@ -75,18 +78,29 @@ interface StepResult {
75
78
 
76
79
  Higher-level (rasterize SVG at save time for crisp output):
77
80
 
78
- - **`saveRasterFromVector(svgString, width, height, format?, quality?)`** — downloads PNG or JPEG rasterized from the SVG
81
+ - **`saveRasterFromVector(svgString, width, height, format?, quality?, options?)`** — downloads PNG or JPEG rasterized from the SVG
79
82
  - **`copyRasterFromVector(svgString, width, height)`** — copies a PNG rasterized from the SVG to the clipboard
80
83
  - **`shareFromVector(svgString, width, height)`** — shares a PNG rasterized from the SVG via the Web Share API
81
84
 
82
85
  Lower-level (operate directly on a canvas or SVG string):
83
86
 
84
- - **`saveRaster(canvas, format?, quality?)`** — downloads PNG or JPEG from a canvas
85
- - **`saveVector(svgString)`** — downloads SVG
87
+ - **`saveRaster(canvas, format?, quality?, options?)`** — downloads PNG or JPEG from a canvas
88
+ - **`saveVector(svgString, options?)`** — downloads SVG
86
89
  - **`copyRaster(canvas)`** — copies PNG to clipboard
87
90
  - **`copyVector(svgString)`** — copies SVG markup to clipboard
88
91
  - **`share(canvas)`** — shares via Web Share API if available
89
92
 
93
+ **`SaveOptions`** controls download filenames, formatted as `${filename}--${suffix}.${ext}`:
94
+
95
+ ```ts
96
+ interface SaveOptions {
97
+ suffix?: string // default: 'prim'
98
+ filename?: string // stem without extension, default: 'output'
99
+ }
100
+ ```
101
+
102
+ Example: `saveRaster(canvas, 'png', 0.92, { filename: 'mountains', suffix: 'v2' })` → `mountains--v2.png`
103
+
90
104
  ### `replayOutput(data: SerializedOutput): ReplayResult`
91
105
 
92
106
  Reconstructs a raster canvas and SVG from a `SerializedOutput` without re-running the optimizer. Use this to restore saved results from localStorage or a database.
@@ -97,4 +111,4 @@ const { raster, svg, svgString } = replayOutput(serializedData);
97
111
 
98
112
  ### Re-exports from prim-lib
99
113
 
100
- `Triangle`, `Rectangle`, `Ellipse`, `Square`, `Hexagon`, `Glyph`, `stepPerf`, `replayOutput`, `RGB`, `SerializedOutput`, `StepData`, `ReplayResult`
114
+ `Triangle`, `Rectangle`, `Ellipse`, `Circle`, `Square`, `Hexagon`, `Glyph`, `stepPerf`, `replayOutput`, `RGB`, `SerializedOutput`, `StepData`, `ReplayResult`
package/dist/index.d.ts CHANGED
@@ -43,13 +43,17 @@ interface JobHandle {
43
43
 
44
44
  declare function run(source: string | File, cfg: Config, callbacks?: RunCallbacks): JobHandle;
45
45
 
46
- declare function saveRaster(canvas: HTMLCanvasElement, format?: 'png' | 'jpeg', quality?: number): void;
47
- declare function saveRasterFromVector(svgString: string, width: number, height: number, format?: 'png' | 'jpeg', quality?: number): Promise<void>;
46
+ interface SaveOptions {
47
+ suffix?: string;
48
+ filename?: string;
49
+ }
50
+ declare function saveRaster(canvas: HTMLCanvasElement, format?: 'png' | 'jpeg', quality?: number, options?: SaveOptions): void;
51
+ declare function saveRasterFromVector(svgString: string, width: number, height: number, format?: 'png' | 'jpeg', quality?: number, options?: SaveOptions): Promise<void>;
48
52
  declare function copyRasterFromVector(svgString: string, width: number, height: number): Promise<void>;
49
53
  declare function shareFromVector(svgString: string, width: number, height: number): Promise<void>;
50
- declare function saveVector(svgString: string): void;
54
+ declare function saveVector(svgString: string, options?: SaveOptions): void;
51
55
  declare function copyVector(svgString: string): Promise<void>;
52
56
  declare function copyRaster(canvas: HTMLCanvasElement): Promise<void>;
53
57
  declare function share(canvas: HTMLCanvasElement): Promise<void>;
54
58
 
55
- export { type Config, type JobHandle, type RunCallbacks, type StepResult, copyRaster, copyRasterFromVector, copyVector, run, saveRaster, saveRasterFromVector, saveVector, share, shareFromVector };
59
+ export { type Config, type JobHandle, type RunCallbacks, type SaveOptions, type StepResult, copyRaster, copyRasterFromVector, copyVector, run, saveRaster, saveRasterFromVector, saveVector, share, shareFromVector };
package/dist/index.js CHANGED
@@ -70,6 +70,11 @@ function run(source, cfg, callbacks = {}) {
70
70
  }
71
71
 
72
72
  // src/exit.ts
73
+ function buildFilename(ext, options) {
74
+ const suffix = options?.suffix ?? "prim";
75
+ const name = options?.filename ?? "output";
76
+ return `${name}--${suffix}.${ext}`;
77
+ }
73
78
  function triggerDownload(url, filename) {
74
79
  const a = document.createElement("a");
75
80
  a.href = url;
@@ -97,15 +102,15 @@ function svgToCanvas(svgString, width, height) {
97
102
  img.src = url;
98
103
  });
99
104
  }
100
- function saveRaster(canvas, format = "png", quality = 0.92) {
105
+ function saveRaster(canvas, format = "png", quality = 0.92, options) {
101
106
  const mimeType = format === "jpeg" ? "image/jpeg" : "image/png";
102
107
  const ext = format === "jpeg" ? "jpg" : "png";
103
108
  const dataUrl = canvas.toDataURL(mimeType, quality);
104
- triggerDownload(dataUrl, `output.${ext}`);
109
+ triggerDownload(dataUrl, buildFilename(ext, options));
105
110
  }
106
- async function saveRasterFromVector(svgString, width, height, format = "png", quality = 0.92) {
111
+ async function saveRasterFromVector(svgString, width, height, format = "png", quality = 0.92, options) {
107
112
  const canvas = await svgToCanvas(svgString, width, height);
108
- saveRaster(canvas, format, quality);
113
+ saveRaster(canvas, format, quality, options);
109
114
  }
110
115
  async function copyRasterFromVector(svgString, width, height) {
111
116
  const canvas = await svgToCanvas(svgString, width, height);
@@ -115,10 +120,10 @@ async function shareFromVector(svgString, width, height) {
115
120
  const canvas = await svgToCanvas(svgString, width, height);
116
121
  return share(canvas);
117
122
  }
118
- function saveVector(svgString) {
123
+ function saveVector(svgString, options) {
119
124
  const blob = new Blob([svgString], { type: "image/svg+xml" });
120
125
  const url = URL.createObjectURL(blob);
121
- triggerDownload(url, "output.svg");
126
+ triggerDownload(url, buildFilename("svg", options));
122
127
  URL.revokeObjectURL(url);
123
128
  }
124
129
  async function copyVector(svgString) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@slithy/prim-interface",
3
- "version": "0.2.6",
3
+ "version": "0.2.7",
4
4
  "description": "Browser-facing API for primitive-based image reconstruction.",
5
5
  "type": "module",
6
6
  "exports": {