@slithy/prim-interface 0.4.0 → 0.4.3

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
@@ -34,7 +34,7 @@ interface RunCallbacks {
34
34
  onStart?: (raster: HTMLCanvasElement, svg: SVGSVGElement) => void;
35
35
  onStep?: (result: StepResult) => void;
36
36
  onComplete?: (serialized: SerializedOutput) => void;
37
- onStop?: () => void;
37
+ onStop?: (serialized: SerializedOutput | null) => void;
38
38
  onError?: (message: string) => void;
39
39
  }
40
40
  interface JobHandle {
package/dist/index.js CHANGED
@@ -4,13 +4,15 @@ function run(source, cfg, callbacks = {}) {
4
4
  const url = source instanceof File ? URL.createObjectURL(source) : source;
5
5
  let optimizer = null;
6
6
  let stopped = false;
7
+ let computeCfg = null;
8
+ const accumulatedSteps = [];
7
9
  Canvas.original(url, cfg).then((original) => {
8
10
  if (stopped) {
9
11
  if (source instanceof File) URL.revokeObjectURL(url);
10
12
  return;
11
13
  }
12
14
  if (source instanceof File) URL.revokeObjectURL(url);
13
- const computeCfg = { ...cfg, width: cfg.width, height: cfg.height };
15
+ computeCfg = { ...cfg, width: cfg.width, height: cfg.height };
14
16
  const pixelRatio = cfg.pixelRatio ?? 1;
15
17
  const viewCfg = { ...computeCfg, width: computeCfg.scale * computeCfg.width, height: computeCfg.scale * computeCfg.height };
16
18
  const deviceViewCfg = { ...viewCfg, width: viewCfg.width * pixelRatio, height: viewCfg.height * pixelRatio };
@@ -23,7 +25,6 @@ function run(source, cfg, callbacks = {}) {
23
25
  svg.setAttribute("height", String(viewCfg.height));
24
26
  callbacks.onStart?.(rasterNode, svg);
25
27
  const serializer = new XMLSerializer();
26
- const accumulatedSteps = [];
27
28
  optimizer = new Optimizer(original, computeCfg);
28
29
  let stepCount = 0;
29
30
  optimizer.onStep = (step) => {
@@ -65,7 +66,14 @@ function run(source, cfg, callbacks = {}) {
65
66
  stop: () => {
66
67
  stopped = true;
67
68
  optimizer?.stop();
68
- callbacks.onStop?.();
69
+ callbacks.onStop?.(computeCfg ? {
70
+ v: 1,
71
+ w: computeCfg.width,
72
+ h: computeCfg.height,
73
+ scale: computeCfg.scale,
74
+ fill: parseColor(cfg.fill),
75
+ steps: accumulatedSteps
76
+ } : null);
69
77
  },
70
78
  pause: () => optimizer?.pause(),
71
79
  resume: () => optimizer?.resume()
@@ -123,7 +131,7 @@ function runWorker(source, cfg, callbacks = {}) {
123
131
  callbacks.onComplete?.(msg.serialized);
124
132
  } else if (msg.type === "stopped") {
125
133
  if (isBlob) URL.revokeObjectURL(url);
126
- callbacks.onStop?.();
134
+ callbacks.onStop?.(msg.serialized);
127
135
  } else if (msg.type === "error") {
128
136
  if (isBlob) URL.revokeObjectURL(url);
129
137
  callbacks.onError?.(msg.message);
@@ -41,6 +41,7 @@ type WorkerOutbound = {
41
41
  serialized: SerializedOutput;
42
42
  } | {
43
43
  type: 'stopped';
44
+ serialized: SerializedOutput | null;
44
45
  } | {
45
46
  type: 'error';
46
47
  message: string;
@@ -11,6 +11,8 @@ var SHAPES = {
11
11
  Glyph
12
12
  };
13
13
  var optimizer = null;
14
+ var computeCfg = null;
15
+ var accumulatedSteps = [];
14
16
  self.onmessage = async (e) => {
15
17
  const msg = e.data;
16
18
  switch (msg.type) {
@@ -50,7 +52,8 @@ self.onmessage = async (e) => {
50
52
  }
51
53
  const original = Canvas.fromBitmap(bitmap, preCfg);
52
54
  bitmap.close();
53
- const computeCfg = { ...preCfg, width: preCfg.width, height: preCfg.height };
55
+ computeCfg = { ...preCfg, width: preCfg.width, height: preCfg.height };
56
+ accumulatedSteps = [];
54
57
  self.postMessage({
55
58
  type: "ready",
56
59
  width: computeCfg.width,
@@ -58,9 +61,9 @@ self.onmessage = async (e) => {
58
61
  scale: computeCfg.scale,
59
62
  fill: computeCfg.fill
60
63
  });
61
- const accumulatedSteps = [];
62
64
  let stepCount = 0;
63
- optimizer = new Optimizer(original, computeCfg, (fn) => setTimeout(fn, 0));
65
+ const cc = computeCfg;
66
+ optimizer = new Optimizer(original, cc, (fn) => setTimeout(fn, 0));
64
67
  optimizer.onStep = (step) => {
65
68
  stepCount++;
66
69
  if (step) {
@@ -71,19 +74,19 @@ self.onmessage = async (e) => {
71
74
  stepData,
72
75
  progress: {
73
76
  current: stepCount,
74
- total: computeCfg.steps,
77
+ total: cc.steps,
75
78
  similarity: parseFloat((100 * (1 - step.distance)).toFixed(2))
76
79
  }
77
80
  };
78
81
  self.postMessage(outbound);
79
82
  }
80
- if (stepCount >= computeCfg.steps) {
83
+ if (stepCount >= cc.steps) {
81
84
  const serialized = {
82
85
  v: 1,
83
- w: computeCfg.width,
84
- h: computeCfg.height,
85
- scale: computeCfg.scale,
86
- fill: parseColor(computeCfg.fill),
86
+ w: cc.width,
87
+ h: cc.height,
88
+ scale: cc.scale,
89
+ fill: parseColor(cc.fill),
87
90
  steps: accumulatedSteps
88
91
  };
89
92
  self.postMessage({ type: "complete", serialized });
@@ -100,7 +103,14 @@ self.onmessage = async (e) => {
100
103
  break;
101
104
  case "stop":
102
105
  optimizer?.stop();
103
- self.postMessage({ type: "stopped" });
106
+ self.postMessage({ type: "stopped", serialized: computeCfg ? {
107
+ v: 1,
108
+ w: computeCfg.width,
109
+ h: computeCfg.height,
110
+ scale: computeCfg.scale,
111
+ fill: parseColor(computeCfg.fill),
112
+ steps: accumulatedSteps
113
+ } : null });
104
114
  break;
105
115
  }
106
116
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@slithy/prim-interface",
3
- "version": "0.4.0",
3
+ "version": "0.4.3",
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.0"
21
+ "@slithy/prim-lib": "0.5.1"
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/tsconfig": "0.0.0",
30
- "@slithy/eslint-config": "0.0.0"
29
+ "@slithy/eslint-config": "0.0.0",
30
+ "@slithy/tsconfig": "0.0.0"
31
31
  },
32
32
  "author": {
33
33
  "name": "Matthew Campagna",