@slithy/prim-lib 0.2.0 → 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 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
@@ -164,7 +164,9 @@ var Canvas = class _Canvas {
164
164
  }
165
165
  return new Promise((resolve) => {
166
166
  const img = new Image();
167
- img.crossOrigin = "anonymous";
167
+ if (!url.startsWith("blob:") && !url.startsWith("data:")) {
168
+ img.crossOrigin = "anonymous";
169
+ }
168
170
  img.src = url;
169
171
  img.onload = () => {
170
172
  const w = img.naturalWidth;
@@ -581,6 +583,59 @@ var Ellipse = class _Ellipse extends Shape {
581
583
  return this;
582
584
  }
583
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
+ };
584
639
  var Glyph = class _Glyph extends Shape {
585
640
  center;
586
641
  text;
@@ -910,6 +965,12 @@ function renderStep(data, ctx) {
910
965
  ctx.fill();
911
966
  break;
912
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
+ }
913
974
  case "s": {
914
975
  ctx.fillRect(data.cx - data.r, data.cy - data.r, 2 * data.r, 2 * data.r);
915
976
  break;
@@ -951,6 +1012,13 @@ function stepToSVG(data) {
951
1012
  node.setAttribute("ry", String(data.ry));
952
1013
  break;
953
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
+ }
954
1022
  case "s": {
955
1023
  node = document.createElementNS(SVGNS, "rect");
956
1024
  node.setAttribute("x", String(data.cx - data.r));
@@ -1000,6 +1068,7 @@ function replayOutput(data) {
1000
1068
  }
1001
1069
  export {
1002
1070
  Canvas,
1071
+ Circle,
1003
1072
  Debug,
1004
1073
  Ellipse,
1005
1074
  Glyph,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@slithy/prim-lib",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "Core engine for primitive-based image reconstruction.",
5
5
  "type": "module",
6
6
  "exports": {