@slithy/prim-interface 0.5.1 → 1.0.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/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { ShapeInterface, StepData, SerializedOutput } from '@slithy/prim-lib';
2
- export { Bbox, Circle, Ellipse, Glyph, Hexagon, NGonOptions, RGB, RectOptions, Rectangle, ReplayResult, SerializedOutput, ShapeInterface, Square, StepData, Triangle, makeNGon, makeRect, replayOutput, stepPerf } from '@slithy/prim-lib';
2
+ export { Bbox, Circle, CircleOptions, Ellipse, EllipseOptions, Glyph, GlyphOptions, Hexagon, NGonOptions, RGB, RectOptions, Rectangle, ReplayResult, SerializedOutput, ShapeInterface, Square, StepData, Triangle, makeCircle, makeEllipse, makeGlyph, makeNGon, makeRect, replayOutput, stepPerf } from '@slithy/prim-lib';
3
3
 
4
4
  interface Config {
5
5
  steps: number;
@@ -51,14 +51,26 @@ interface SaveOptions {
51
51
  suffix?: string;
52
52
  filename?: string;
53
53
  }
54
+ interface SaveRasterOptions extends SaveOptions {
55
+ format?: 'png' | 'jpeg';
56
+ quality?: number;
57
+ }
58
+ interface SaveRasterFromVectorOptions extends SaveOptions {
59
+ format?: 'png' | 'jpeg';
60
+ quality?: number;
61
+ background?: Background;
62
+ }
63
+ interface ShareFromVectorOptions extends SaveOptions {
64
+ background?: Background;
65
+ }
54
66
  type Background = string | [png: string, jpeg: string];
55
- declare function saveRaster(canvas: HTMLCanvasElement, format?: 'png' | 'jpeg', quality?: number, options?: SaveOptions): void;
56
- declare function saveRasterFromVector(svgString: string, width: number, height: number, format?: 'png' | 'jpeg', quality?: number, options?: SaveOptions, background?: Background): Promise<void>;
67
+ declare function saveRaster(canvas: HTMLCanvasElement, options?: SaveRasterOptions): void;
68
+ declare function saveRasterFromVector(svgString: string, width: number, height: number, options?: SaveRasterFromVectorOptions): Promise<void>;
57
69
  declare function copyRasterFromVector(svgString: string, width: number, height: number): Promise<void>;
58
- declare function shareFromVector(svgString: string, width: number, height: number, options?: SaveOptions, background?: Background): Promise<void>;
70
+ declare function shareFromVector(svgString: string, width: number, height: number, options?: ShareFromVectorOptions): Promise<void>;
59
71
  declare function saveVector(svgString: string, options?: SaveOptions): void;
60
72
  declare function copyVector(svgString: string): Promise<void>;
61
73
  declare function copyRaster(canvas: HTMLCanvasElement): Promise<void>;
62
74
  declare function share(canvas: HTMLCanvasElement, options?: SaveOptions): Promise<void>;
63
75
 
64
- export { type Background, type Config, type JobHandle, type RunCallbacks, type SaveOptions, type StepResult, copyRaster, copyRasterFromVector, copyVector, run, runWorker, saveRaster, saveRasterFromVector, saveVector, share, shareFromVector };
76
+ export { type Background, type Config, type JobHandle, type RunCallbacks, type SaveOptions, type SaveRasterFromVectorOptions, type SaveRasterOptions, type ShareFromVectorOptions, type StepResult, copyRaster, copyRasterFromVector, copyVector, run, runWorker, saveRaster, saveRasterFromVector, saveVector, share, shareFromVector };
package/dist/index.js CHANGED
@@ -15,7 +15,8 @@ function run(source, cfg, callbacks = {}) {
15
15
  computeCfg = { ...cfg, width: cfg.width, height: cfg.height };
16
16
  const pixelRatio = cfg.pixelRatio ?? 1;
17
17
  const viewCfg = { ...computeCfg, width: computeCfg.scale * computeCfg.width, height: computeCfg.scale * computeCfg.height };
18
- const deviceViewCfg = { ...viewCfg, width: viewCfg.width * pixelRatio, height: viewCfg.height * pixelRatio };
18
+ const displayFill = computeCfg.outputFill ?? computeCfg.fill;
19
+ const deviceViewCfg = { ...viewCfg, width: viewCfg.width * pixelRatio, height: viewCfg.height * pixelRatio, fill: displayFill };
19
20
  const result = Canvas.empty(deviceViewCfg);
20
21
  result.ctx.scale(computeCfg.scale * pixelRatio, computeCfg.scale * pixelRatio);
21
22
  if (!(result.node instanceof HTMLCanvasElement)) throw new Error("[prim] Expected HTMLCanvasElement");
@@ -93,18 +94,20 @@ function runWorker(source, cfg, callbacks = {}) {
93
94
  worker.onmessage = (e) => {
94
95
  const msg = e.data;
95
96
  if (msg.type === "ready") {
96
- const { width, height, scale, fill } = msg;
97
+ const { width, height, scale, fill, outputFill } = msg;
97
98
  const pixelRatio = cfg.pixelRatio ?? 1;
98
- const computeCfg = { ...cfg, width, height, scale, fill };
99
+ const computeCfg = { ...cfg, width, height, scale, fill, outputFill };
99
100
  const viewCfg = {
100
101
  ...computeCfg,
101
102
  width: scale * width,
102
103
  height: scale * height
103
104
  };
105
+ const displayFill = outputFill ?? fill;
104
106
  const deviceViewCfg = {
105
107
  ...viewCfg,
106
108
  width: viewCfg.width * pixelRatio,
107
- height: viewCfg.height * pixelRatio
109
+ height: viewCfg.height * pixelRatio,
110
+ fill: displayFill
108
111
  };
109
112
  result = Canvas2.empty(deviceViewCfg);
110
113
  const displayCanvas = result.node;
@@ -203,22 +206,26 @@ function svgToCanvas(svgString, width, height, background) {
203
206
  img.src = url;
204
207
  });
205
208
  }
206
- function saveRaster(canvas, format = "png", quality = 0.92, options) {
209
+ function saveRaster(canvas, options) {
210
+ const format = options?.format ?? "png";
211
+ const quality = options?.quality ?? 0.92;
207
212
  const mimeType = format === "jpeg" ? "image/jpeg" : "image/png";
208
213
  const ext = format === "jpeg" ? "jpg" : "png";
209
214
  const dataUrl = canvas.toDataURL(mimeType, quality);
210
215
  triggerDownload(dataUrl, buildFilename(ext, options));
211
216
  }
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));
214
- saveRaster(canvas, format, quality, options);
217
+ async function saveRasterFromVector(svgString, width, height, options) {
218
+ const format = options?.format ?? "png";
219
+ const quality = options?.quality ?? 0.92;
220
+ const canvas = await svgToCanvas(svgString, width, height, resolveBackground(options?.background, format));
221
+ saveRaster(canvas, { format, quality, suffix: options?.suffix, filename: options?.filename });
215
222
  }
216
223
  async function copyRasterFromVector(svgString, width, height) {
217
224
  const canvas = await svgToCanvas(svgString, width, height);
218
225
  return copyRaster(canvas);
219
226
  }
220
- async function shareFromVector(svgString, width, height, options, background) {
221
- const canvas = await svgToCanvas(svgString, width, height, resolveBackground(background, "png"));
227
+ async function shareFromVector(svgString, width, height, options) {
228
+ const canvas = await svgToCanvas(svgString, width, height, resolveBackground(options?.background, "png"));
222
229
  return share(canvas, options);
223
230
  }
224
231
  function saveVector(svgString, options) {
@@ -251,7 +258,7 @@ async function share(canvas, options) {
251
258
  }
252
259
 
253
260
  // src/index.ts
254
- import { Triangle, Rectangle, Ellipse, Circle, Square, Hexagon, Glyph, stepPerf, replayOutput, makeNGon, makeRect } from "@slithy/prim-lib";
261
+ import { Triangle, Rectangle, Ellipse, Circle, Square, Hexagon, Glyph, stepPerf, replayOutput, makeNGon, makeRect, makeCircle, makeEllipse, makeGlyph } from "@slithy/prim-lib";
255
262
  export {
256
263
  Circle,
257
264
  Ellipse,
@@ -263,6 +270,9 @@ export {
263
270
  copyRaster,
264
271
  copyRasterFromVector,
265
272
  copyVector,
273
+ makeCircle,
274
+ makeEllipse,
275
+ makeGlyph,
266
276
  makeNGon,
267
277
  makeRect,
268
278
  replayOutput,
@@ -1,4 +1,4 @@
1
- import { NGonOptions, RectOptions, PreCfg, StepData, SerializedOutput } from '@slithy/prim-lib';
1
+ import { NGonOptions, RectOptions, CircleOptions, EllipseOptions, GlyphOptions, PreCfg, StepData, SerializedOutput } from '@slithy/prim-lib';
2
2
 
3
3
  type ShapeTypeSpec = string | {
4
4
  f: 'ngon';
@@ -6,6 +6,15 @@ type ShapeTypeSpec = string | {
6
6
  } | {
7
7
  f: 'rect';
8
8
  o: RectOptions;
9
+ } | {
10
+ f: 'circle';
11
+ o: CircleOptions;
12
+ } | {
13
+ f: 'ellipse';
14
+ o: EllipseOptions;
15
+ } | {
16
+ f: 'glyph';
17
+ o: GlyphOptions;
9
18
  };
10
19
  type WorkerCfg = Omit<PreCfg, 'shapeTypes'> & {
11
20
  shapeTypeNames: ShapeTypeSpec[];
@@ -28,6 +37,7 @@ type WorkerOutbound = {
28
37
  height: number;
29
38
  scale: number;
30
39
  fill: string;
40
+ outputFill?: string;
31
41
  } | {
32
42
  type: 'step';
33
43
  stepData: StepData;
@@ -1,5 +1,5 @@
1
1
  // src/worker-entry.ts
2
- import { Canvas, Optimizer, parseColor, makeNGon, makeRect } from "@slithy/prim-lib";
2
+ import { Canvas, Optimizer, parseColor, makeNGon, makeRect, makeCircle, makeEllipse, makeGlyph } from "@slithy/prim-lib";
3
3
  import { Triangle, Rectangle, Ellipse, Circle, Square, Hexagon, Glyph } from "@slithy/prim-lib";
4
4
  var SHAPES = {
5
5
  Triangle,
@@ -26,6 +26,12 @@ self.onmessage = async (e) => {
26
26
  return makeNGon(spec.o);
27
27
  case "rect":
28
28
  return makeRect(spec.o);
29
+ case "circle":
30
+ return makeCircle(spec.o);
31
+ case "ellipse":
32
+ return makeEllipse(spec.o);
33
+ case "glyph":
34
+ return makeGlyph(spec.o);
29
35
  }
30
36
  }
31
37
  const ctor = SHAPES[spec];
@@ -59,7 +65,8 @@ self.onmessage = async (e) => {
59
65
  width: computeCfg.width,
60
66
  height: computeCfg.height,
61
67
  scale: computeCfg.scale,
62
- fill: computeCfg.fill
68
+ fill: computeCfg.fill,
69
+ outputFill: computeCfg.outputFill
63
70
  });
64
71
  let stepCount = 0;
65
72
  const cc = computeCfg;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@slithy/prim-interface",
3
- "version": "0.5.1",
3
+ "version": "1.0.0",
4
4
  "description": "Browser-facing API for primitive-based image reconstruction.",
5
5
  "type": "module",
6
6
  "exports": {
@@ -18,7 +18,7 @@
18
18
  ],
19
19
  "sideEffects": false,
20
20
  "dependencies": {
21
- "@slithy/prim-lib": "0.5.1"
21
+ "@slithy/prim-lib": "0.6.0"
22
22
  },
23
23
  "devDependencies": {
24
24
  "@vitest/coverage-v8": "^4.1.2",
@@ -26,8 +26,8 @@
26
26
  "tsup": "^8",
27
27
  "typescript": "^5",
28
28
  "vitest": "^4.1.2",
29
- "@slithy/eslint-config": "0.0.0",
30
- "@slithy/tsconfig": "0.0.0"
29
+ "@slithy/tsconfig": "0.0.0",
30
+ "@slithy/eslint-config": "0.0.0"
31
31
  },
32
32
  "author": {
33
33
  "name": "Matthew Campagna",