@slithy/prim-lib 0.4.0 → 0.4.1
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.js +64 -3
- package/package.json +5 -5
package/dist/index.js
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
+
|
|
1
4
|
// src/util.ts
|
|
2
5
|
var SVGNS = "http://www.w3.org/2000/svg";
|
|
3
6
|
function parseColor(color) {
|
|
@@ -5,18 +8,23 @@ function parseColor(color) {
|
|
|
5
8
|
if (!m) throw new Error(`Cannot parse color: ${color}`);
|
|
6
9
|
return [parseInt(m[1]), parseInt(m[2]), parseInt(m[3])];
|
|
7
10
|
}
|
|
11
|
+
__name(parseColor, "parseColor");
|
|
8
12
|
function clamp(x, min, max) {
|
|
9
13
|
return Math.max(min, Math.min(max, x));
|
|
10
14
|
}
|
|
15
|
+
__name(clamp, "clamp");
|
|
11
16
|
function clampColor(x) {
|
|
12
17
|
return clamp(x, 0, 255);
|
|
13
18
|
}
|
|
19
|
+
__name(clampColor, "clampColor");
|
|
14
20
|
function distanceToDifference(distance, pixels) {
|
|
15
21
|
return Math.pow(distance * 255, 2) * (3 * pixels);
|
|
16
22
|
}
|
|
23
|
+
__name(distanceToDifference, "distanceToDifference");
|
|
17
24
|
function differenceToDistance(difference2, pixels) {
|
|
18
25
|
return Math.sqrt(difference2 / (3 * pixels)) / 255;
|
|
19
26
|
}
|
|
27
|
+
__name(differenceToDistance, "differenceToDistance");
|
|
20
28
|
function difference(data, dataOther) {
|
|
21
29
|
let sum = 0, dr = 0, dg = 0, db = 0;
|
|
22
30
|
for (let i = 0; i < data.data.length; i += 4) {
|
|
@@ -27,6 +35,7 @@ function difference(data, dataOther) {
|
|
|
27
35
|
}
|
|
28
36
|
return sum;
|
|
29
37
|
}
|
|
38
|
+
__name(difference, "difference");
|
|
30
39
|
function getFill(data) {
|
|
31
40
|
const w = data.width;
|
|
32
41
|
const h = data.height;
|
|
@@ -49,6 +58,7 @@ function getFill(data) {
|
|
|
49
58
|
rgb = rgb.map((x) => ~~(x / count)).map(clampColor);
|
|
50
59
|
return `rgb(${rgb[0]}, ${rgb[1]}, ${rgb[2]})`;
|
|
51
60
|
}
|
|
61
|
+
__name(getFill, "getFill");
|
|
52
62
|
function computeColorAndDifferenceChange(offset, imageData, alpha) {
|
|
53
63
|
const { shape, current, target } = imageData;
|
|
54
64
|
const shapeData = shape.data;
|
|
@@ -116,12 +126,14 @@ function computeColorAndDifferenceChange(offset, imageData, alpha) {
|
|
|
116
126
|
}
|
|
117
127
|
return { color: `rgb(${cr}, ${cg}, ${cb})`, differenceChange: sum };
|
|
118
128
|
}
|
|
129
|
+
__name(computeColorAndDifferenceChange, "computeColorAndDifferenceChange");
|
|
119
130
|
|
|
120
131
|
// src/canvas.ts
|
|
121
132
|
function getScale(width, height, limit, allowUpscale = false) {
|
|
122
133
|
const scale = Math.max(width / limit, height / limit);
|
|
123
134
|
return allowUpscale ? scale : Math.max(scale, 1);
|
|
124
135
|
}
|
|
136
|
+
__name(getScale, "getScale");
|
|
125
137
|
function svgRect(w, h) {
|
|
126
138
|
const node = document.createElementNS(SVGNS, "rect");
|
|
127
139
|
node.setAttribute("x", "0");
|
|
@@ -130,7 +142,11 @@ function svgRect(w, h) {
|
|
|
130
142
|
node.setAttribute("height", String(h));
|
|
131
143
|
return node;
|
|
132
144
|
}
|
|
145
|
+
__name(svgRect, "svgRect");
|
|
133
146
|
var Canvas = class _Canvas {
|
|
147
|
+
static {
|
|
148
|
+
__name(this, "Canvas");
|
|
149
|
+
}
|
|
134
150
|
node;
|
|
135
151
|
ctx;
|
|
136
152
|
_imageData;
|
|
@@ -280,6 +296,9 @@ var Canvas = class _Canvas {
|
|
|
280
296
|
|
|
281
297
|
// src/state.ts
|
|
282
298
|
var State = class {
|
|
299
|
+
static {
|
|
300
|
+
__name(this, "State");
|
|
301
|
+
}
|
|
283
302
|
target;
|
|
284
303
|
canvas;
|
|
285
304
|
distance;
|
|
@@ -302,6 +321,9 @@ var stepPerf = {
|
|
|
302
321
|
}
|
|
303
322
|
};
|
|
304
323
|
var Step = class _Step {
|
|
324
|
+
static {
|
|
325
|
+
__name(this, "Step");
|
|
326
|
+
}
|
|
305
327
|
shape;
|
|
306
328
|
cfg;
|
|
307
329
|
alpha;
|
|
@@ -366,6 +388,9 @@ var Step = class _Step {
|
|
|
366
388
|
// src/shape.ts
|
|
367
389
|
var _rasterCanvas = null;
|
|
368
390
|
var Shape = class {
|
|
391
|
+
static {
|
|
392
|
+
__name(this, "Shape");
|
|
393
|
+
}
|
|
369
394
|
bbox;
|
|
370
395
|
static randomPoint(width, height) {
|
|
371
396
|
return [~~(Math.random() * width), ~~(Math.random() * height)];
|
|
@@ -417,12 +442,15 @@ var Shape = class {
|
|
|
417
442
|
ctx.translate(-this.bbox.left, -this.bbox.top);
|
|
418
443
|
this.render(ctx);
|
|
419
444
|
const data = ctx.getImageData(0, 0, w, h);
|
|
420
|
-
return { getImageData: () => data };
|
|
445
|
+
return { getImageData: /* @__PURE__ */ __name(() => data, "getImageData") };
|
|
421
446
|
}
|
|
422
447
|
render(_ctx) {
|
|
423
448
|
}
|
|
424
449
|
};
|
|
425
450
|
var Polygon = class _Polygon extends Shape {
|
|
451
|
+
static {
|
|
452
|
+
__name(this, "Polygon");
|
|
453
|
+
}
|
|
426
454
|
points;
|
|
427
455
|
constructor(w, h, count) {
|
|
428
456
|
super(w, h);
|
|
@@ -496,6 +524,9 @@ var Polygon = class _Polygon extends Shape {
|
|
|
496
524
|
}
|
|
497
525
|
};
|
|
498
526
|
var Triangle = class _Triangle extends Polygon {
|
|
527
|
+
static {
|
|
528
|
+
__name(this, "Triangle");
|
|
529
|
+
}
|
|
499
530
|
constructor(w, h) {
|
|
500
531
|
super(w, h, 3);
|
|
501
532
|
}
|
|
@@ -507,6 +538,9 @@ var Triangle = class _Triangle extends Polygon {
|
|
|
507
538
|
}
|
|
508
539
|
};
|
|
509
540
|
var Rectangle = class _Rectangle extends Polygon {
|
|
541
|
+
static {
|
|
542
|
+
__name(this, "Rectangle");
|
|
543
|
+
}
|
|
510
544
|
constructor(w, h) {
|
|
511
545
|
super(w, h, 4);
|
|
512
546
|
}
|
|
@@ -556,6 +590,9 @@ var Rectangle = class _Rectangle extends Polygon {
|
|
|
556
590
|
}
|
|
557
591
|
};
|
|
558
592
|
var Ellipse = class _Ellipse extends Shape {
|
|
593
|
+
static {
|
|
594
|
+
__name(this, "Ellipse");
|
|
595
|
+
}
|
|
559
596
|
center;
|
|
560
597
|
rx;
|
|
561
598
|
ry;
|
|
@@ -617,6 +654,9 @@ var Ellipse = class _Ellipse extends Shape {
|
|
|
617
654
|
}
|
|
618
655
|
};
|
|
619
656
|
var Circle = class _Circle extends Shape {
|
|
657
|
+
static {
|
|
658
|
+
__name(this, "Circle");
|
|
659
|
+
}
|
|
620
660
|
center;
|
|
621
661
|
r;
|
|
622
662
|
constructor(w, h) {
|
|
@@ -670,6 +710,9 @@ var Circle = class _Circle extends Shape {
|
|
|
670
710
|
}
|
|
671
711
|
};
|
|
672
712
|
var Glyph = class _Glyph extends Shape {
|
|
713
|
+
static {
|
|
714
|
+
__name(this, "Glyph");
|
|
715
|
+
}
|
|
673
716
|
center;
|
|
674
717
|
text;
|
|
675
718
|
fontSize;
|
|
@@ -733,6 +776,9 @@ var Glyph = class _Glyph extends Shape {
|
|
|
733
776
|
}
|
|
734
777
|
};
|
|
735
778
|
var Square = class _Square extends Shape {
|
|
779
|
+
static {
|
|
780
|
+
__name(this, "Square");
|
|
781
|
+
}
|
|
736
782
|
center;
|
|
737
783
|
r;
|
|
738
784
|
constructor(w, h) {
|
|
@@ -785,6 +831,9 @@ var Square = class _Square extends Shape {
|
|
|
785
831
|
}
|
|
786
832
|
};
|
|
787
833
|
var Hexagon = class _Hexagon extends Shape {
|
|
834
|
+
static {
|
|
835
|
+
__name(this, "Hexagon");
|
|
836
|
+
}
|
|
788
837
|
center;
|
|
789
838
|
r;
|
|
790
839
|
angle;
|
|
@@ -867,6 +916,9 @@ var Hexagon = class _Hexagon extends Shape {
|
|
|
867
916
|
}
|
|
868
917
|
};
|
|
869
918
|
var Debug = class extends Shape {
|
|
919
|
+
static {
|
|
920
|
+
__name(this, "Debug");
|
|
921
|
+
}
|
|
870
922
|
constructor(w, h) {
|
|
871
923
|
super(w, h);
|
|
872
924
|
this.bbox = { left: 0, top: 0, width: w, height: h };
|
|
@@ -893,7 +945,11 @@ function buildStepPlan(cfg) {
|
|
|
893
945
|
}
|
|
894
946
|
return plan;
|
|
895
947
|
}
|
|
948
|
+
__name(buildStepPlan, "buildStepPlan");
|
|
896
949
|
var Optimizer = class {
|
|
950
|
+
static {
|
|
951
|
+
__name(this, "Optimizer");
|
|
952
|
+
}
|
|
897
953
|
cfg;
|
|
898
954
|
state;
|
|
899
955
|
onStep;
|
|
@@ -972,7 +1028,7 @@ var Optimizer = class {
|
|
|
972
1028
|
let resolve;
|
|
973
1029
|
let bestStep = step;
|
|
974
1030
|
const promise = new Promise((r) => resolve = r);
|
|
975
|
-
const tryMutation = () => {
|
|
1031
|
+
const tryMutation = /* @__PURE__ */ __name(() => {
|
|
976
1032
|
if (failedAttempts >= LIMIT) {
|
|
977
1033
|
return resolve(bestStep);
|
|
978
1034
|
}
|
|
@@ -985,7 +1041,7 @@ var Optimizer = class {
|
|
|
985
1041
|
}
|
|
986
1042
|
tryMutation();
|
|
987
1043
|
});
|
|
988
|
-
};
|
|
1044
|
+
}, "tryMutation");
|
|
989
1045
|
tryMutation();
|
|
990
1046
|
return promise;
|
|
991
1047
|
}
|
|
@@ -995,12 +1051,14 @@ var Optimizer = class {
|
|
|
995
1051
|
function rgbString([r, g, b]) {
|
|
996
1052
|
return `rgb(${r}, ${g}, ${b})`;
|
|
997
1053
|
}
|
|
1054
|
+
__name(rgbString, "rgbString");
|
|
998
1055
|
function hexPoints(cx, cy, r, angle) {
|
|
999
1056
|
return Array.from({ length: 6 }, (_, i) => {
|
|
1000
1057
|
const a = angle + i * Math.PI / 3;
|
|
1001
1058
|
return [~~(cx + r * Math.cos(a)), ~~(cy + r * Math.sin(a))];
|
|
1002
1059
|
});
|
|
1003
1060
|
}
|
|
1061
|
+
__name(hexPoints, "hexPoints");
|
|
1004
1062
|
function renderStepToCtx(data, ctx) {
|
|
1005
1063
|
ctx.globalAlpha = data.a;
|
|
1006
1064
|
ctx.fillStyle = rgbString(data.c);
|
|
@@ -1046,6 +1104,7 @@ function renderStepToCtx(data, ctx) {
|
|
|
1046
1104
|
}
|
|
1047
1105
|
}
|
|
1048
1106
|
}
|
|
1107
|
+
__name(renderStepToCtx, "renderStepToCtx");
|
|
1049
1108
|
function stepDataToSVGElement(data) {
|
|
1050
1109
|
const color = rgbString(data.c);
|
|
1051
1110
|
const opacity = data.a.toFixed(2);
|
|
@@ -1103,6 +1162,7 @@ function stepDataToSVGElement(data) {
|
|
|
1103
1162
|
node.setAttribute("fill-opacity", opacity);
|
|
1104
1163
|
return node;
|
|
1105
1164
|
}
|
|
1165
|
+
__name(stepDataToSVGElement, "stepDataToSVGElement");
|
|
1106
1166
|
function replayOutput(data) {
|
|
1107
1167
|
const fill = rgbString(data.fill);
|
|
1108
1168
|
const vw = data.w * data.scale;
|
|
@@ -1120,6 +1180,7 @@ function replayOutput(data) {
|
|
|
1120
1180
|
const svgString = new XMLSerializer().serializeToString(svg);
|
|
1121
1181
|
return { raster: raster.node, svg, svgString };
|
|
1122
1182
|
}
|
|
1183
|
+
__name(replayOutput, "replayOutput");
|
|
1123
1184
|
export {
|
|
1124
1185
|
Canvas,
|
|
1125
1186
|
Circle,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@slithy/prim-lib",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.1",
|
|
4
4
|
"description": "Core engine for primitive-based image reconstruction.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -19,8 +19,8 @@
|
|
|
19
19
|
"tsup": "^8",
|
|
20
20
|
"typescript": "^5",
|
|
21
21
|
"vitest": "^4.1.2",
|
|
22
|
-
"@slithy/
|
|
23
|
-
"@slithy/
|
|
22
|
+
"@slithy/tsconfig": "0.0.0",
|
|
23
|
+
"@slithy/eslint-config": "0.0.0"
|
|
24
24
|
},
|
|
25
25
|
"author": {
|
|
26
26
|
"name": "Matthew Campagna",
|
|
@@ -44,8 +44,8 @@
|
|
|
44
44
|
},
|
|
45
45
|
"scripts": {
|
|
46
46
|
"clean": "rm -rf dist",
|
|
47
|
-
"build": "rm -rf dist && tsup src/index.ts --format esm --dts",
|
|
48
|
-
"dev": "tsup src/index.ts --format esm --watch",
|
|
47
|
+
"build": "rm -rf dist && tsup src/index.ts --format esm --dts --keep-names",
|
|
48
|
+
"dev": "tsup src/index.ts --format esm --watch --keep-names",
|
|
49
49
|
"typecheck": "tsc --noEmit",
|
|
50
50
|
"lint": "eslint .",
|
|
51
51
|
"test": "vitest run",
|