plotboilerplate 1.12.10 → 1.16.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.
Files changed (213) hide show
  1. package/README.md +5 -1
  2. package/changelog.md +50 -0
  3. package/dist/index.esm.js +999 -510
  4. package/dist/index.esm.js.map +1 -1
  5. package/dist/index.esm.min.js +1 -1
  6. package/dist/index.esm.min.js.map +1 -1
  7. package/dist/plotboilerplate.browser.min.js +1 -1
  8. package/dist/plotboilerplate.browser.min.js.map +1 -1
  9. package/dist/plotboilerplate.js +1013 -509
  10. package/dist/plotboilerplate.min.js +1 -1
  11. package/package.json +1 -1
  12. package/src/cjs/BezierPath.d.ts +45 -2
  13. package/src/cjs/BezierPath.js +114 -18
  14. package/src/cjs/BezierPath.js.map +1 -1
  15. package/src/cjs/Circle.d.ts +25 -10
  16. package/src/cjs/Circle.js +38 -33
  17. package/src/cjs/Circle.js.map +1 -1
  18. package/src/cjs/CircleSector.d.ts +27 -13
  19. package/src/cjs/CircleSector.js +38 -23
  20. package/src/cjs/CircleSector.js.map +1 -1
  21. package/src/cjs/CubicBezierCurve.d.ts +17 -1
  22. package/src/cjs/CubicBezierCurve.js +78 -79
  23. package/src/cjs/CubicBezierCurve.js.map +1 -1
  24. package/src/cjs/KeyHandler.d.ts +10 -3
  25. package/src/cjs/KeyHandler.js +28 -23
  26. package/src/cjs/KeyHandler.js.map +1 -1
  27. package/src/cjs/Line.d.ts +2 -1
  28. package/src/cjs/Line.js +19 -18
  29. package/src/cjs/Line.js.map +1 -1
  30. package/src/cjs/MouseHandler.js +43 -53
  31. package/src/cjs/MouseHandler.js.map +1 -1
  32. package/src/cjs/PBImage.d.ts +16 -1
  33. package/src/cjs/PBImage.js +13 -3
  34. package/src/cjs/PBImage.js.map +1 -1
  35. package/src/cjs/PBText.d.ts +130 -0
  36. package/src/cjs/PBText.js +76 -0
  37. package/src/cjs/PBText.js.map +1 -0
  38. package/src/cjs/PlotBoilerplate.d.ts +2 -1
  39. package/src/cjs/PlotBoilerplate.js +44 -16
  40. package/src/cjs/PlotBoilerplate.js.map +1 -1
  41. package/src/cjs/Polygon.d.ts +37 -1
  42. package/src/cjs/Polygon.js +131 -54
  43. package/src/cjs/Polygon.js.map +1 -1
  44. package/src/cjs/Triangle.d.ts +16 -1
  45. package/src/cjs/Triangle.js +50 -54
  46. package/src/cjs/Triangle.js.map +1 -1
  47. package/src/cjs/VEllipse.d.ts +16 -1
  48. package/src/cjs/VEllipse.js +25 -12
  49. package/src/cjs/VEllipse.js.map +1 -1
  50. package/src/cjs/VEllipseSector.d.ts +15 -1
  51. package/src/cjs/VEllipseSector.js +11 -1
  52. package/src/cjs/VEllipseSector.js.map +1 -1
  53. package/src/cjs/Vector.d.ts +3 -1
  54. package/src/cjs/Vector.js +32 -36
  55. package/src/cjs/Vector.js.map +1 -1
  56. package/src/cjs/VertTuple.d.ts +15 -1
  57. package/src/cjs/VertTuple.js +14 -19
  58. package/src/cjs/VertTuple.js.map +1 -1
  59. package/src/cjs/Vertex.d.ts +66 -4
  60. package/src/cjs/Vertex.js +99 -17
  61. package/src/cjs/Vertex.js.map +1 -1
  62. package/src/cjs/VertexListeners.d.ts +12 -8
  63. package/src/cjs/VertexListeners.js +18 -25
  64. package/src/cjs/VertexListeners.js.map +1 -1
  65. package/src/cjs/draw.d.ts +30 -2
  66. package/src/cjs/draw.js +51 -6
  67. package/src/cjs/draw.js.map +1 -1
  68. package/src/cjs/drawgl.d.ts +29 -2
  69. package/src/cjs/drawgl.js +21 -4
  70. package/src/cjs/drawgl.js.map +1 -1
  71. package/src/cjs/drawutilssvg.d.ts +25 -7
  72. package/src/cjs/drawutilssvg.js +62 -14
  73. package/src/cjs/drawutilssvg.js.map +1 -1
  74. package/src/cjs/entry.js +1 -0
  75. package/src/cjs/index.d.ts +1 -0
  76. package/src/cjs/index.js +1 -0
  77. package/src/cjs/index.js.map +1 -1
  78. package/src/cjs/interfaces/DrawLib.d.ts +27 -6
  79. package/src/cjs/interfaces/DrawLib.js +3 -0
  80. package/src/cjs/interfaces/DrawLib.js.map +1 -1
  81. package/src/cjs/interfaces/core.d.ts +9 -1
  82. package/src/cjs/interfaces/core.js +1 -0
  83. package/src/cjs/interfaces/core.js.map +1 -1
  84. package/src/cjs/utils/creategui.js +1 -1
  85. package/src/cjs/utils/findInVertexArray.d.ts +19 -0
  86. package/src/cjs/utils/findInVertexArray.js +29 -0
  87. package/src/cjs/utils/findInVertexArray.js.map +1 -0
  88. package/src/cjs/utils/helpers/BezierPathInteractionHelper.d.ts +15 -0
  89. package/src/cjs/utils/helpers/BezierPathInteractionHelper.js +55 -28
  90. package/src/cjs/utils/helpers/BezierPathInteractionHelper.js.map +1 -1
  91. package/src/cjs/utils/io/DndFileHandler.d.ts +63 -0
  92. package/src/cjs/utils/io/DndFileHandler.js +112 -0
  93. package/src/cjs/utils/io/DndFileHandler.js.map +1 -0
  94. package/src/cjs/utils/io/FileDrop.d.ts +98 -0
  95. package/src/cjs/utils/io/FileDrop.js +142 -0
  96. package/src/cjs/utils/io/FileDrop.js.map +1 -0
  97. package/src/esm/BezierPath.d.ts +45 -2
  98. package/src/esm/BezierPath.js +114 -18
  99. package/src/esm/BezierPath.js.map +1 -1
  100. package/src/esm/Circle.d.ts +25 -10
  101. package/src/esm/Circle.js +38 -33
  102. package/src/esm/Circle.js.map +1 -1
  103. package/src/esm/CircleSector.d.ts +27 -13
  104. package/src/esm/CircleSector.js +38 -23
  105. package/src/esm/CircleSector.js.map +1 -1
  106. package/src/esm/CubicBezierCurve.d.ts +17 -1
  107. package/src/esm/CubicBezierCurve.js +78 -79
  108. package/src/esm/CubicBezierCurve.js.map +1 -1
  109. package/src/esm/KeyHandler.d.ts +10 -3
  110. package/src/esm/KeyHandler.js +28 -23
  111. package/src/esm/KeyHandler.js.map +1 -1
  112. package/src/esm/Line.d.ts +2 -1
  113. package/src/esm/Line.js +19 -18
  114. package/src/esm/Line.js.map +1 -1
  115. package/src/esm/MouseHandler.js +43 -53
  116. package/src/esm/MouseHandler.js.map +1 -1
  117. package/src/esm/PBImage.d.ts +16 -1
  118. package/src/esm/PBImage.js +13 -3
  119. package/src/esm/PBImage.js.map +1 -1
  120. package/src/esm/PBText.d.ts +130 -0
  121. package/src/esm/PBText.js +71 -0
  122. package/src/esm/PBText.js.map +1 -0
  123. package/src/esm/PlotBoilerplate.d.ts +2 -1
  124. package/src/esm/PlotBoilerplate.js +44 -16
  125. package/src/esm/PlotBoilerplate.js.map +1 -1
  126. package/src/esm/Polygon.d.ts +37 -1
  127. package/src/esm/Polygon.js +131 -54
  128. package/src/esm/Polygon.js.map +1 -1
  129. package/src/esm/Triangle.d.ts +16 -1
  130. package/src/esm/Triangle.js +50 -54
  131. package/src/esm/Triangle.js.map +1 -1
  132. package/src/esm/VEllipse.d.ts +16 -1
  133. package/src/esm/VEllipse.js +25 -12
  134. package/src/esm/VEllipse.js.map +1 -1
  135. package/src/esm/VEllipseSector.d.ts +15 -1
  136. package/src/esm/VEllipseSector.js +11 -1
  137. package/src/esm/VEllipseSector.js.map +1 -1
  138. package/src/esm/Vector.d.ts +3 -1
  139. package/src/esm/Vector.js +32 -36
  140. package/src/esm/Vector.js.map +1 -1
  141. package/src/esm/VertTuple.d.ts +15 -1
  142. package/src/esm/VertTuple.js +14 -19
  143. package/src/esm/VertTuple.js.map +1 -1
  144. package/src/esm/Vertex.d.ts +66 -4
  145. package/src/esm/Vertex.js +99 -17
  146. package/src/esm/Vertex.js.map +1 -1
  147. package/src/esm/VertexListeners.d.ts +12 -8
  148. package/src/esm/VertexListeners.js +18 -25
  149. package/src/esm/VertexListeners.js.map +1 -1
  150. package/src/esm/draw.d.ts +30 -2
  151. package/src/esm/draw.js +51 -6
  152. package/src/esm/draw.js.map +1 -1
  153. package/src/esm/drawgl.d.ts +29 -2
  154. package/src/esm/drawgl.js +21 -4
  155. package/src/esm/drawgl.js.map +1 -1
  156. package/src/esm/drawutilssvg.d.ts +25 -7
  157. package/src/esm/drawutilssvg.js +62 -14
  158. package/src/esm/drawutilssvg.js.map +1 -1
  159. package/src/esm/entry.js +1 -0
  160. package/src/esm/index.d.ts +1 -0
  161. package/src/esm/index.js +1 -0
  162. package/src/esm/index.js.map +1 -1
  163. package/src/esm/interfaces/DrawLib.d.ts +27 -6
  164. package/src/esm/interfaces/DrawLib.js +3 -0
  165. package/src/esm/interfaces/DrawLib.js.map +1 -1
  166. package/src/esm/interfaces/core.d.ts +9 -1
  167. package/src/esm/interfaces/core.js +1 -0
  168. package/src/esm/interfaces/core.js.map +1 -1
  169. package/src/esm/utils/findInVertexArray.d.ts +19 -0
  170. package/src/esm/utils/findInVertexArray.js +25 -0
  171. package/src/esm/utils/findInVertexArray.js.map +1 -0
  172. package/src/esm/utils/helpers/BezierPathInteractionHelper.d.ts +15 -0
  173. package/src/esm/utils/helpers/BezierPathInteractionHelper.js +54 -28
  174. package/src/esm/utils/helpers/BezierPathInteractionHelper.js.map +1 -1
  175. package/src/esm/utils/io/DndFileHandler.d.ts +63 -0
  176. package/src/esm/utils/io/DndFileHandler.js +106 -0
  177. package/src/esm/utils/io/DndFileHandler.js.map +1 -0
  178. package/src/esm/utils/io/FileDrop.d.ts +98 -0
  179. package/src/esm/utils/io/FileDrop.js +136 -0
  180. package/src/esm/utils/io/FileDrop.js.map +1 -0
  181. package/src/ts/BezierPath.ts +133 -16
  182. package/src/ts/Circle.ts +213 -200
  183. package/src/ts/CircleSector.ts +171 -141
  184. package/src/ts/CubicBezierCurve.ts +777 -810
  185. package/src/ts/KeyHandler.ts +221 -212
  186. package/src/ts/Line.ts +77 -80
  187. package/src/ts/MouseHandler.ts +356 -368
  188. package/src/ts/PBImage.ts +81 -68
  189. package/src/ts/PBText.ts +166 -0
  190. package/src/ts/PlotBoilerplate.ts +48 -20
  191. package/src/ts/Polygon.ts +519 -451
  192. package/src/ts/Triangle.ts +484 -489
  193. package/src/ts/VEllipse.ts +36 -11
  194. package/src/ts/VEllipseSector.ts +20 -1
  195. package/src/ts/Vector.ts +164 -160
  196. package/src/ts/VertTuple.ts +333 -338
  197. package/src/ts/Vertex.ts +117 -16
  198. package/src/ts/VertexListeners.ts +248 -266
  199. package/src/ts/draw.ts +71 -8
  200. package/src/ts/drawgl.ts +48 -8
  201. package/src/ts/drawutilssvg.ts +94 -17
  202. package/src/ts/index.ts +1 -0
  203. package/src/ts/interfaces/DrawLib.ts +30 -4
  204. package/src/ts/interfaces/core.ts +9 -0
  205. package/src/ts/utils/findInVertexArray.ts +28 -0
  206. package/src/ts/utils/helpers/BezierPathInteractionHelper.ts +61 -30
  207. package/src/ts/utils/io/FileDrop.ts +178 -0
  208. package/dist/plotboilerplate.browser.min.js.LICENSE.txt +0 -8
  209. package/src/ts/utils/algorithms/clearPolygonDuplicateVertices.ts~ +0 -5
  210. package/src/ts/utils/algorithms/findPolygonSelfIntersections.ts~ +0 -45
  211. package/src/ts/utils/algorithms/sh-tmp.ts~ +0 -59
  212. package/src/ts/utils/algorithms/splitPolygonToNonIntersecting.ts~ +0 -113
  213. package/src/ts/utils/algorithms/sutherlandHodgman.ts~ +0 -88
package/src/ts/drawgl.ts CHANGED
@@ -12,7 +12,16 @@
12
12
  import { CubicBezierCurve } from "./CubicBezierCurve";
13
13
  import { Polygon } from "./Polygon";
14
14
  import { Vertex } from "./Vertex";
15
- import { DrawLib, XYCoords, SVGPathParams, SVGSerializable, UID, DrawLibConfiguration } from "./interfaces";
15
+ import {
16
+ DrawLib,
17
+ XYCoords,
18
+ SVGPathParams,
19
+ SVGSerializable,
20
+ UID,
21
+ DrawLibConfiguration,
22
+ FontStyle,
23
+ FontWeight
24
+ } from "./interfaces";
16
25
 
17
26
  /**
18
27
  * @classdesc A wrapper class for basic drawing operations. This is the WebGL
@@ -471,9 +480,9 @@ export class drawutilsgl implements DrawLib<void> {
471
480
  * @param {string} color - The color to use.
472
481
  * @param {number=1} lineWidth - (optional) The line with to use (default is 1).
473
482
  **/
474
- rect(position: XYCoords, width: number, height: number, color: string, lineWidth?: number): void {
483
+ rect(position: XYCoords, width: number, height: number, color: string, lineWidth?: number): void {
475
484
  // NOT YET IMPLEMENTED
476
- }
485
+ }
477
486
 
478
487
  /**
479
488
  * Draw a grid of horizontal and vertical lines with the given (CSS-) color.
@@ -655,7 +664,40 @@ export class drawutilsgl implements DrawLib<void> {
655
664
  // NOT YET IMPLEMENTED
656
665
  }
657
666
 
658
- text(text: string, x: number, y: number, options?: { color?: string }) {
667
+ /**
668
+ * Draw a text at the given relative position.
669
+ *
670
+ * @method text
671
+ * @param {string} text - The text to draw.
672
+ * @param {number} x - The x-position to draw the text at.
673
+ * @param {number} y - The y-position to draw the text at.
674
+ * @param {string=} options.color - The Color to use.
675
+ * @param {string=} options.fontFamily - The font family to use.
676
+ * @param {number=} options.fontSize - The font size (in pixels) to use.
677
+ * @param {FontStyle=} options.fontStyle - The font style to use.
678
+ * @param {FontWeight=} options.fontWeight - The font weight to use.
679
+ * @param {number=} options.lineHeight - The line height (in pixels) to use.
680
+ * @param {number=} options.rotation - The (optional) rotation in radians.
681
+ * @param {string=} options.textAlign - The text align to use. According to the specifiactions (https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/textAlign) valid values are `"left" || "right" || "center" || "start" || "end"`.
682
+ * @return {void}
683
+ * @instance
684
+ * @memberof drawutils
685
+ */
686
+ text(
687
+ text: string,
688
+ x: number,
689
+ y: number,
690
+ options?: {
691
+ color?: string;
692
+ fontFamily?: string;
693
+ fontSize?: number;
694
+ fontStyle?: FontStyle;
695
+ fontWeight?: FontWeight;
696
+ lineHeight?: number;
697
+ textAlign?: CanvasRenderingContext2D["textAlign"];
698
+ rotation?: number;
699
+ }
700
+ ) {
659
701
  // NOT YET IMPLEMENTED
660
702
  }
661
703
 
@@ -667,14 +709,12 @@ export class drawutilsgl implements DrawLib<void> {
667
709
  * @param {number} x - The x-position to draw the text at.
668
710
  * @param {number} y - The y-position to draw the text at.
669
711
  * @param {number=} rotation - The (aoptional) rotation in radians.
712
+ * @param {string="black"} color - The color to use (default is black).
670
713
  * @return {void}
671
714
  * @instance
672
715
  * @memberof drawutils
673
716
  */
674
- // +---------------------------------------------------------------------------------
675
- // | Draw a non-scaling text label at the given position.
676
- // +-------------------------------
677
- label(text: string, x: number, y: number, rotation: number) {
717
+ label(text: string, x: number, y: number, rotation?: number, color?: string) {
678
718
  // NOT YET IMPLEMENTED
679
719
  }
680
720
 
@@ -26,14 +26,30 @@
26
26
  * @modified 2021-03-31 Added 'ellipseSector' the the class names.
27
27
  * @modified 2021-03-31 Implemented buffering using a buffer <g> node and the beginDrawCycle and endDrawCycle methods.
28
28
  * @modified 2021-05-31 Added the `setConfiguration` function from `DrawLib`.
29
- * @version 1.3.0
29
+ * @modified 2021-11-15 Adding more parameters tot the `text()` function: fontSize, textAlign, fontFamily, lineHeight.
30
+ * @modified 2021-11-19 Fixing the `label(text,x,y)` position.
31
+ * @modified 2021-11-19 Added the `color` param to the `label(...)` function.
32
+ * @version 1.4.0
30
33
  **/
31
34
 
32
35
  import { CircleSector } from "./CircleSector";
33
36
  import { CubicBezierCurve } from "./CubicBezierCurve";
34
37
  import { Polygon } from "./Polygon";
35
38
  import { Vertex } from "./Vertex";
36
- import { DrawConfig, DrawLib, DrawSettings, XYCoords, XYDimension, SVGPathParams, UID, DrawLibConfiguration } from "./interfaces";
39
+ import {
40
+ DrawConfig,
41
+ DrawLib,
42
+ DrawSettings,
43
+ XYCoords,
44
+ XYDimension,
45
+ SVGPathParams,
46
+ UID,
47
+ DrawLibConfiguration,
48
+ FontStyle,
49
+ FontWeight
50
+ } from "./interfaces";
51
+
52
+ const RAD_TO_DEG = 180 / Math.PI;
37
53
 
38
54
  /**
39
55
  * @classdesc A helper class for basic SVG drawing operations. This class should
@@ -203,7 +219,8 @@ export class drawutilssvg implements DrawLib<void | SVGElement> {
203
219
  "vertex": "Vertex",
204
220
  "line": "Line",
205
221
  "vector": "Vector",
206
- "image": "Image"
222
+ "image": "Image",
223
+ "text": "Text"
207
224
  };
208
225
  // Question: why isn't this working if the svgNode is created dynamically? (nodeStyle.sheet is null)
209
226
  const rules = [];
@@ -1065,29 +1082,82 @@ export class drawutilssvg implements DrawLib<void | SVGElement> {
1065
1082
  }
1066
1083
  if (!isOpen) d.push("Z");
1067
1084
  node.setAttribute("d", d.join(" "));
1068
- return this._bindFillDraw(node, "polyline", color, lineWidth || 1);
1085
+ return this._bindFillDraw(node, "polygon", color, lineWidth || 1);
1069
1086
  }
1070
1087
 
1071
1088
  /**
1072
- * Draw a text label at the given relative position.
1089
+ * Draw a text at the given relative position.
1073
1090
  *
1074
- * @method label
1091
+ * @method text
1075
1092
  * @param {string} text - The text to draw.
1076
1093
  * @param {number} x - The x-position to draw the text at.
1077
1094
  * @param {number} y - The y-position to draw the text at.
1078
- * @param {number=} rotation - The (optional) rotation in radians.
1095
+ * @param {string=} options.color - The Color to use.
1096
+ * @param {string=} options.fontFamily - The font family to use.
1097
+ * @param {number=} options.fontSize - The font size (in pixels) to use.
1098
+ * @param {FontStyle=} options.fontStyle - The font style to use.
1099
+ * @param {FontWeight=} options.fontWeight - The font weight to use.
1100
+ * @param {number=} options.lineHeight - The line height (in pixels) to use.
1101
+ * @param {number=} options.rotation - The (optional) rotation in radians.
1102
+ * @param {string=} options.textAlign - The text align to use. According to the specifiactions (https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/textAlign) valid values are `"left" || "right" || "center" || "start" || "end"`.
1079
1103
  * @return {void}
1080
1104
  * @instance
1081
- * @memberof drawutilssvg
1105
+ * @memberof drawutils
1082
1106
  */
1083
- text(text: string, x: number, y: number, options?: { color?: string }): SVGElement {
1107
+ text(
1108
+ text: string,
1109
+ x: number,
1110
+ y: number,
1111
+ options?: {
1112
+ color?: string;
1113
+ fontFamily?: string;
1114
+ fontSize?: number;
1115
+ fontStyle?: FontStyle;
1116
+ fontWeight?: FontWeight;
1117
+ lineHeight?: number;
1118
+ textAlign?: CanvasRenderingContext2D["textAlign"];
1119
+ rotation?: number;
1120
+ }
1121
+ ): SVGElement {
1084
1122
  options = options || {};
1085
1123
  const color: string = options.color || "black";
1086
-
1087
- const node: SVGElement = this.makeNode("text");
1088
- node.setAttribute("x", `${this._x(x)}`);
1089
- node.setAttribute("y", `${this._y(y)}`);
1090
- node.innerHTML = text;
1124
+ const lineHeight = (options.lineHeight ?? options.fontSize ?? 0) * this.scale.x;
1125
+ // https://www.w3.org/TR/SVG/text.html#TextAnchorProperty
1126
+ // start | middle | end
1127
+ const textAlign: "start" | "middle" | "end" =
1128
+ options.textAlign === "left" || options.textAlign === "start"
1129
+ ? "start"
1130
+ : options.textAlign === "center"
1131
+ ? "middle"
1132
+ : options.textAlign === "right" || options.textAlign === "end"
1133
+ ? "end"
1134
+ : "start";
1135
+ const transformOrigin = `${this._x(x)}px ${this._y(y)}px`;
1136
+ const translate = `translate(${this._x(x)} ${this._y(y) + lineHeight / 2})`;
1137
+ // Safari has a transform-origin/rotation bug.
1138
+ // It's essential to use rotate(r,x,y) here. "rotate(r)"" with transform-origin(x,y) won't do the job.
1139
+ // And rotate and translate cannot be used is combination on a text object.
1140
+ // So wrap the text inside a <g>, translate the <g>, and rotate the text inside.
1141
+ const rotate = options.rotation ? `rotate(${options.rotation * RAD_TO_DEG} 0 0)` : ``;
1142
+
1143
+ const node: SVGElement = this.makeNode("g");
1144
+ const curId = this.curId;
1145
+ this.curId = curId + "_text";
1146
+ const textNode: SVGElement = this.makeNode("text");
1147
+ node.appendChild(textNode);
1148
+ textNode.setAttribute("font-family", options.fontFamily); // May be undefined
1149
+ textNode.setAttribute("font-size", options.fontSize ? `${options.fontSize * this.scale.x}` : null);
1150
+ textNode.setAttribute("font-style", options.fontStyle ? `${options.fontStyle}` : null);
1151
+ textNode.setAttribute("font-weight", options.fontWeight ? `${options.fontWeight}` : null);
1152
+ textNode.setAttribute("text-anchor", textAlign);
1153
+ textNode.setAttribute("transform-origin", "0 0");
1154
+ textNode.setAttribute("transform", rotate);
1155
+ node.setAttribute("transform-origin", transformOrigin);
1156
+ node.setAttribute("transform", translate);
1157
+
1158
+ textNode.innerHTML = text;
1159
+ // Restore old ID
1160
+ this.curId = curId;
1091
1161
  return this._bindFillDraw(node, "text", color, 1);
1092
1162
  }
1093
1163
 
@@ -1099,16 +1169,23 @@ export class drawutilssvg implements DrawLib<void | SVGElement> {
1099
1169
  * @param {number} x - The x-position to draw the text at.
1100
1170
  * @param {number} y - The y-position to draw the text at.
1101
1171
  * @param {number=} rotation - The (optional) rotation in radians.
1172
+ * @param {string="black"} color - The color to use (default is black).
1102
1173
  * @return {void}
1103
1174
  * @instance
1104
1175
  * @memberof drawutilssvg
1105
1176
  */
1106
- label(text: string, x: number, y: number, rotation: number): SVGElement {
1177
+ label(text: string, x: number, y: number, rotation?: number, color?: string): SVGElement {
1107
1178
  const node: SVGElement = this.makeNode("text");
1108
1179
  // For some strange reason SVG rotation transforms use degrees instead of radians
1109
- node.setAttribute("transform", `translate(${this.offset.x},${this.offset.y}), rotate(${(rotation / Math.PI) * 180})`);
1180
+ node.setAttribute("transform", `translate(${x},${y}), rotate(${((rotation || 0) / Math.PI) * 180})`);
1181
+
1182
+ node.setAttribute("font-family", "Arial");
1183
+ node.setAttribute("font-size", "9pt");
1184
+ node.setAttribute("font-style", "normal");
1185
+ node.setAttribute("font-weight", "lighter");
1186
+
1110
1187
  node.innerHTML = text;
1111
- return this._bindFillDraw(node, "label", "black", null);
1188
+ return this._bindFillDraw(node, "label", color || "black", null);
1112
1189
  }
1113
1190
 
1114
1191
  /**
package/src/ts/index.ts CHANGED
@@ -13,6 +13,7 @@ export * from "./KeyHandler";
13
13
  export * from "./Line";
14
14
  export * from "./MouseHandler";
15
15
  export * from "./PBImage";
16
+ export * from "./PBText";
16
17
  export * from "./PlotBoilerplate";
17
18
  export * from "./Polygon";
18
19
  export * from "./SVGBuilder";
@@ -12,6 +12,9 @@
12
12
  * @modified 2021-03-30 Added the `endDrawCycle` function to `DrawLib`.
13
13
  * @modified 2021-05-31 Added the `drawLib.setConfiguration` function.
14
14
  * @modified 2021-05-31 Splitted the large interfaces.ts file into this one and others.
15
+ * @modified 2021-11-12 Added `text()` params fontSize, fontFamily, rotation, textAlign.
16
+ * @modified 2021-11-16 Added `text()` params fontWeight and fontStyle.
17
+ * @modified 2021-11-19 Added the `color` param to the `label(...)` function.
15
18
  **/
16
19
 
17
20
  import { Polygon } from "../Polygon";
@@ -27,6 +30,21 @@ export interface DrawLibConfiguration {
27
30
  blendMode?: 'source-over' | 'source-in' | 'source-out' | 'source-atop' | 'destination-over' | 'destination-in' | 'destination-out' | 'destination-atop' | 'lighter' | 'copy' | 'xor' | 'multiply' | 'screen' | 'overlay' | 'darken' | 'lighten' | 'color-dodge' | 'color-burn' | 'hard-light' | 'soft-light' | 'difference' | 'exclusion' | 'hue' | 'saturation' | 'color' | 'luminosity' | null;
28
31
  }
29
32
 
33
+ export type FontWeight = "normal" | "bold" | "bolder" | "lighter" | 100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900;
34
+
35
+ export type FontStyle = "normal" | "italic" | "oblique";
36
+
37
+ export interface FontOptions {
38
+ color?: string;
39
+ fontFamily?: string;
40
+ fontSize?: number;
41
+ fontStyle?: FontStyle;
42
+ fontWeight?: FontWeight;
43
+ lineHeight?: number;
44
+ textAlign?: CanvasRenderingContext2D["textAlign"];
45
+ rotation?: number;
46
+ }
47
+
30
48
  /**
31
49
  * An interface all drawing libraries must implement to be used with PlotBoilerplate.
32
50
  *
@@ -432,18 +450,25 @@ export interface DrawLib<R> {
432
450
  polyline: (vertices: Array<Vertex>, isOpen: boolean, color: string, lineWidth?: number) => R;
433
451
 
434
452
  /**
435
- * Draw a text label at the given relative position.
453
+ * Draw a text at the given relative position.
436
454
  *
437
455
  * @method text
438
456
  * @param {string} text - The text to draw.
439
457
  * @param {number} x - The x-position to draw the text at.
440
458
  * @param {number} y - The y-position to draw the text at.
441
- * @param {number=} rotation - The (optional) rotation in radians.
459
+ * @param {string=} options.color - The Color to use.
460
+ * @param {string=} options.fontFamily - The font family to use.
461
+ * @param {number=} options.fontSize - The font size (in pixels) to use.
462
+ * @param {FontStyle=} options.fontStyle - The font style to use.
463
+ * @param {FontWeight=} options.fontWeight - The font weight to use.
464
+ * @param {number=} options.lineHeight - The line height (in pixels) to use.
465
+ * @param {number=} options.rotation - The (optional) rotation in radians.
466
+ * @param {string=} options.textAlign - The text align to use. According to the specifiactions (https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/textAlign) valid values are `"left" || "right" || "center" || "start" || "end"`.
442
467
  * @return {void}
443
468
  * @instance
444
469
  * @memberof drawutils
445
470
  */
446
- text: (text: string, x: number, y: number, options?: { color?: string }) => R;
471
+ text: (text: string, x: number, y: number, options?: FontOptions) => R;
447
472
 
448
473
  /**
449
474
  * Draw a non-scaling text label at the given absolute position.
@@ -453,11 +478,12 @@ export interface DrawLib<R> {
453
478
  * @param {number} x - The x-position to draw the text at.
454
479
  * @param {number} y - The y-position to draw the text at.
455
480
  * @param {number=} rotation - The (aoptional) rotation in radians.
481
+ * @param {string="black"} color - The color to use (default is black).
456
482
  * @return {void}
457
483
  * @instance
458
484
  * @memberof drawutils
459
485
  */
460
- label: (text: string, x: number, y: number, rotation: number) => R;
486
+ label: (text: string, x: number, y: number, rotation?: number, color?: string) => R;
461
487
 
462
488
  /**
463
489
  * Draw an SVG-like path given by the specified path data.
@@ -13,6 +13,7 @@
13
13
  * @modified 2021-05-31 Added the `drawLib.setConfiguration` function.
14
14
  * @modified 2021-05-31 Splitted the large interfaces.ts file into this one and others.
15
15
  * @modified 2021-06-21 Added `IBounds.getCenter()`.
16
+ * @modified 2021-11-16 Added `text` options to the `DrawConfig`.
16
17
  **/
17
18
 
18
19
  import { Vertex } from "../Vertex";
@@ -28,6 +29,7 @@ import { BezierPath } from "../BezierPath";
28
29
  import { Line } from "../Line";
29
30
  import { PlotBoilerplate } from "../PlotBoilerplate";
30
31
  import { DrawLib } from "./DrawLib";
32
+ import { PBText } from "../PBText";
31
33
 
32
34
  /**
33
35
  * @classdesc Coordinates (x,y) on the plane.
@@ -73,6 +75,7 @@ export type Drawable =
73
75
  | Circle
74
76
  | CircleSector
75
77
  | PBImage
78
+ | PBText
76
79
  | VEllipse
77
80
  | VEllipseSector
78
81
  | Polygon
@@ -189,6 +192,12 @@ export interface DrawConfig {
189
192
  line: DrawSettings;
190
193
  vector: DrawSettings;
191
194
  image: DrawSettings;
195
+ text: {
196
+ color: string;
197
+ lineWidth: number;
198
+ fill?: boolean;
199
+ anchor?: boolean;
200
+ };
192
201
  }
193
202
 
194
203
  /** This is a fix for the problem, that the constructor's "name" attribute is not
@@ -0,0 +1,28 @@
1
+ /**
2
+ * @author Ikaros Kappler
3
+ * @date 2022-01-10
4
+ * @version 1.0.0
5
+ */
6
+
7
+ import { Vertex } from "../Vertex";
8
+ import { XYCoords } from "../interfaces";
9
+
10
+ /**
11
+ * In an array of vertices find the first occurence of a specific vertex, using
12
+ * and epsilon tolerance.
13
+ *
14
+ * @param {Array<Vertex>} vertexList - The vertex array to search in.
15
+ * @param {XYCoords} vertex - A vertex/position to search for. This can be any x-y-tuple and does not
16
+ * necessarily need to be an instance of Vertex.
17
+ * @param {number} epsilon - The epsilon tolerance to use (should be >= 0). This is a required param to avoid excessive
18
+ * is-undef checking if this function runs in a loop.
19
+ * @returns {boolean} - The first index of a vertex that matched or -1 if no such vertex could be found.
20
+ */
21
+ export const findInVertexArray = (vertexList: Array<Vertex>, vertex: XYCoords, epsilon: number): number => {
22
+ for (var i = 0; i < vertexList.length; i++) {
23
+ if (vertexList[i].distance(vertex) < epsilon) {
24
+ return i;
25
+ }
26
+ }
27
+ return -1;
28
+ };
@@ -118,7 +118,11 @@ export class BezierPathInteractionHelper {
118
118
  private currentA: Vertex; // Position on the curve
119
119
  private currentB: Vertex; // mouse/touch position
120
120
 
121
+ private _mouseHandler: MouseHandler;
122
+ private _touchHandler: AlloyFinger;
121
123
  private _keyHandler: KeyHandler;
124
+ private _mouseEnterListener: () => void;
125
+ private _mouseLeaveListener: () => void;
122
126
 
123
127
  /**
124
128
  * Pre: all paths must have been added to the PlotBoilerplate's drawable buffer (use the add(Drawable) function).
@@ -173,8 +177,16 @@ export class BezierPathInteractionHelper {
173
177
  this.addPath(paths[i]);
174
178
  }
175
179
 
176
- this._installMouseListener();
177
- this._installTouchListener();
180
+ this._mouseEnterListener = () => {
181
+ this.mouseIsOver = true;
182
+ };
183
+ this._mouseLeaveListener = () => {
184
+ this.mouseIsOver = false;
185
+ this._clearMoveEvent();
186
+ };
187
+
188
+ this._mouseHandler = this._installMouseListener();
189
+ this._touchHandler = this._installTouchListener();
178
190
  this._keyHandler = this._installKeyListener();
179
191
 
180
192
  // Paths might have changed by auto-adjustment.
@@ -249,6 +261,28 @@ export class BezierPathInteractionHelper {
249
261
  this._handleMoveEvent(this.currentB.x, this.currentB.y);
250
262
  }
251
263
 
264
+ /**
265
+ * This function should invalidate any installed listeners and invalidate this object.
266
+ * After calling this function the object might not hold valid data any more and
267
+ * should not be used any more.
268
+ *
269
+ * @method destroy
270
+ * @instance
271
+ * @memberof BezierPathInteractionHelper
272
+ * @return {void}
273
+ **/
274
+ destroy(): void {
275
+ for (var i = 0; i < this.paths.length; i++) {
276
+ this._removeDefaultPathListeners(this.paths[i]);
277
+ // removePathVertexDragStartListeners(this.paths[i], listener);
278
+ }
279
+ this.paths = [];
280
+ this.pb.canvas.removeEventListener("mouseenter", this._mouseEnterListener);
281
+ this.pb.canvas.removeEventListener("mouseleave", this._mouseLeaveListener);
282
+ this._mouseHandler.destroy();
283
+ this._keyHandler.destroy();
284
+ }
285
+
252
286
  // +---------------------------------------------------------------------------------
253
287
  // | A helper function to locate a given path instance inside the array.
254
288
  // |
@@ -335,6 +369,7 @@ export class BezierPathInteractionHelper {
335
369
  curStartControl = path.bezierCurves[i + 1].startControlPoint;
336
370
  }
337
371
  } else {
372
+ // TODO: remove drag listener from removed vertex!
338
373
  deletedVertIndices.push(i);
339
374
  }
340
375
  }
@@ -361,6 +396,7 @@ export class BezierPathInteractionHelper {
361
396
  private _replacePathAt(pathIndex: number, newPath: BezierPath): void {
362
397
  const oldPath: BezierPath = this.paths[pathIndex];
363
398
  this.pb.remove(oldPath, false, true); // Remove with vertices
399
+ oldPath.destroy();
364
400
  this._removeDefaultPathListeners(oldPath);
365
401
  BezierPathInteractionHelper.setPathAutoAdjust(newPath);
366
402
  this.paths[pathIndex] = newPath;
@@ -396,36 +432,38 @@ export class BezierPathInteractionHelper {
396
432
  // +---------------------------------------------------------------------------------
397
433
  // | Called once upon initialization.
398
434
  // +-------------------------------
399
- private _installTouchListener(): void {
435
+ private _installTouchListener(): AlloyFinger {
400
436
  var _self: BezierPathInteractionHelper = this;
401
437
  const afProps = {
402
438
  // Todo: which event types does AlloyFinger use?
403
- touchStart: function (e: TouchEvent) {
439
+ touchStart: function (_event: TouchEvent) {
404
440
  _self.mouseIsOver = true;
405
441
  },
406
- touchMove: function (e: TouchEvent) {
407
- if (_self.pb.getDraggedElementCount() == 0 && e.touches.length > 0) {
442
+ touchMove: function (event: TouchEvent) {
443
+ if (_self.pb.getDraggedElementCount() == 0 && event.touches.length > 0) {
408
444
  // console.log('touchmove');
409
- _self._handleMoveEvent(e.touches[0].clientX, e.touches[0].clientY);
445
+ _self._handleMoveEvent(event.touches[0].clientX, event.touches[0].clientY);
410
446
  }
411
447
  },
412
- touchEnd: function (e: TouchEvent) {
448
+ touchEnd: function (_event: TouchEvent) {
413
449
  _self.mouseIsOver = false;
414
450
  _self._clearMoveEvent();
415
451
  }
416
452
  };
417
453
  // new AlloyFinger(this.pb.canvas, afProps);
418
- if (window["createAlloyFinger"])
419
- window["createAlloyFinger"](this.pb.eventCatcher ? this.pb.eventCatcher : this.pb.canvas, afProps);
420
- else new AlloyFinger(this.pb.eventCatcher ? this.pb.eventCatcher : this.pb.canvas, afProps);
454
+ if (window["createAlloyFinger"]) {
455
+ return window["createAlloyFinger"](this.pb.eventCatcher ? this.pb.eventCatcher : this.pb.canvas, afProps);
456
+ } else {
457
+ return new AlloyFinger(this.pb.eventCatcher ? this.pb.eventCatcher : this.pb.canvas, afProps);
458
+ }
421
459
  }
422
460
 
423
461
  // +---------------------------------------------------------------------------------
424
462
  // | Called once upon initialization.
425
463
  // +-------------------------------
426
- private _installMouseListener(): void {
464
+ private _installMouseListener(): MouseHandler {
427
465
  var _self: BezierPathInteractionHelper = this;
428
- new MouseHandler(this.pb.canvas)
466
+ var mouseHandler = new MouseHandler(this.pb.canvas)
429
467
  .up(function (e: XMouseEvent) {
430
468
  if (e.params.wasDragged) return;
431
469
  if (_self._keyHandler.isDown("shift")) return;
@@ -470,13 +508,16 @@ export class BezierPathInteractionHelper {
470
508
  _self._handleMoveEvent(e.params.pos.x, e.params.pos.y);
471
509
  });
472
510
 
473
- _self.pb.canvas.addEventListener("mouseenter", function () {
474
- _self.mouseIsOver = true;
475
- });
476
- _self.pb.canvas.addEventListener("mouseleave", function () {
477
- _self.mouseIsOver = false;
478
- _self._clearMoveEvent();
479
- });
511
+ // _self.pb.canvas.addEventListener("mouseenter", function () {
512
+ // _self.mouseIsOver = true;
513
+ // });
514
+ // _self.pb.canvas.addEventListener("mouseleave", function () {
515
+ // _self.mouseIsOver = false;
516
+ // _self._clearMoveEvent();
517
+ // });
518
+ this.pb.canvas.addEventListener("mouseenter", this._mouseEnterListener);
519
+ this.pb.canvas.addEventListener("mouseleave", this._mouseLeaveListener);
520
+ return mouseHandler;
480
521
  }
481
522
 
482
523
  // +---------------------------------------------------------------------------------
@@ -491,16 +532,6 @@ export class BezierPathInteractionHelper {
491
532
  });
492
533
  }
493
534
 
494
- // +---------------------------------------------------------------------------------
495
- // | Adds vertex listeners to all path points.
496
- // |
497
- // | @param {BezierPath} path - The path to add vertex listeners to.
498
- // +-------------------------------
499
- // TODO: THIS CAN BE REMOVED?
500
- // private _addDefaultPathListeners( path:BezierPath ) : void {
501
- // BezierPathInteractionHelper.addPathVertexDragListeners( path, this._updateMinDistance );
502
- // };
503
-
504
535
  // +---------------------------------------------------------------------------------
505
536
  // | Removes vertex listeners from all path points.
506
537
  // |