@slithy/prim-lib 0.2.1 → 0.3.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.d.ts +19 -1
- package/dist/index.js +77 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -38,6 +38,7 @@ interface Cfg {
|
|
|
38
38
|
alpha: number;
|
|
39
39
|
mutateAlpha: boolean;
|
|
40
40
|
shapeTypes: Array<new (w: number, h: number) => ShapeInterface>;
|
|
41
|
+
shapeWeights?: number[];
|
|
41
42
|
shapes: number;
|
|
42
43
|
mutations: number;
|
|
43
44
|
steps: number;
|
|
@@ -74,6 +75,13 @@ type StepData = {
|
|
|
74
75
|
cy: number;
|
|
75
76
|
rx: number;
|
|
76
77
|
ry: number;
|
|
78
|
+
} | {
|
|
79
|
+
t: 'c';
|
|
80
|
+
a: number;
|
|
81
|
+
c: RGB;
|
|
82
|
+
cx: number;
|
|
83
|
+
cy: number;
|
|
84
|
+
r: number;
|
|
77
85
|
} | {
|
|
78
86
|
t: 's';
|
|
79
87
|
a: number;
|
|
@@ -223,6 +231,16 @@ declare class Ellipse extends Shape {
|
|
|
223
231
|
mutate(_cfg?: Partial<Cfg>): ShapeInterface;
|
|
224
232
|
computeBbox(): this;
|
|
225
233
|
}
|
|
234
|
+
declare class Circle extends Shape {
|
|
235
|
+
center: Point;
|
|
236
|
+
r: number;
|
|
237
|
+
constructor(w: number, h: number);
|
|
238
|
+
computeBbox(): this;
|
|
239
|
+
render(ctx: CanvasRenderingContext2D): void;
|
|
240
|
+
toSVG(): SVGElement;
|
|
241
|
+
toData(a: number, c: RGB): StepData;
|
|
242
|
+
mutate(_cfg?: Partial<Cfg>): ShapeInterface;
|
|
243
|
+
}
|
|
226
244
|
declare class Glyph extends Shape {
|
|
227
245
|
center: Point;
|
|
228
246
|
text: string;
|
|
@@ -283,4 +301,4 @@ interface ReplayResult {
|
|
|
283
301
|
}
|
|
284
302
|
declare function replayOutput(data: SerializedOutput): ReplayResult;
|
|
285
303
|
|
|
286
|
-
export { type Bbox, Canvas, type Cfg, Debug, Ellipse, Glyph, Hexagon, type ImageDataLike, Optimizer, type Point, type PreCfg, type RGB, Rectangle, type ReplayResult, SVGNS, type SerializedOutput, Shape, type ShapeImageData, type ShapeInterface, Square, State, Step, type StepData, Triangle, clamp, clampColor, computeColorAndDifferenceChange, difference, differenceToDistance, distanceToDifference, getFill, parseColor, replayOutput, stepPerf };
|
|
304
|
+
export { type Bbox, Canvas, type Cfg, Circle, Debug, Ellipse, Glyph, Hexagon, type ImageDataLike, Optimizer, type Point, type PreCfg, type RGB, Rectangle, type ReplayResult, SVGNS, type SerializedOutput, Shape, type ShapeImageData, type ShapeInterface, Square, State, Step, type StepData, Triangle, clamp, clampColor, computeColorAndDifferenceChange, difference, differenceToDistance, distanceToDifference, getFill, parseColor, replayOutput, stepPerf };
|
package/dist/index.js
CHANGED
|
@@ -349,6 +349,16 @@ var Shape = class {
|
|
|
349
349
|
}
|
|
350
350
|
static create(cfg) {
|
|
351
351
|
const ctors = cfg.shapeTypes;
|
|
352
|
+
const weights = cfg.shapeWeights;
|
|
353
|
+
if (weights && weights.length === ctors.length) {
|
|
354
|
+
const total = weights.reduce((sum, w) => sum + w, 0);
|
|
355
|
+
let r = Math.random() * total;
|
|
356
|
+
for (let i = 0; i < weights.length; i++) {
|
|
357
|
+
r -= weights[i];
|
|
358
|
+
if (r <= 0) return new ctors[i](cfg.width, cfg.height);
|
|
359
|
+
}
|
|
360
|
+
return new ctors[ctors.length - 1](cfg.width, cfg.height);
|
|
361
|
+
}
|
|
352
362
|
const index = Math.floor(Math.random() * ctors.length);
|
|
353
363
|
const ctor = ctors[index];
|
|
354
364
|
return new ctor(cfg.width, cfg.height);
|
|
@@ -583,6 +593,59 @@ var Ellipse = class _Ellipse extends Shape {
|
|
|
583
593
|
return this;
|
|
584
594
|
}
|
|
585
595
|
};
|
|
596
|
+
var Circle = class _Circle extends Shape {
|
|
597
|
+
center;
|
|
598
|
+
r;
|
|
599
|
+
constructor(w, h) {
|
|
600
|
+
super(w, h);
|
|
601
|
+
this.center = Shape.randomPoint(w, h);
|
|
602
|
+
this.r = 1 + ~~(Math.random() * 20);
|
|
603
|
+
this.computeBbox();
|
|
604
|
+
}
|
|
605
|
+
computeBbox() {
|
|
606
|
+
this.bbox = {
|
|
607
|
+
left: this.center[0] - this.r,
|
|
608
|
+
top: this.center[1] - this.r,
|
|
609
|
+
width: 2 * this.r || 1,
|
|
610
|
+
height: 2 * this.r || 1
|
|
611
|
+
};
|
|
612
|
+
return this;
|
|
613
|
+
}
|
|
614
|
+
render(ctx) {
|
|
615
|
+
ctx.beginPath();
|
|
616
|
+
ctx.arc(this.center[0], this.center[1], this.r, 0, 2 * Math.PI);
|
|
617
|
+
ctx.fill();
|
|
618
|
+
}
|
|
619
|
+
toSVG() {
|
|
620
|
+
const node = document.createElementNS(SVGNS, "circle");
|
|
621
|
+
node.setAttribute("cx", String(this.center[0]));
|
|
622
|
+
node.setAttribute("cy", String(this.center[1]));
|
|
623
|
+
node.setAttribute("r", String(this.r));
|
|
624
|
+
return node;
|
|
625
|
+
}
|
|
626
|
+
toData(a, c) {
|
|
627
|
+
return { t: "c", a, c, cx: this.center[0], cy: this.center[1], r: this.r };
|
|
628
|
+
}
|
|
629
|
+
mutate(_cfg) {
|
|
630
|
+
const clone = new _Circle(0, 0);
|
|
631
|
+
clone.center = [this.center[0], this.center[1]];
|
|
632
|
+
clone.r = this.r;
|
|
633
|
+
switch (Math.floor(Math.random() * 2)) {
|
|
634
|
+
case 0: {
|
|
635
|
+
const angle = Math.random() * 2 * Math.PI;
|
|
636
|
+
const radius = Math.random() * 20;
|
|
637
|
+
clone.center[0] += ~~(radius * Math.cos(angle));
|
|
638
|
+
clone.center[1] += ~~(radius * Math.sin(angle));
|
|
639
|
+
break;
|
|
640
|
+
}
|
|
641
|
+
case 1:
|
|
642
|
+
clone.r += (Math.random() - 0.5) * 20;
|
|
643
|
+
clone.r = Math.max(1, ~~clone.r);
|
|
644
|
+
break;
|
|
645
|
+
}
|
|
646
|
+
return clone.computeBbox();
|
|
647
|
+
}
|
|
648
|
+
};
|
|
586
649
|
var Glyph = class _Glyph extends Shape {
|
|
587
650
|
center;
|
|
588
651
|
text;
|
|
@@ -912,6 +975,12 @@ function renderStep(data, ctx) {
|
|
|
912
975
|
ctx.fill();
|
|
913
976
|
break;
|
|
914
977
|
}
|
|
978
|
+
case "c": {
|
|
979
|
+
ctx.beginPath();
|
|
980
|
+
ctx.arc(data.cx, data.cy, data.r, 0, 2 * Math.PI);
|
|
981
|
+
ctx.fill();
|
|
982
|
+
break;
|
|
983
|
+
}
|
|
915
984
|
case "s": {
|
|
916
985
|
ctx.fillRect(data.cx - data.r, data.cy - data.r, 2 * data.r, 2 * data.r);
|
|
917
986
|
break;
|
|
@@ -953,6 +1022,13 @@ function stepToSVG(data) {
|
|
|
953
1022
|
node.setAttribute("ry", String(data.ry));
|
|
954
1023
|
break;
|
|
955
1024
|
}
|
|
1025
|
+
case "c": {
|
|
1026
|
+
node = document.createElementNS(SVGNS, "circle");
|
|
1027
|
+
node.setAttribute("cx", String(data.cx));
|
|
1028
|
+
node.setAttribute("cy", String(data.cy));
|
|
1029
|
+
node.setAttribute("r", String(data.r));
|
|
1030
|
+
break;
|
|
1031
|
+
}
|
|
956
1032
|
case "s": {
|
|
957
1033
|
node = document.createElementNS(SVGNS, "rect");
|
|
958
1034
|
node.setAttribute("x", String(data.cx - data.r));
|
|
@@ -1002,6 +1078,7 @@ function replayOutput(data) {
|
|
|
1002
1078
|
}
|
|
1003
1079
|
export {
|
|
1004
1080
|
Canvas,
|
|
1081
|
+
Circle,
|
|
1005
1082
|
Debug,
|
|
1006
1083
|
Ellipse,
|
|
1007
1084
|
Glyph,
|