@slithy/prim-lib 0.2.1 → 0.3.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 +18 -1
- package/dist/index.js +67 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -74,6 +74,13 @@ type StepData = {
|
|
|
74
74
|
cy: number;
|
|
75
75
|
rx: number;
|
|
76
76
|
ry: number;
|
|
77
|
+
} | {
|
|
78
|
+
t: 'c';
|
|
79
|
+
a: number;
|
|
80
|
+
c: RGB;
|
|
81
|
+
cx: number;
|
|
82
|
+
cy: number;
|
|
83
|
+
r: number;
|
|
77
84
|
} | {
|
|
78
85
|
t: 's';
|
|
79
86
|
a: number;
|
|
@@ -223,6 +230,16 @@ declare class Ellipse extends Shape {
|
|
|
223
230
|
mutate(_cfg?: Partial<Cfg>): ShapeInterface;
|
|
224
231
|
computeBbox(): this;
|
|
225
232
|
}
|
|
233
|
+
declare class Circle extends Shape {
|
|
234
|
+
center: Point;
|
|
235
|
+
r: number;
|
|
236
|
+
constructor(w: number, h: number);
|
|
237
|
+
computeBbox(): this;
|
|
238
|
+
render(ctx: CanvasRenderingContext2D): void;
|
|
239
|
+
toSVG(): SVGElement;
|
|
240
|
+
toData(a: number, c: RGB): StepData;
|
|
241
|
+
mutate(_cfg?: Partial<Cfg>): ShapeInterface;
|
|
242
|
+
}
|
|
226
243
|
declare class Glyph extends Shape {
|
|
227
244
|
center: Point;
|
|
228
245
|
text: string;
|
|
@@ -283,4 +300,4 @@ interface ReplayResult {
|
|
|
283
300
|
}
|
|
284
301
|
declare function replayOutput(data: SerializedOutput): ReplayResult;
|
|
285
302
|
|
|
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 };
|
|
303
|
+
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
|
@@ -583,6 +583,59 @@ var Ellipse = class _Ellipse extends Shape {
|
|
|
583
583
|
return this;
|
|
584
584
|
}
|
|
585
585
|
};
|
|
586
|
+
var Circle = class _Circle extends Shape {
|
|
587
|
+
center;
|
|
588
|
+
r;
|
|
589
|
+
constructor(w, h) {
|
|
590
|
+
super(w, h);
|
|
591
|
+
this.center = Shape.randomPoint(w, h);
|
|
592
|
+
this.r = 1 + ~~(Math.random() * 20);
|
|
593
|
+
this.computeBbox();
|
|
594
|
+
}
|
|
595
|
+
computeBbox() {
|
|
596
|
+
this.bbox = {
|
|
597
|
+
left: this.center[0] - this.r,
|
|
598
|
+
top: this.center[1] - this.r,
|
|
599
|
+
width: 2 * this.r || 1,
|
|
600
|
+
height: 2 * this.r || 1
|
|
601
|
+
};
|
|
602
|
+
return this;
|
|
603
|
+
}
|
|
604
|
+
render(ctx) {
|
|
605
|
+
ctx.beginPath();
|
|
606
|
+
ctx.arc(this.center[0], this.center[1], this.r, 0, 2 * Math.PI);
|
|
607
|
+
ctx.fill();
|
|
608
|
+
}
|
|
609
|
+
toSVG() {
|
|
610
|
+
const node = document.createElementNS(SVGNS, "circle");
|
|
611
|
+
node.setAttribute("cx", String(this.center[0]));
|
|
612
|
+
node.setAttribute("cy", String(this.center[1]));
|
|
613
|
+
node.setAttribute("r", String(this.r));
|
|
614
|
+
return node;
|
|
615
|
+
}
|
|
616
|
+
toData(a, c) {
|
|
617
|
+
return { t: "c", a, c, cx: this.center[0], cy: this.center[1], r: this.r };
|
|
618
|
+
}
|
|
619
|
+
mutate(_cfg) {
|
|
620
|
+
const clone = new _Circle(0, 0);
|
|
621
|
+
clone.center = [this.center[0], this.center[1]];
|
|
622
|
+
clone.r = this.r;
|
|
623
|
+
switch (Math.floor(Math.random() * 2)) {
|
|
624
|
+
case 0: {
|
|
625
|
+
const angle = Math.random() * 2 * Math.PI;
|
|
626
|
+
const radius = Math.random() * 20;
|
|
627
|
+
clone.center[0] += ~~(radius * Math.cos(angle));
|
|
628
|
+
clone.center[1] += ~~(radius * Math.sin(angle));
|
|
629
|
+
break;
|
|
630
|
+
}
|
|
631
|
+
case 1:
|
|
632
|
+
clone.r += (Math.random() - 0.5) * 20;
|
|
633
|
+
clone.r = Math.max(1, ~~clone.r);
|
|
634
|
+
break;
|
|
635
|
+
}
|
|
636
|
+
return clone.computeBbox();
|
|
637
|
+
}
|
|
638
|
+
};
|
|
586
639
|
var Glyph = class _Glyph extends Shape {
|
|
587
640
|
center;
|
|
588
641
|
text;
|
|
@@ -912,6 +965,12 @@ function renderStep(data, ctx) {
|
|
|
912
965
|
ctx.fill();
|
|
913
966
|
break;
|
|
914
967
|
}
|
|
968
|
+
case "c": {
|
|
969
|
+
ctx.beginPath();
|
|
970
|
+
ctx.arc(data.cx, data.cy, data.r, 0, 2 * Math.PI);
|
|
971
|
+
ctx.fill();
|
|
972
|
+
break;
|
|
973
|
+
}
|
|
915
974
|
case "s": {
|
|
916
975
|
ctx.fillRect(data.cx - data.r, data.cy - data.r, 2 * data.r, 2 * data.r);
|
|
917
976
|
break;
|
|
@@ -953,6 +1012,13 @@ function stepToSVG(data) {
|
|
|
953
1012
|
node.setAttribute("ry", String(data.ry));
|
|
954
1013
|
break;
|
|
955
1014
|
}
|
|
1015
|
+
case "c": {
|
|
1016
|
+
node = document.createElementNS(SVGNS, "circle");
|
|
1017
|
+
node.setAttribute("cx", String(data.cx));
|
|
1018
|
+
node.setAttribute("cy", String(data.cy));
|
|
1019
|
+
node.setAttribute("r", String(data.r));
|
|
1020
|
+
break;
|
|
1021
|
+
}
|
|
956
1022
|
case "s": {
|
|
957
1023
|
node = document.createElementNS(SVGNS, "rect");
|
|
958
1024
|
node.setAttribute("x", String(data.cx - data.r));
|
|
@@ -1002,6 +1068,7 @@ function replayOutput(data) {
|
|
|
1002
1068
|
}
|
|
1003
1069
|
export {
|
|
1004
1070
|
Canvas,
|
|
1071
|
+
Circle,
|
|
1005
1072
|
Debug,
|
|
1006
1073
|
Ellipse,
|
|
1007
1074
|
Glyph,
|