@m2c2kit/addons 0.3.11 → 0.3.13

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
@@ -111,7 +111,7 @@ declare class Button extends Composite implements IText {
111
111
  };
112
112
  cornerRadius: number;
113
113
  fontSize: number;
114
- text: string;
114
+ private _text;
115
115
  private _fontColor;
116
116
  private backgroundPaint?;
117
117
  /**
@@ -126,6 +126,8 @@ declare class Button extends Composite implements IText {
126
126
  constructor(options: ButtonOptions);
127
127
  initialize(): void;
128
128
  dispose(): void;
129
+ get text(): string;
130
+ set text(text: string);
129
131
  get backgroundColor(): RgbaColor;
130
132
  set backgroundColor(backgroundColor: RgbaColor);
131
133
  get fontColor(): RgbaColor;
@@ -253,6 +255,8 @@ interface StrokeInteraction {
253
255
  position: Point;
254
256
  /** Device ISO8601 Timestamp of the interaction. */
255
257
  iso8601Timestamp: string;
258
+ /** Was the interaction's position interpolated? (clipped to DrawPad boundary) because the user drew out of bounds? @remarks Only StrokeMove and StrokeEnd can be interpolated. A StrokeStart position can never begin out of bounds. */
259
+ interpolated: boolean;
256
260
  }
257
261
  type DrawPadStroke = Array<StrokeInteraction>;
258
262
  interface DrawPadItem {
@@ -310,6 +314,7 @@ declare class DrawPad extends Composite {
310
314
  private initializeDrawArea;
311
315
  private dist;
312
316
  private handleTapDown;
317
+ private addInterpolatedStrokeMove;
313
318
  private handleTapLeave;
314
319
  private handleTapUpAny;
315
320
  private handlePointerMove;
@@ -365,6 +370,24 @@ declare class DrawPad extends Composite {
365
370
  * PNG format.
366
371
  */
367
372
  takeScreenshot(): string;
373
+ /**
374
+ * Determines whether a point is within the DrawPad.
375
+ *
376
+ * @param point - The point to check
377
+ * @returns True - if the point is within the DrawPad, false otherwise
378
+ */
379
+ private isPointWithinDrawPad;
380
+ /**
381
+ * Interpolates a point to the border of the DrawPad based on a line that
382
+ * crosses the DrawPad border. The line is formed by the current "out of
383
+ * bounds" point the and previous "within bounds" point.
384
+ *
385
+ * @param currentPoint - The current point
386
+ * @param previousPoint - The previous point
387
+ * @param drawPadSize - The size of the DrawPad
388
+ * @returns A new point on the border of the DrawPad
389
+ */
390
+ private interpolateToDrawPadBorder;
368
391
  private arrayBufferToBase64String;
369
392
  get backgroundColor(): RgbaColor;
370
393
  set backgroundColor(backgroundColor: RgbaColor);
package/dist/index.js CHANGED
@@ -258,7 +258,7 @@ class Button extends Composite {
258
258
  this.size = { width: 200, height: 50 };
259
259
  this.cornerRadius = 9;
260
260
  this.fontSize = 20;
261
- this.text = "";
261
+ this._text = "";
262
262
  this._fontColor = WebColors.White;
263
263
  if (options.text) {
264
264
  this.text = options.text;
@@ -266,10 +266,10 @@ class Button extends Composite {
266
266
  if (options.size) {
267
267
  this.size = options.size;
268
268
  }
269
- if (options.cornerRadius) {
269
+ if (options.cornerRadius !== void 0) {
270
270
  this.cornerRadius = options.cornerRadius;
271
271
  }
272
- if (options.fontSize) {
272
+ if (options.fontSize !== void 0) {
273
273
  this.fontSize = options.fontSize;
274
274
  }
275
275
  if (options.fontColor) {
@@ -308,6 +308,13 @@ class Button extends Composite {
308
308
  dispose() {
309
309
  CanvasKitHelpers.Dispose([this.backgroundPaint]);
310
310
  }
311
+ get text() {
312
+ return this._text;
313
+ }
314
+ set text(text) {
315
+ this._text = text;
316
+ this.needsInitialization = true;
317
+ }
311
318
  get backgroundColor() {
312
319
  return this._backgroundColor;
313
320
  }
@@ -674,8 +681,8 @@ class DrawPad extends Composite {
674
681
  this.drawArea.onTapUpAny(() => {
675
682
  this.handleTapUpAny();
676
683
  });
677
- this.drawArea.onTapLeave(() => {
678
- this.handleTapLeave();
684
+ this.drawArea.onTapLeave((e) => {
685
+ this.handleTapLeave(e);
679
686
  });
680
687
  }
681
688
  this.drawArea.fillColor = this.backgroundColor;
@@ -687,11 +694,10 @@ class DrawPad extends Composite {
687
694
  }
688
695
  handleTapDown(e) {
689
696
  if (this.isUserInteractionEnabled) {
690
- const drawShape = this.drawShape;
691
- if (!drawShape) {
692
- throw new Error("no draw shape");
697
+ if (!this.drawShape?.path) {
698
+ throw new Error("DrawPad.handleTapDown(): no drawShape.path");
693
699
  }
694
- const path = drawShape.path;
700
+ const path = this.drawShape.path;
695
701
  if (this.continuousDrawingOnly && path.subpaths.length !== 0) {
696
702
  const prevPoint = path.subpaths[path.subpaths.length - 1][path.subpaths[path.subpaths.length - 1].length - 1];
697
703
  const currentPoint = e.point;
@@ -700,6 +706,7 @@ class DrawPad extends Composite {
700
706
  return;
701
707
  }
702
708
  }
709
+ this.currentStrokesNotAllowed = false;
703
710
  this.isDrawingPointerDown = true;
704
711
  path.move(e.point);
705
712
  const drawPadEvent = {
@@ -712,13 +719,43 @@ class DrawPad extends Composite {
712
719
  {
713
720
  type: DrawPadEventType.StrokeStart,
714
721
  position: e.point,
715
- iso8601Timestamp: (/* @__PURE__ */ new Date()).toISOString()
722
+ iso8601Timestamp: (/* @__PURE__ */ new Date()).toISOString(),
723
+ interpolated: false
716
724
  }
717
725
  ]);
718
726
  this.raiseDrawPadEvent(drawPadEvent);
719
727
  }
720
728
  }
721
- handleTapLeave() {
729
+ addInterpolatedStrokeMove(point) {
730
+ const strokeCount = this.strokes.length;
731
+ const strokeInteractionCount = this.strokes[strokeCount - 1].length;
732
+ const previousPoint = this.strokes[this.strokes.length - 1][strokeInteractionCount - 1].position;
733
+ const interpolatedPoint = this.interpolateToDrawPadBorder(
734
+ point,
735
+ previousPoint,
736
+ this.size
737
+ );
738
+ if (!this.drawShape?.path) {
739
+ throw new Error("DrawPad.addInterpolatedStrokeMove(): no drawShape.path");
740
+ }
741
+ const path = this.drawShape.path;
742
+ path.addLine(interpolatedPoint);
743
+ const drawPadEvent = {
744
+ type: DrawPadEventType.StrokeMove,
745
+ target: this,
746
+ handled: false,
747
+ position: interpolatedPoint
748
+ };
749
+ this.strokes[strokeCount - 1].push({
750
+ type: DrawPadEventType.StrokeMove,
751
+ position: interpolatedPoint,
752
+ iso8601Timestamp: (/* @__PURE__ */ new Date()).toISOString(),
753
+ interpolated: true
754
+ });
755
+ this.raiseDrawPadEvent(drawPadEvent);
756
+ return interpolatedPoint;
757
+ }
758
+ handleTapLeave(e) {
722
759
  if (this.currentStrokesNotAllowed) {
723
760
  this.isDrawingPointerDown = false;
724
761
  return;
@@ -727,6 +764,12 @@ class DrawPad extends Composite {
727
764
  this.isDrawingPointerDown = false;
728
765
  const strokeCount = this.strokes.length;
729
766
  const strokeInteractionCount = this.strokes[strokeCount - 1].length;
767
+ let pointWasInterpolated = false;
768
+ let point = e.point;
769
+ if (!this.isPointWithinDrawPad(e.point, this.size)) {
770
+ point = this.addInterpolatedStrokeMove(e.point);
771
+ pointWasInterpolated = true;
772
+ }
730
773
  const drawPadEvent = {
731
774
  type: DrawPadEventType.StrokeEnd,
732
775
  position: this.strokes[strokeCount - 1][strokeInteractionCount - 1].position,
@@ -735,10 +778,12 @@ class DrawPad extends Composite {
735
778
  };
736
779
  this.strokes[strokeCount - 1].push({
737
780
  type: DrawPadEventType.StrokeEnd,
738
- position: this.strokes[strokeCount - 1][strokeInteractionCount - 1].position,
739
- iso8601Timestamp: (/* @__PURE__ */ new Date()).toISOString()
781
+ position: pointWasInterpolated ? point : this.strokes[strokeCount - 1][strokeInteractionCount - 1].position,
782
+ iso8601Timestamp: (/* @__PURE__ */ new Date()).toISOString(),
783
+ interpolated: pointWasInterpolated
740
784
  });
741
785
  this.raiseDrawPadEvent(drawPadEvent);
786
+ this.currentStrokesNotAllowed = true;
742
787
  } else {
743
788
  this.pointerIsDownAndPointerLeftDrawAreaWhenDown = true;
744
789
  }
@@ -762,18 +807,18 @@ class DrawPad extends Composite {
762
807
  this.strokes[strokeCount - 1].push({
763
808
  type: DrawPadEventType.StrokeEnd,
764
809
  position: this.strokes[strokeCount - 1][strokeInteractionCount - 1].position,
765
- iso8601Timestamp: (/* @__PURE__ */ new Date()).toISOString()
810
+ iso8601Timestamp: (/* @__PURE__ */ new Date()).toISOString(),
811
+ interpolated: false
766
812
  });
767
813
  this.raiseDrawPadEvent(drawPadEvent);
768
814
  }
769
815
  }
770
816
  handlePointerMove(e) {
771
817
  if (this.isUserInteractionEnabled && this.isDrawingPointerDown) {
772
- const drawShape = this.drawShape;
773
- if (!drawShape) {
774
- throw new Error("no draw shape");
818
+ if (!this.drawShape?.path) {
819
+ throw new Error("DrawPad.handlePointerMove(): no drawShape.path");
775
820
  }
776
- const path = drawShape.path;
821
+ const path = this.drawShape.path;
777
822
  if (this.isDrawingPointerDown && !this.pointerIsDownAndPointerLeftDrawAreaWhenDown) {
778
823
  path.addLine(e.point);
779
824
  }
@@ -791,7 +836,8 @@ class DrawPad extends Composite {
791
836
  this.strokes[strokeCount - 1].push({
792
837
  type: DrawPadEventType.StrokeMove,
793
838
  position: e.point,
794
- iso8601Timestamp: (/* @__PURE__ */ new Date()).toISOString()
839
+ iso8601Timestamp: (/* @__PURE__ */ new Date()).toISOString(),
840
+ interpolated: false
795
841
  });
796
842
  this.raiseDrawPadEvent(drawPadEvent);
797
843
  }
@@ -820,11 +866,10 @@ class DrawPad extends Composite {
820
866
  * Removes all strokes from the DrawPad.
821
867
  */
822
868
  clear() {
823
- const drawShape = this.drawShape;
824
- if (!drawShape) {
825
- throw new Error("no draw shape");
869
+ if (!this.drawShape?.path) {
870
+ throw new Error("DrawPad.clear(): no drawShape.path");
826
871
  }
827
- const path = drawShape.path;
872
+ const path = this.drawShape.path;
828
873
  path.clear();
829
874
  this.strokes = new Array();
830
875
  }
@@ -994,13 +1039,9 @@ class DrawPad extends Composite {
994
1039
  * PNG format.
995
1040
  */
996
1041
  takeScreenshot() {
997
- const surface = this.game.surface;
998
- if (!surface) {
999
- throw new Error("no surface");
1000
- }
1001
1042
  const drawArea = this.drawArea;
1002
1043
  if (!drawArea) {
1003
- throw new Error("no draw area");
1044
+ throw new Error("DrawPad.takeScreenshot(): no drawArea");
1004
1045
  }
1005
1046
  const sx = (drawArea.absolutePosition.x - drawArea.size.width / 2) * Globals.canvasScale;
1006
1047
  const sy = (drawArea.absolutePosition.y - drawArea.size.height / 2) * Globals.canvasScale;
@@ -1021,15 +1062,85 @@ class DrawPad extends Composite {
1021
1062
  pixelData.length / sh
1022
1063
  );
1023
1064
  if (!croppedImage) {
1024
- throw new Error("no cropped image");
1065
+ throw new Error("DrawPad.takeScreenshot(): no croppedImage");
1025
1066
  }
1026
1067
  const bytes = croppedImage.encodeToBytes();
1027
1068
  if (!bytes) {
1028
- throw new Error("no bytes");
1069
+ throw new Error(
1070
+ "DrawPad.takeScreenshot(): croppedImage.encodeToBytes() failed"
1071
+ );
1029
1072
  }
1030
1073
  croppedImage.delete();
1031
1074
  return this.arrayBufferToBase64String(bytes);
1032
1075
  }
1076
+ /**
1077
+ * Determines whether a point is within the DrawPad.
1078
+ *
1079
+ * @param point - The point to check
1080
+ * @returns True - if the point is within the DrawPad, false otherwise
1081
+ */
1082
+ isPointWithinDrawPad(point, drawPadSize) {
1083
+ return point.x >= 0 && point.x <= drawPadSize.width && point.y >= 0 && point.y <= drawPadSize.height;
1084
+ }
1085
+ /**
1086
+ * Interpolates a point to the border of the DrawPad based on a line that
1087
+ * crosses the DrawPad border. The line is formed by the current "out of
1088
+ * bounds" point the and previous "within bounds" point.
1089
+ *
1090
+ * @param currentPoint - The current point
1091
+ * @param previousPoint - The previous point
1092
+ * @param drawPadSize - The size of the DrawPad
1093
+ * @returns A new point on the border of the DrawPad
1094
+ */
1095
+ interpolateToDrawPadBorder(currentPoint, previousPoint, drawPadSize) {
1096
+ const slope = (currentPoint.y - previousPoint.y) / (currentPoint.x - previousPoint.x);
1097
+ const intercept = currentPoint.y - slope * currentPoint.x;
1098
+ const newPoint = { x: 0, y: 0 };
1099
+ if (!Number.isFinite(slope)) {
1100
+ newPoint.x = currentPoint.x;
1101
+ if (currentPoint.y - previousPoint.y > 0) {
1102
+ newPoint.y = drawPadSize.height;
1103
+ return newPoint;
1104
+ }
1105
+ if (currentPoint.y - previousPoint.y < 0) {
1106
+ newPoint.y = 0;
1107
+ return newPoint;
1108
+ }
1109
+ }
1110
+ const yLeft = slope * 0 + intercept;
1111
+ const yRight = slope * drawPadSize.width + intercept;
1112
+ if (yLeft >= 0 && yLeft <= drawPadSize.height) {
1113
+ if (currentPoint.x - previousPoint.x < 0) {
1114
+ newPoint.x = 0;
1115
+ newPoint.y = yLeft;
1116
+ return newPoint;
1117
+ }
1118
+ }
1119
+ if (yRight >= 0 && yRight <= drawPadSize.height) {
1120
+ if (currentPoint.x - previousPoint.x > 0) {
1121
+ newPoint.x = drawPadSize.width;
1122
+ newPoint.y = yRight;
1123
+ return newPoint;
1124
+ }
1125
+ }
1126
+ const xTop = (0 - intercept) / slope;
1127
+ const xBottom = (drawPadSize.height - intercept) / slope;
1128
+ if (xTop >= 0 && xTop <= drawPadSize.width) {
1129
+ if (currentPoint.y - previousPoint.y < 0) {
1130
+ newPoint.x = xTop;
1131
+ newPoint.y = 0;
1132
+ return newPoint;
1133
+ }
1134
+ }
1135
+ if (xBottom >= 0 && xBottom <= drawPadSize.width) {
1136
+ if (currentPoint.y - previousPoint.y > 0) {
1137
+ newPoint.x = xBottom;
1138
+ newPoint.y = drawPadSize.height;
1139
+ return newPoint;
1140
+ }
1141
+ }
1142
+ return currentPoint;
1143
+ }
1033
1144
  arrayBufferToBase64String(buffer) {
1034
1145
  let binary = "";
1035
1146
  const bytes = new Uint8Array(buffer);
@@ -1074,7 +1185,7 @@ class DrawPad extends Composite {
1074
1185
  this.needsInitialization = true;
1075
1186
  }
1076
1187
  duplicate(newName) {
1077
- throw new Error("Method not implemented.");
1188
+ throw new Error("DrawPad.duplicate(): Method not implemented.");
1078
1189
  }
1079
1190
  }
1080
1191
 
@@ -1742,7 +1853,7 @@ class Instructions extends Story {
1742
1853
  }
1743
1854
  }
1744
1855
 
1745
- console.log("\u26AA @m2c2kit/addons version 0.3.11 (60325bea)");
1856
+ console.log("\u26AA @m2c2kit/addons version 0.3.13 (d8a00e86)");
1746
1857
 
1747
1858
  export { Button, Dialog, DialogResult, DrawPad, DrawPadEventType, DrawPadItemEventType, Grid, Instructions, VirtualKeyboard };
1748
1859
  //# sourceMappingURL=index.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@m2c2kit/addons",
3
- "version": "0.3.11",
3
+ "version": "0.3.13",
4
4
  "description": "Additions to m2c2kit core functionality, such as button, grid, and instructions",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",
@@ -35,11 +35,11 @@
35
35
  "devDependencies": {
36
36
  "@rollup/plugin-replace": "5.0.5",
37
37
  "rimraf": "5.0.5",
38
- "rollup": "4.6.1",
38
+ "rollup": "4.9.2",
39
39
  "rollup-plugin-copy": "3.5.0",
40
40
  "rollup-plugin-dts": "6.1.0",
41
41
  "rollup-plugin-esbuild": "6.1.0",
42
- "typescript": "5.3.2"
42
+ "typescript": "5.3.3"
43
43
  },
44
44
  "engines": {
45
45
  "node": ">=18"