@slithy/prim-interface 0.1.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/LICENSE +91 -0
- package/README.md +56 -0
- package/dist/index.d.ts +48 -0
- package/dist/index.js +108 -0
- package/package.json +57 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
# PolyForm Noncommercial License 1.0.0
|
|
2
|
+
|
|
3
|
+
<https://polyformproject.org/licenses/noncommercial/1.0.0>
|
|
4
|
+
|
|
5
|
+
Required Notice: Copyright Matthew Campagna
|
|
6
|
+
|
|
7
|
+
## Acceptance
|
|
8
|
+
|
|
9
|
+
In order to get any license under these terms, you must agree to them as both strict obligations and conditions to all your licenses.
|
|
10
|
+
|
|
11
|
+
## Copyright License
|
|
12
|
+
|
|
13
|
+
The licensor grants you a copyright license for the software to do everything you might do with the software that would otherwise infringe the licensor's copyright in it for any permitted purpose. However, you may only distribute the software according to [Distribution License](#distribution-license) and make changes or new works based on the software according to [Changes and New Works License](#changes-and-new-works-license).
|
|
14
|
+
|
|
15
|
+
## Distribution License
|
|
16
|
+
|
|
17
|
+
The licensor grants you an additional copyright license to distribute copies of the software. Your license to distribute covers distributing the software with changes and new works permitted by [Changes and New Works License](#changes-and-new-works-license).
|
|
18
|
+
|
|
19
|
+
## Notices
|
|
20
|
+
|
|
21
|
+
You must ensure that anyone who gets a copy of any part of the software from you also gets a copy of these terms or the URL for them above, as well as copies of any plain-text lines beginning with `Required Notice:` that the licensor provided with the software. For example:
|
|
22
|
+
|
|
23
|
+
> Required Notice: Copyright Yoyodyne, Inc. (http://example.com)
|
|
24
|
+
|
|
25
|
+
## Changes and New Works License
|
|
26
|
+
|
|
27
|
+
The licensor grants you an additional copyright license to make changes and new works based on the software for any permitted purpose.
|
|
28
|
+
|
|
29
|
+
## Patent License
|
|
30
|
+
|
|
31
|
+
The licensor grants you a patent license for the software that covers patent claims the licensor can license, or becomes able to license, that you would infringe by using the software.
|
|
32
|
+
|
|
33
|
+
## Noncommercial Purposes
|
|
34
|
+
|
|
35
|
+
Any noncommercial purpose is a permitted purpose.
|
|
36
|
+
|
|
37
|
+
## Personal Uses
|
|
38
|
+
|
|
39
|
+
Personal use for research, experiment, and testing for the benefit of public knowledge, personal study, private entertainment, hobby projects, amateur pursuits, or religious observance, without any anticipated commercial application, is use for a permitted purpose.
|
|
40
|
+
|
|
41
|
+
## Noncommercial Organizations
|
|
42
|
+
|
|
43
|
+
Use by any charitable organization, educational institution, public research organization, public safety or health organization, environmental protection organization, or government institution is use for a permitted purpose regardless of the source of funding or obligations resulting from the funding.
|
|
44
|
+
|
|
45
|
+
## Fair Use
|
|
46
|
+
|
|
47
|
+
You may have "fair use" rights for the software under the law. These terms do not limit them.
|
|
48
|
+
|
|
49
|
+
## No Other Rights
|
|
50
|
+
|
|
51
|
+
These terms do not allow you to sublicense or transfer any of your licenses to anyone else, or prevent the licensor from granting licenses to anyone else. These terms do not imply any other licenses.
|
|
52
|
+
|
|
53
|
+
## Patent Defense
|
|
54
|
+
|
|
55
|
+
If you make any written claim that the software infringes or contributes to infringement of any patent, your patent license for the software granted under these terms ends immediately. If your company makes such a claim, your patent license ends immediately for work on behalf of your company.
|
|
56
|
+
|
|
57
|
+
## Violations
|
|
58
|
+
|
|
59
|
+
The first time you are notified in writing that you have violated any of these terms, or done anything with the software not covered by your licenses, your licenses can nonetheless continue if you come into full compliance with these terms, and take practical steps to correct past violations, within 32 days of receiving notice. Otherwise, all your licenses end immediately.
|
|
60
|
+
|
|
61
|
+
## No Liability
|
|
62
|
+
|
|
63
|
+
***As far as the law allows, the software comes as is, without any warranty or condition, and the licensor will not be liable to you for any damages arising out of these terms or the use or nature of the software, under any kind of legal claim.***
|
|
64
|
+
|
|
65
|
+
## Definitions
|
|
66
|
+
|
|
67
|
+
The **licensor** is the individual or entity offering these terms, and the **software** is the software the licensor makes available under these terms.
|
|
68
|
+
|
|
69
|
+
**You** refers to the individual or entity agreeing to these terms.
|
|
70
|
+
|
|
71
|
+
**Your company** is any legal entity, sole proprietorship, or other kind of organization that you work for, plus all organizations that have control over, are under the control of, or are under common control with that organization. **Control** means ownership of substantially all the assets of an entity, or the power to direct its management and policies by vote, contract, or otherwise. Control can be direct or indirect.
|
|
72
|
+
|
|
73
|
+
**Your licenses** are all the licenses granted to you for the software under these terms.
|
|
74
|
+
|
|
75
|
+
**Use** means anything you do with the software requiring one of your licenses.
|
|
76
|
+
|
|
77
|
+
---
|
|
78
|
+
|
|
79
|
+
## Third-party attributions
|
|
80
|
+
|
|
81
|
+
This software is derived from the following MIT-licensed works:
|
|
82
|
+
|
|
83
|
+
**primitive** by Michael Fogleman
|
|
84
|
+
https://github.com/fogleman/primitive
|
|
85
|
+
Copyright (c) 2016 Michael Fogleman — MIT License
|
|
86
|
+
|
|
87
|
+
**primitive.js** by Ondřej Žára
|
|
88
|
+
https://github.com/ondras/primitive.js
|
|
89
|
+
Copyright (c) Ondřej Žára — MIT License
|
|
90
|
+
|
|
91
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notices and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# @slithy/prim-interface
|
|
2
|
+
|
|
3
|
+
Browser-facing API for `@slithy/prim-lib`. Consumed by `apps/prim-demo`.
|
|
4
|
+
|
|
5
|
+
## What it does
|
|
6
|
+
|
|
7
|
+
Wraps `prim-lib` in a simple `run()` API and provides save/copy/share helpers for exporting results.
|
|
8
|
+
|
|
9
|
+
## Exports
|
|
10
|
+
|
|
11
|
+
### `run(source, config, callbacks)`
|
|
12
|
+
|
|
13
|
+
Starts a reconstruction job. Returns a `JobHandle` for stopping, pausing, and resuming.
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
const job = run(imageUrl, config, {
|
|
17
|
+
onStart(raster, svg) {},
|
|
18
|
+
onStep({ raster, svg, svgString, progress }) {},
|
|
19
|
+
onComplete() {},
|
|
20
|
+
onStop() {},
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
job.stop();
|
|
24
|
+
job.pause();
|
|
25
|
+
job.resume();
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
### Config
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
interface Config {
|
|
32
|
+
steps: number // shapes in the final image
|
|
33
|
+
shapes: number // candidate shapes evaluated per step
|
|
34
|
+
mutations: number // random mutations tried per candidate
|
|
35
|
+
alpha: number // shape opacity
|
|
36
|
+
mutateAlpha: boolean // whether opacity is mutated per candidate
|
|
37
|
+
computeSize: number // internal render resolution (px)
|
|
38
|
+
viewSize: number // display resolution (px)
|
|
39
|
+
shapeTypes: ShapeConstructor[]
|
|
40
|
+
fill: 'auto' | string
|
|
41
|
+
}
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
`width`, `height`, and `scale` are set automatically by `run()` after loading the source image.
|
|
45
|
+
|
|
46
|
+
### Exit helpers
|
|
47
|
+
|
|
48
|
+
- **`saveRaster(canvas, format?, quality?)`** — downloads PNG or JPEG
|
|
49
|
+
- **`saveVector(svgString)`** — downloads SVG
|
|
50
|
+
- **`copyRaster(canvas)`** — copies PNG to clipboard
|
|
51
|
+
- **`copyVector(svgString)`** — copies SVG markup to clipboard
|
|
52
|
+
- **`share(canvas)`** — uses Web Share API if available
|
|
53
|
+
|
|
54
|
+
### Re-exports from prim-lib
|
|
55
|
+
|
|
56
|
+
`Triangle`, `Rectangle`, `Ellipse`, `Hexagon`, `Smiley`, `stepPerf`
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { ShapeInterface } from '@slithy/prim-lib';
|
|
2
|
+
export { Bbox, Ellipse, Hexagon, Rectangle, ShapeInterface, Smiley, Triangle, stepPerf } from '@slithy/prim-lib';
|
|
3
|
+
|
|
4
|
+
interface Config {
|
|
5
|
+
steps: number;
|
|
6
|
+
shapes: number;
|
|
7
|
+
mutations: number;
|
|
8
|
+
alpha: number;
|
|
9
|
+
mutateAlpha: boolean;
|
|
10
|
+
computeSize: number;
|
|
11
|
+
viewSize: number;
|
|
12
|
+
shapeTypes: Array<new (w: number, h: number) => ShapeInterface>;
|
|
13
|
+
fill: 'auto' | string;
|
|
14
|
+
width?: number;
|
|
15
|
+
height?: number;
|
|
16
|
+
scale?: number;
|
|
17
|
+
}
|
|
18
|
+
interface StepResult {
|
|
19
|
+
raster: HTMLCanvasElement;
|
|
20
|
+
svg: SVGSVGElement;
|
|
21
|
+
svgString: string;
|
|
22
|
+
progress: {
|
|
23
|
+
current: number;
|
|
24
|
+
total: number;
|
|
25
|
+
similarity: number;
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
interface RunCallbacks {
|
|
29
|
+
onStart?: (raster: HTMLCanvasElement, svg: SVGSVGElement) => void;
|
|
30
|
+
onStep?: (result: StepResult) => void;
|
|
31
|
+
onComplete?: () => void;
|
|
32
|
+
onStop?: () => void;
|
|
33
|
+
}
|
|
34
|
+
interface JobHandle {
|
|
35
|
+
stop(): void;
|
|
36
|
+
pause(): void;
|
|
37
|
+
resume(): void;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
declare function run(source: string | File, cfg: Config, callbacks?: RunCallbacks): JobHandle;
|
|
41
|
+
|
|
42
|
+
declare function saveRaster(canvas: HTMLCanvasElement, format?: 'png' | 'jpeg', quality?: number): void;
|
|
43
|
+
declare function saveVector(svgString: string): void;
|
|
44
|
+
declare function copyVector(svgString: string): Promise<void>;
|
|
45
|
+
declare function copyRaster(canvas: HTMLCanvasElement): Promise<void>;
|
|
46
|
+
declare function share(canvas: HTMLCanvasElement): Promise<void>;
|
|
47
|
+
|
|
48
|
+
export { type Config, type JobHandle, type RunCallbacks, type StepResult, copyRaster, copyVector, run, saveRaster, saveVector, share };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
// src/run.ts
|
|
2
|
+
import { Canvas, Optimizer } from "@slithy/prim-lib";
|
|
3
|
+
function run(source, cfg, callbacks = {}) {
|
|
4
|
+
const url = source instanceof File ? URL.createObjectURL(source) : source;
|
|
5
|
+
let optimizer = null;
|
|
6
|
+
let stopped = false;
|
|
7
|
+
Canvas.original(url, cfg).then((original) => {
|
|
8
|
+
if (stopped) {
|
|
9
|
+
if (source instanceof File) URL.revokeObjectURL(url);
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
if (source instanceof File) URL.revokeObjectURL(url);
|
|
13
|
+
const computeCfg = { ...cfg, width: cfg.width, height: cfg.height };
|
|
14
|
+
const viewCfg = { ...computeCfg, width: computeCfg.scale * computeCfg.width, height: computeCfg.scale * computeCfg.height };
|
|
15
|
+
const result = Canvas.empty(viewCfg);
|
|
16
|
+
result.ctx.scale(computeCfg.scale, computeCfg.scale);
|
|
17
|
+
const svg = Canvas.empty(computeCfg, true);
|
|
18
|
+
svg.setAttribute("width", String(viewCfg.width));
|
|
19
|
+
svg.setAttribute("height", String(viewCfg.height));
|
|
20
|
+
callbacks.onStart?.(result.node, svg);
|
|
21
|
+
const serializer = new XMLSerializer();
|
|
22
|
+
optimizer = new Optimizer(original, computeCfg);
|
|
23
|
+
let stepCount = 0;
|
|
24
|
+
optimizer.onStep = (step) => {
|
|
25
|
+
stepCount++;
|
|
26
|
+
if (step) {
|
|
27
|
+
result.drawStep(step);
|
|
28
|
+
svg.appendChild(step.toSVG());
|
|
29
|
+
callbacks.onStep?.({
|
|
30
|
+
raster: result.node,
|
|
31
|
+
svg,
|
|
32
|
+
svgString: serializer.serializeToString(svg),
|
|
33
|
+
progress: {
|
|
34
|
+
current: stepCount,
|
|
35
|
+
total: cfg.steps,
|
|
36
|
+
similarity: parseFloat((100 * (1 - step.distance)).toFixed(2))
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
if (stepCount >= cfg.steps) {
|
|
41
|
+
callbacks.onComplete?.();
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
optimizer.start();
|
|
45
|
+
}).catch((err) => {
|
|
46
|
+
console.error("[prim] run failed:", err);
|
|
47
|
+
});
|
|
48
|
+
return {
|
|
49
|
+
stop: () => {
|
|
50
|
+
stopped = true;
|
|
51
|
+
optimizer?.stop();
|
|
52
|
+
callbacks.onStop?.();
|
|
53
|
+
},
|
|
54
|
+
pause: () => optimizer?.pause(),
|
|
55
|
+
resume: () => optimizer?.resume()
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// src/exit.ts
|
|
60
|
+
function triggerDownload(url, filename) {
|
|
61
|
+
const a = document.createElement("a");
|
|
62
|
+
a.href = url;
|
|
63
|
+
a.download = filename;
|
|
64
|
+
a.click();
|
|
65
|
+
}
|
|
66
|
+
function saveRaster(canvas, format = "png", quality = 0.92) {
|
|
67
|
+
const mimeType = format === "jpeg" ? "image/jpeg" : "image/png";
|
|
68
|
+
const ext = format === "jpeg" ? "jpg" : "png";
|
|
69
|
+
const dataUrl = canvas.toDataURL(mimeType, quality);
|
|
70
|
+
triggerDownload(dataUrl, `output.${ext}`);
|
|
71
|
+
}
|
|
72
|
+
function saveVector(svgString) {
|
|
73
|
+
const blob = new Blob([svgString], { type: "image/svg+xml" });
|
|
74
|
+
const url = URL.createObjectURL(blob);
|
|
75
|
+
triggerDownload(url, "output.svg");
|
|
76
|
+
URL.revokeObjectURL(url);
|
|
77
|
+
}
|
|
78
|
+
async function copyVector(svgString) {
|
|
79
|
+
await navigator.clipboard.writeText(svgString);
|
|
80
|
+
}
|
|
81
|
+
async function copyRaster(canvas) {
|
|
82
|
+
const blob = await new Promise((resolve, reject) => canvas.toBlob((b) => b ? resolve(b) : reject(new Error("toBlob failed"))));
|
|
83
|
+
await navigator.clipboard.write([new ClipboardItem({ "image/png": blob })]);
|
|
84
|
+
}
|
|
85
|
+
async function share(canvas) {
|
|
86
|
+
const blob = await new Promise((resolve, reject) => canvas.toBlob((b) => b ? resolve(b) : reject(new Error("toBlob failed"))));
|
|
87
|
+
const file = new File([blob], "output.png", { type: "image/png" });
|
|
88
|
+
if (navigator.canShare({ files: [file] })) {
|
|
89
|
+
await navigator.share({ files: [file] });
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// src/index.ts
|
|
94
|
+
import { Triangle, Rectangle, Ellipse, Hexagon, Smiley, stepPerf } from "@slithy/prim-lib";
|
|
95
|
+
export {
|
|
96
|
+
Ellipse,
|
|
97
|
+
Hexagon,
|
|
98
|
+
Rectangle,
|
|
99
|
+
Smiley,
|
|
100
|
+
Triangle,
|
|
101
|
+
copyRaster,
|
|
102
|
+
copyVector,
|
|
103
|
+
run,
|
|
104
|
+
saveRaster,
|
|
105
|
+
saveVector,
|
|
106
|
+
share,
|
|
107
|
+
stepPerf
|
|
108
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@slithy/prim-interface",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Browser-facing API for primitive-based image reconstruction.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"import": "./dist/index.js",
|
|
9
|
+
"types": "./dist/index.d.ts"
|
|
10
|
+
}
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"dist"
|
|
14
|
+
],
|
|
15
|
+
"sideEffects": false,
|
|
16
|
+
"dependencies": {
|
|
17
|
+
"@slithy/prim-lib": "0.1.0"
|
|
18
|
+
},
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"@vitest/coverage-v8": "^4.1.0",
|
|
21
|
+
"jsdom": "^28",
|
|
22
|
+
"tsup": "^8",
|
|
23
|
+
"typescript": "^5",
|
|
24
|
+
"vitest": "^4",
|
|
25
|
+
"@slithy/tsconfig": "0.0.0",
|
|
26
|
+
"@slithy/eslint-config": "0.0.0"
|
|
27
|
+
},
|
|
28
|
+
"author": {
|
|
29
|
+
"name": "Matthew Campagna",
|
|
30
|
+
"url": "https://github.com/mjcampagna"
|
|
31
|
+
},
|
|
32
|
+
"license": "PolyForm-Noncommercial-1.0.0",
|
|
33
|
+
"keywords": [
|
|
34
|
+
"image",
|
|
35
|
+
"reconstruction",
|
|
36
|
+
"primitive",
|
|
37
|
+
"shapes",
|
|
38
|
+
"canvas"
|
|
39
|
+
],
|
|
40
|
+
"repository": {
|
|
41
|
+
"type": "git",
|
|
42
|
+
"url": "https://github.com/mjcampagna/slithy.git",
|
|
43
|
+
"directory": "packages/prim-interface"
|
|
44
|
+
},
|
|
45
|
+
"publishConfig": {
|
|
46
|
+
"access": "public"
|
|
47
|
+
},
|
|
48
|
+
"scripts": {
|
|
49
|
+
"clean": "rm -rf dist",
|
|
50
|
+
"build": "rm -rf dist && tsup src/index.ts --format esm --dts",
|
|
51
|
+
"dev": "tsup src/index.ts --format esm --watch",
|
|
52
|
+
"typecheck": "tsc --noEmit",
|
|
53
|
+
"lint": "eslint .",
|
|
54
|
+
"test": "vitest run",
|
|
55
|
+
"test:watch": "vitest"
|
|
56
|
+
}
|
|
57
|
+
}
|