plotboilerplate 1.14.0 → 1.17.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 (232) hide show
  1. package/README.md +2 -0
  2. package/changelog.md +57 -0
  3. package/dist/index.esm.js +856 -694
  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 +858 -598
  10. package/dist/plotboilerplate.min.js +1 -1
  11. package/package.json +1 -1
  12. package/src/cjs/BezierPath.d.ts +32 -14
  13. package/src/cjs/BezierPath.js +65 -24
  14. package/src/cjs/BezierPath.js.map +1 -1
  15. package/src/cjs/Bounds.d.ts +22 -1
  16. package/src/cjs/Bounds.js +24 -1
  17. package/src/cjs/Bounds.js.map +1 -1
  18. package/src/cjs/Circle.d.ts +15 -13
  19. package/src/cjs/Circle.js +16 -34
  20. package/src/cjs/Circle.js.map +1 -1
  21. package/src/cjs/CircleSector.d.ts +17 -16
  22. package/src/cjs/CircleSector.js +12 -24
  23. package/src/cjs/CircleSector.js.map +1 -1
  24. package/src/cjs/CubicBezierCurve.d.ts +16 -15
  25. package/src/cjs/CubicBezierCurve.js +57 -94
  26. package/src/cjs/CubicBezierCurve.js.map +1 -1
  27. package/src/cjs/KeyHandler.d.ts +10 -3
  28. package/src/cjs/KeyHandler.js +28 -23
  29. package/src/cjs/KeyHandler.js.map +1 -1
  30. package/src/cjs/Line.d.ts +2 -15
  31. package/src/cjs/Line.js +6 -31
  32. package/src/cjs/Line.js.map +1 -1
  33. package/src/cjs/MouseHandler.js +43 -53
  34. package/src/cjs/MouseHandler.js.map +1 -1
  35. package/src/cjs/PBImage.d.ts +15 -13
  36. package/src/cjs/PBImage.js +11 -15
  37. package/src/cjs/PBImage.js.map +1 -1
  38. package/src/cjs/PBText.d.ts +13 -12
  39. package/src/cjs/PBText.js +8 -12
  40. package/src/cjs/PBText.js.map +1 -1
  41. package/src/cjs/PlotBoilerplate.js +3 -2
  42. package/src/cjs/PlotBoilerplate.js.map +1 -1
  43. package/src/cjs/Polygon.d.ts +44 -14
  44. package/src/cjs/Polygon.js +116 -66
  45. package/src/cjs/Polygon.js.map +1 -1
  46. package/src/cjs/Triangle.d.ts +15 -13
  47. package/src/cjs/Triangle.js +23 -64
  48. package/src/cjs/Triangle.js.map +1 -1
  49. package/src/cjs/VEllipse.d.ts +14 -9
  50. package/src/cjs/VEllipse.js +10 -18
  51. package/src/cjs/VEllipse.js.map +1 -1
  52. package/src/cjs/VEllipseSector.d.ts +15 -1
  53. package/src/cjs/VEllipseSector.js +11 -1
  54. package/src/cjs/VEllipseSector.js.map +1 -1
  55. package/src/cjs/Vector.d.ts +3 -15
  56. package/src/cjs/Vector.js +9 -49
  57. package/src/cjs/Vector.js.map +1 -1
  58. package/src/cjs/VertTuple.d.ts +15 -1
  59. package/src/cjs/VertTuple.js +14 -19
  60. package/src/cjs/VertTuple.js.map +1 -1
  61. package/src/cjs/Vertex.d.ts +47 -13
  62. package/src/cjs/Vertex.js +62 -22
  63. package/src/cjs/Vertex.js.map +1 -1
  64. package/src/cjs/VertexListeners.d.ts +12 -8
  65. package/src/cjs/VertexListeners.js +18 -25
  66. package/src/cjs/VertexListeners.js.map +1 -1
  67. package/src/cjs/draw.d.ts +41 -2
  68. package/src/cjs/draw.js +140 -3
  69. package/src/cjs/draw.js.map +1 -1
  70. package/src/cjs/drawgl.d.ts +38 -2
  71. package/src/cjs/drawgl.js +41 -2
  72. package/src/cjs/drawgl.js.map +1 -1
  73. package/src/cjs/drawutilssvg.d.ts +66 -3
  74. package/src/cjs/drawutilssvg.js +140 -15
  75. package/src/cjs/drawutilssvg.js.map +1 -1
  76. package/src/cjs/index.d.ts +0 -1
  77. package/src/cjs/index.js +0 -1
  78. package/src/cjs/index.js.map +1 -1
  79. package/src/cjs/interfaces/DrawLib.d.ts +61 -26
  80. package/src/cjs/interfaces/DrawLib.js +2 -0
  81. package/src/cjs/interfaces/DrawLib.js.map +1 -1
  82. package/src/cjs/interfaces/core.d.ts +0 -12
  83. package/src/cjs/module.d.ts +1 -3
  84. package/src/cjs/module.js +2 -5
  85. package/src/cjs/module.js.map +1 -1
  86. package/src/cjs/utils/creategui.js +16 -2
  87. package/src/cjs/utils/datastructures/Girih.d.ts +5 -0
  88. package/src/cjs/utils/datastructures/Girih.js +18 -14
  89. package/src/cjs/utils/datastructures/Girih.js.map +1 -1
  90. package/src/cjs/utils/datastructures/GirihPenroseRhombus.js +42 -25
  91. package/src/cjs/utils/datastructures/GirihPenroseRhombus.js.map +1 -1
  92. package/src/cjs/utils/datastructures/GirihTile.js +2 -13
  93. package/src/cjs/utils/datastructures/GirihTile.js.map +1 -1
  94. package/src/cjs/utils/findInVertexArray.d.ts +19 -0
  95. package/src/cjs/utils/findInVertexArray.js +29 -0
  96. package/src/cjs/utils/findInVertexArray.js.map +1 -0
  97. package/src/cjs/utils/helpers/BezierPathInteractionHelper.d.ts +17 -1
  98. package/src/cjs/utils/helpers/BezierPathInteractionHelper.js +50 -32
  99. package/src/cjs/utils/helpers/BezierPathInteractionHelper.js.map +1 -1
  100. package/src/cjs/utils/io/DndFileHandler.d.ts +63 -0
  101. package/src/cjs/utils/io/DndFileHandler.js +112 -0
  102. package/src/cjs/utils/io/DndFileHandler.js.map +1 -0
  103. package/src/cjs/utils/io/FileDrop.d.ts +98 -0
  104. package/src/cjs/utils/io/FileDrop.js +142 -0
  105. package/src/cjs/utils/io/FileDrop.js.map +1 -0
  106. package/src/esm/BezierPath.d.ts +32 -14
  107. package/src/esm/BezierPath.js +65 -24
  108. package/src/esm/BezierPath.js.map +1 -1
  109. package/src/esm/Bounds.d.ts +22 -1
  110. package/src/esm/Bounds.js +24 -1
  111. package/src/esm/Bounds.js.map +1 -1
  112. package/src/esm/Circle.d.ts +15 -13
  113. package/src/esm/Circle.js +16 -34
  114. package/src/esm/Circle.js.map +1 -1
  115. package/src/esm/CircleSector.d.ts +17 -16
  116. package/src/esm/CircleSector.js +12 -24
  117. package/src/esm/CircleSector.js.map +1 -1
  118. package/src/esm/CubicBezierCurve.d.ts +16 -15
  119. package/src/esm/CubicBezierCurve.js +57 -94
  120. package/src/esm/CubicBezierCurve.js.map +1 -1
  121. package/src/esm/KeyHandler.d.ts +10 -3
  122. package/src/esm/KeyHandler.js +28 -23
  123. package/src/esm/KeyHandler.js.map +1 -1
  124. package/src/esm/Line.d.ts +2 -15
  125. package/src/esm/Line.js +6 -31
  126. package/src/esm/Line.js.map +1 -1
  127. package/src/esm/MouseHandler.js +43 -53
  128. package/src/esm/MouseHandler.js.map +1 -1
  129. package/src/esm/PBImage.d.ts +15 -13
  130. package/src/esm/PBImage.js +11 -15
  131. package/src/esm/PBImage.js.map +1 -1
  132. package/src/esm/PBText.d.ts +13 -12
  133. package/src/esm/PBText.js +8 -12
  134. package/src/esm/PBText.js.map +1 -1
  135. package/src/esm/PlotBoilerplate.js +3 -2
  136. package/src/esm/PlotBoilerplate.js.map +1 -1
  137. package/src/esm/Polygon.d.ts +44 -14
  138. package/src/esm/Polygon.js +116 -66
  139. package/src/esm/Polygon.js.map +1 -1
  140. package/src/esm/Triangle.d.ts +15 -13
  141. package/src/esm/Triangle.js +23 -64
  142. package/src/esm/Triangle.js.map +1 -1
  143. package/src/esm/VEllipse.d.ts +14 -9
  144. package/src/esm/VEllipse.js +10 -18
  145. package/src/esm/VEllipse.js.map +1 -1
  146. package/src/esm/VEllipseSector.d.ts +15 -1
  147. package/src/esm/VEllipseSector.js +11 -1
  148. package/src/esm/VEllipseSector.js.map +1 -1
  149. package/src/esm/Vector.d.ts +3 -15
  150. package/src/esm/Vector.js +9 -49
  151. package/src/esm/Vector.js.map +1 -1
  152. package/src/esm/VertTuple.d.ts +15 -1
  153. package/src/esm/VertTuple.js +14 -19
  154. package/src/esm/VertTuple.js.map +1 -1
  155. package/src/esm/Vertex.d.ts +47 -13
  156. package/src/esm/Vertex.js +62 -22
  157. package/src/esm/Vertex.js.map +1 -1
  158. package/src/esm/VertexListeners.d.ts +12 -8
  159. package/src/esm/VertexListeners.js +18 -25
  160. package/src/esm/VertexListeners.js.map +1 -1
  161. package/src/esm/draw.d.ts +41 -2
  162. package/src/esm/draw.js +139 -3
  163. package/src/esm/draw.js.map +1 -1
  164. package/src/esm/drawgl.d.ts +38 -2
  165. package/src/esm/drawgl.js +41 -2
  166. package/src/esm/drawgl.js.map +1 -1
  167. package/src/esm/drawutilssvg.d.ts +66 -3
  168. package/src/esm/drawutilssvg.js +140 -15
  169. package/src/esm/drawutilssvg.js.map +1 -1
  170. package/src/esm/index.d.ts +0 -1
  171. package/src/esm/index.js +0 -1
  172. package/src/esm/index.js.map +1 -1
  173. package/src/esm/interfaces/DrawLib.d.ts +61 -26
  174. package/src/esm/interfaces/DrawLib.js +2 -0
  175. package/src/esm/interfaces/DrawLib.js.map +1 -1
  176. package/src/esm/interfaces/core.d.ts +0 -12
  177. package/src/esm/module.d.ts +1 -3
  178. package/src/esm/module.js +5 -4
  179. package/src/esm/module.js.map +1 -1
  180. package/src/esm/utils/datastructures/Girih.d.ts +5 -0
  181. package/src/esm/utils/datastructures/Girih.js +18 -14
  182. package/src/esm/utils/datastructures/Girih.js.map +1 -1
  183. package/src/esm/utils/datastructures/GirihPenroseRhombus.js +42 -25
  184. package/src/esm/utils/datastructures/GirihPenroseRhombus.js.map +1 -1
  185. package/src/esm/utils/datastructures/GirihTile.js +2 -13
  186. package/src/esm/utils/datastructures/GirihTile.js.map +1 -1
  187. package/src/esm/utils/findInVertexArray.d.ts +19 -0
  188. package/src/esm/utils/findInVertexArray.js +25 -0
  189. package/src/esm/utils/findInVertexArray.js.map +1 -0
  190. package/src/esm/utils/helpers/BezierPathInteractionHelper.d.ts +17 -1
  191. package/src/esm/utils/helpers/BezierPathInteractionHelper.js +49 -32
  192. package/src/esm/utils/helpers/BezierPathInteractionHelper.js.map +1 -1
  193. package/src/esm/utils/io/DndFileHandler.d.ts +63 -0
  194. package/src/esm/utils/io/DndFileHandler.js +106 -0
  195. package/src/esm/utils/io/DndFileHandler.js.map +1 -0
  196. package/src/esm/utils/io/FileDrop.d.ts +98 -0
  197. package/src/esm/utils/io/FileDrop.js +136 -0
  198. package/src/esm/utils/io/FileDrop.js.map +1 -0
  199. package/src/ts/BezierPath.ts +78 -22
  200. package/src/ts/Bounds.ts +25 -1
  201. package/src/ts/Circle.ts +187 -200
  202. package/src/ts/CircleSector.ts +141 -141
  203. package/src/ts/CubicBezierCurve.ts +738 -810
  204. package/src/ts/KeyHandler.ts +221 -212
  205. package/src/ts/Line.ts +48 -80
  206. package/src/ts/MouseHandler.ts +356 -368
  207. package/src/ts/PBImage.ts +67 -69
  208. package/src/ts/PBText.ts +16 -14
  209. package/src/ts/PlotBoilerplate.ts +4 -2
  210. package/src/ts/Polygon.ts +493 -451
  211. package/src/ts/Triangle.ts +444 -489
  212. package/src/ts/VEllipse.ts +18 -17
  213. package/src/ts/VEllipseSector.ts +20 -1
  214. package/src/ts/Vector.ts +124 -160
  215. package/src/ts/VertTuple.ts +333 -338
  216. package/src/ts/Vertex.ts +75 -21
  217. package/src/ts/VertexListeners.ts +248 -266
  218. package/src/ts/draw.ts +192 -3
  219. package/src/ts/drawgl.ts +44 -2
  220. package/src/ts/drawutilssvg.ts +181 -15
  221. package/src/ts/index.ts +0 -1
  222. package/src/ts/interfaces/DrawLib.ts +69 -26
  223. package/src/ts/interfaces/core.ts +0 -11
  224. package/src/ts/module.ts +2 -4
  225. package/src/ts/utils/datastructures/Girih.ts +204 -209
  226. package/src/ts/utils/datastructures/GirihPenroseRhombus.ts +170 -152
  227. package/src/ts/utils/datastructures/GirihTile.ts +394 -419
  228. package/src/ts/utils/findInVertexArray.ts +28 -0
  229. package/src/ts/utils/helpers/BezierPathInteractionHelper.ts +56 -34
  230. package/src/ts/utils/io/FileDrop.ts +178 -0
  231. package/src/ts/SVGBuilder.ts +0 -109
  232. package/src/ts/utils/algorithms/sh-tmp.ts~ +0 -59
package/src/ts/draw.ts CHANGED
@@ -39,7 +39,10 @@
39
39
  * @modified 2021-05-31 Added the `setConfiguration` function from `DrawLib`.
40
40
  * @modified 2021-11-12 Adding more parameters tot the `text()` function: fontSize, textAlign, fontFamily, lineHeight.
41
41
  * @modified 2021-11-19 Added the `color` param to the `label(...)` function.
42
- * @version 1.10.0
42
+ * @modified 2022-02-03 Added the `lineWidth` param to the `crosshair` function.
43
+ * @modified 2022-02-03 Added the `cross(...)` function.
44
+ * @modified 2022-03-27 Added the `texturedPoly` function.
45
+ * @version 1.12.0
43
46
  **/
44
47
 
45
48
  import { CubicBezierCurve } from "./CubicBezierCurve";
@@ -47,6 +50,7 @@ import { Polygon } from "./Polygon";
47
50
  import { Vertex } from "./Vertex";
48
51
  import { DrawLib, SVGPathParams, XYCoords, UID, DrawLibConfiguration, FontStyle, FontWeight } from "./interfaces";
49
52
  import { drawutilssvg } from "./drawutilssvg";
53
+ import { Bounds } from "./Bounds";
50
54
 
51
55
  // Todo: rename this class to Drawutils?
52
56
  /**
@@ -251,6 +255,146 @@ export class drawutils implements DrawLib<void> {
251
255
  this.ctx.restore();
252
256
  }
253
257
 
258
+ /**
259
+ * Draw an image at the given position with the given size.<br>
260
+ * <br>
261
+ * Note: SVG images may have resizing issues at the moment.Draw a line and an arrow at the end (zB) of the given line with the specified (CSS-) color.
262
+ *
263
+ * @method texturedPoly
264
+ * @param {Image} textureImage - The image object to draw.
265
+ * @param {Bounds} textureSize - The texture size to use; these are the original bounds to map the polygon vertices to.
266
+ * @param {Polygon} polygon - The polygon to use as clip path.
267
+ * @param {Vertex} polygonPosition - The polygon's position (relative), measured at the bounding box's center.
268
+ * @param {number} rotation - The rotation to use for the polygon (and for the texture).
269
+ * @param {XYCoords={x:0,y:0}} rotationCenter - (optional) The rotational center; default is center of bounding box.
270
+ * @return {void}
271
+ * @instance
272
+ * @memberof drawutils
273
+ **/
274
+ texturedPoly(
275
+ textureImage: HTMLImageElement,
276
+ textureSize: Bounds,
277
+ polygon: Polygon,
278
+ polygonPosition: Vertex,
279
+ rotation: number
280
+ ): void {
281
+ var basePolygonBounds = polygon.getBounds();
282
+ var targetCenterDifference = polygonPosition.clone().difference(basePolygonBounds.getCenter());
283
+ // var rotationalOffset = rotationCenter ? polygonPosition.difference(rotationCenter) : { x: 0, y: 0 };
284
+ // var rotationalOffset = { x: 0, y: 0 };
285
+ var tileCenter = basePolygonBounds.getCenter().sub(targetCenterDifference);
286
+
287
+ // Get the position offset of the polygon
288
+ var targetTextureSize = new Vertex(textureSize.width, textureSize.height);
289
+ // var targetTextureOffset = new Vertex(-textureSize.width / 2, -textureSize.height / 2).sub(targetCenterDifference);
290
+ var targetTextureOffset = new Vertex(textureSize.min.x, textureSize.min.y).sub(polygonPosition);
291
+
292
+ this.ctx.save();
293
+ // this.ctx.translate(this.offset.x + rotationCenter.x * this.scale.x, this.offset.y + rotationCenter.y * this.scale.y);
294
+ this.ctx.translate(this.offset.x + polygonPosition.x * this.scale.x, this.offset.y + polygonPosition.y * this.scale.y);
295
+
296
+ drawutils.helpers.clipPoly(
297
+ this.ctx,
298
+ {
299
+ x: -polygonPosition.x * this.scale.x,
300
+ y: -polygonPosition.y * this.scale.y
301
+ },
302
+ this.scale,
303
+ polygon.vertices
304
+ );
305
+ this.ctx.scale(this.scale.x, this.scale.y);
306
+ this.ctx.rotate(rotation);
307
+ this.ctx.drawImage(
308
+ textureImage,
309
+ 0,
310
+ 0,
311
+ textureImage.naturalWidth - 1, // There is this horrible Safari bug (fixed in newer versions)
312
+ textureImage.naturalHeight - 1, // To avoid errors substract 1 here.
313
+ targetTextureOffset.x, // * this.scale.x,
314
+ targetTextureOffset.y, // * this.scale.y,
315
+ targetTextureSize.x, // * this.scale.x,
316
+ targetTextureSize.y // * this.scale.y
317
+ );
318
+
319
+ this.ctx.restore();
320
+ }
321
+
322
+ _texturedPoly(
323
+ textureImage: HTMLImageElement,
324
+ textureSize: Bounds,
325
+ polygon: Polygon,
326
+ polygonPosition: Vertex,
327
+ rotation: number,
328
+ rotationCenter: XYCoords = { x: 0, y: 0 }
329
+ ): void {
330
+ var basePolygonBounds = polygon.getBounds();
331
+ var targetCenterDifference = polygonPosition.clone().difference(basePolygonBounds.getCenter());
332
+ var rotationalOffset = rotationCenter ? polygonPosition.difference(rotationCenter) : { x: 0, y: 0 };
333
+ // var rotationalOffset = { x: 0, y: 0 };
334
+ var tileCenter = basePolygonBounds.getCenter().sub(targetCenterDifference);
335
+
336
+ // Get the position offset of the polygon
337
+ var targetTextureSize = new Vertex(textureSize.width, textureSize.height);
338
+ var targetTextureOffset = new Vertex(-textureSize.width / 2, -textureSize.height / 2).sub(targetCenterDifference);
339
+
340
+ this.ctx.save();
341
+
342
+ // this.ctx.translate(
343
+ // this.offset.x + (tileCenter.x - rotationalOffset.x * 0 + targetTextureOffset.x * 0.0) * this.scale.x,
344
+ // this.offset.y + (tileCenter.y - rotationalOffset.y * 0 + targetTextureOffset.y * 0.0) * this.scale.y
345
+ // );
346
+ this.ctx.translate(
347
+ this.offset.x + (tileCenter.x - rotationalOffset.x * 0 + targetTextureOffset.x * 0.0) * this.scale.x,
348
+ this.offset.y + (tileCenter.y - rotationalOffset.y * 0 + targetTextureOffset.y * 0.0) * this.scale.y
349
+ );
350
+ this.ctx.rotate(rotation);
351
+
352
+ drawutils.helpers.clipPoly(
353
+ this.ctx,
354
+ {
355
+ x: (-targetCenterDifference.x * 1 - tileCenter.x - rotationalOffset.x) * this.scale.x,
356
+ y: (-targetCenterDifference.y * 1 - tileCenter.y - rotationalOffset.y) * this.scale.y
357
+ },
358
+ this.scale,
359
+ polygon.vertices
360
+ );
361
+ this.ctx.drawImage(
362
+ textureImage,
363
+ 0,
364
+ 0,
365
+ textureImage.naturalWidth - 1, // There is this horrible Safari bug (fixed in newer versions)
366
+ textureImage.naturalHeight - 1, // To avoid errors substract 1 here.
367
+ (-polygonPosition.x + targetTextureOffset.x * 1 - rotationalOffset.x * 1) * this.scale.x,
368
+ (-polygonPosition.y + targetTextureOffset.y * 1 - rotationalOffset.y * 1) * this.scale.y,
369
+ targetTextureSize.x * this.scale.x,
370
+ targetTextureSize.y * this.scale.y
371
+ );
372
+
373
+ // const scaledTextureSize = new Bounds(
374
+ // new Vertex(
375
+ // -polygonPosition.x + targetTextureOffset.x - rotationalOffset.x,
376
+ // -polygonPosition.y + targetTextureOffset.y - rotationalOffset.y
377
+ // ).scaleXY(this.scale, rotationCenter),
378
+ // new Vertex(
379
+ // -polygonPosition.x + targetTextureOffset.x - rotationalOffset.x + targetTextureSize.x,
380
+ // -polygonPosition.y + targetTextureOffset.y - rotationalOffset.y + targetTextureSize.y
381
+ // ).scaleXY(this.scale, rotationCenter)
382
+ // );
383
+ // this.ctx.drawImage(
384
+ // textureImage,
385
+ // 0,
386
+ // 0,
387
+ // textureImage.naturalWidth - 1, // There is this horrible Safari bug (fixed in newer versions)
388
+ // textureImage.naturalHeight - 1, // To avoid errors substract 1 here.
389
+ // scaledTextureSize.min.x,
390
+ // scaledTextureSize.min.y,
391
+ // scaledTextureSize.width,
392
+ // scaledTextureSize.height
393
+ // );
394
+
395
+ this.ctx.restore();
396
+ }
397
+
254
398
  /**
255
399
  * Draw a rectangle.
256
400
  *
@@ -784,11 +928,12 @@ export class drawutils implements DrawLib<void> {
784
928
  * @param {XYCoords} center - The center of the crosshair.
785
929
  * @param {number} radius - The radius of the crosshair.
786
930
  * @param {string} color - The CSS color to draw the crosshair with.
931
+ * @param {number=0.5} lineWidth - (optional, default=0.5) The line width to use.
787
932
  * @return {void}
788
933
  * @instance
789
934
  * @memberof drawutils
790
935
  */
791
- crosshair(center: XYCoords, radius: number, color: string) {
936
+ crosshair(center: XYCoords, radius: number, color: string, lineWidth?: number) {
792
937
  this.ctx.save();
793
938
  this.ctx.beginPath();
794
939
  this.ctx.moveTo(this.offset.x + center.x * this.scale.x - radius, this.offset.y + center.y * this.scale.y);
@@ -796,7 +941,35 @@ export class drawutils implements DrawLib<void> {
796
941
  this.ctx.moveTo(this.offset.x + center.x * this.scale.x, this.offset.y + center.y * this.scale.y - radius);
797
942
  this.ctx.lineTo(this.offset.x + center.x * this.scale.x, this.offset.y + center.y * this.scale.y + radius);
798
943
  this.ctx.strokeStyle = color;
799
- this.ctx.lineWidth = 0.5;
944
+ this.ctx.lineWidth = lineWidth || 0.5;
945
+ this.ctx.stroke();
946
+ this.ctx.closePath();
947
+ this.ctx.restore();
948
+ }
949
+
950
+ /**
951
+ * Draw a cross with diagonal axes with given radius, color and lineWidth at the given position.<br>
952
+ * <br>
953
+ * Note that the x's radius will not be affected by scaling.
954
+ *
955
+ * @method crosshair
956
+ * @param {XYCoords} center - The center of the crosshair.
957
+ * @param {number} radius - The radius of the crosshair.
958
+ * @param {string} color - The CSS color to draw the crosshair with.
959
+ * @param {number=1} lineWidth - (optional, default=1.0) The line width to use.
960
+ * @return {void}
961
+ * @instance
962
+ * @memberof drawutils
963
+ */
964
+ cross(center: XYCoords, radius: number, color: string, lineWidth?: number) {
965
+ this.ctx.save();
966
+ this.ctx.beginPath();
967
+ this.ctx.moveTo(this.offset.x + center.x * this.scale.x - radius, this.offset.y + center.y * this.scale.y - radius);
968
+ this.ctx.lineTo(this.offset.x + center.x * this.scale.x + radius, this.offset.y + center.y * this.scale.y + radius);
969
+ this.ctx.moveTo(this.offset.x + center.x * this.scale.x - radius, this.offset.y + center.y * this.scale.y + radius);
970
+ this.ctx.lineTo(this.offset.x + center.x * this.scale.x + radius, this.offset.y + center.y * this.scale.y - radius);
971
+ this.ctx.strokeStyle = color;
972
+ this.ctx.lineWidth = lineWidth || 1.0;
800
973
  this.ctx.stroke();
801
974
  this.ctx.closePath();
802
975
  this.ctx.restore();
@@ -992,4 +1165,20 @@ export class drawutils implements DrawLib<void> {
992
1165
  this.ctx.fillStyle = color;
993
1166
  this.ctx.fillRect(0, 0, this.ctx.canvas.width, this.ctx.canvas.height);
994
1167
  }
1168
+
1169
+ private static helpers = {
1170
+ // A helper function to define the clipping path.
1171
+ // This could be a candidate for the draw library.
1172
+ clipPoly: (ctx: CanvasRenderingContext2D, offset: XYCoords, scale: XYCoords, vertices: Array<XYCoords>): void => {
1173
+ ctx.beginPath();
1174
+ // Set clip mask
1175
+ ctx.moveTo(offset.x + vertices[0].x * scale.x, offset.y + vertices[0].y * scale.y);
1176
+ for (var i = 1; i < vertices.length; i++) {
1177
+ const vert: XYCoords = vertices[i];
1178
+ ctx.lineTo(offset.x + vert.x * scale.x, offset.y + vert.y * scale.y);
1179
+ }
1180
+ ctx.closePath();
1181
+ ctx.clip();
1182
+ }
1183
+ };
995
1184
  }
package/src/ts/drawgl.ts CHANGED
@@ -6,7 +6,10 @@
6
6
  * @modified 2020-10-15 Re-added the text() function.
7
7
  * @modified 2021-01-24 Added the `setCurrentId` function.
8
8
  * @modified 2021-05-31 Added the `setConfiguration` function from `DrawLib`.
9
- * @version 0.0.5
9
+ * @modified 2022-02-03 Added the `lineWidth` param to the `crosshair` function.
10
+ * @modified 2022-02-03 Added the `cross(...)` function.
11
+ * @modified 2022-03-27 Added the `texturedPoly` function.
12
+ * @version 0.0.7
10
13
  **/
11
14
 
12
15
  import { CubicBezierCurve } from "./CubicBezierCurve";
@@ -22,6 +25,7 @@ import {
22
25
  FontStyle,
23
26
  FontWeight
24
27
  } from "./interfaces";
28
+ import { Bounds } from "./Bounds";
25
29
 
26
30
  /**
27
31
  * @classdesc A wrapper class for basic drawing operations. This is the WebGL
@@ -283,6 +287,25 @@ export class drawutilsgl implements DrawLib<void> {
283
287
  // NOT YET IMPLEMENTED
284
288
  }
285
289
 
290
+ /**
291
+ * Draw an image at the given position with the given size.<br>
292
+ * <br>
293
+ * Note: SVG images may have resizing issues at the moment.Draw a line and an arrow at the end (zB) of the given line with the specified (CSS-) color.
294
+ *
295
+ * @method texturedPoly
296
+ * @param {Image} textureImage - The image object to draw.
297
+ * @param {Bounds} textureSize - The texture size to use; these are the original bounds to map the polygon vertices to.
298
+ * @param {Polygon} polygon - The polygon to use as clip path.
299
+ * @param {Vertex} polygonPosition - The polygon's position (relative), measured at the bounding box's center.
300
+ * @param {number} rotation - The rotation to use for the polygon (and for the texture).
301
+ * @return {void}
302
+ * @instance
303
+ * @memberof drawutilsgl
304
+ **/
305
+ texturedPoly(textureImage: HTMLImageElement, textureSize: Bounds, polygon: Polygon, polygonPosition: Vertex, rotation: number) {
306
+ // NOT YET IMPLEMENTED
307
+ }
308
+
286
309
  // +---------------------------------------------------------------------------------
287
310
  // | This is the final helper function for drawing and filling stuff. It is not
288
311
  // | intended to be used from the outside.
@@ -588,11 +611,30 @@ export class drawutilsgl implements DrawLib<void> {
588
611
  * @param {XYCoords} center - The center of the crosshair.
589
612
  * @param {number} radius - The radius of the crosshair.
590
613
  * @param {string} color - The CSS color to draw the crosshair with.
614
+ * @param {number=0.5} lineWidth - (optional, default=0.5) The line width to use.
615
+ * @return {void}
616
+ * @instance
617
+ * @memberof drawutils
618
+ */
619
+ crosshair(center: XYCoords, radius: number, color: string, lineWidth?: number) {
620
+ // NOT YET IMPLEMENTED
621
+ }
622
+
623
+ /**
624
+ * Draw a cross with diagonal axes with given radius, color and lineWidth at the given position.<br>
625
+ * <br>
626
+ * Note that the x's radius will not be affected by scaling.
627
+ *
628
+ * @method crosshair
629
+ * @param {XYCoords} center - The center of the crosshair.
630
+ * @param {number} radius - The radius of the crosshair.
631
+ * @param {string} color - The CSS color to draw the crosshair with.
632
+ * @param {number=1} lineWidth - (optional, default=1.0) The line width to use.
591
633
  * @return {void}
592
634
  * @instance
593
635
  * @memberof drawutils
594
636
  */
595
- crosshair(center: XYCoords, radius: number, color: string) {
637
+ cross(center: XYCoords, radius: number, color: string, lineWidth?: number) {
596
638
  // NOT YET IMPLEMENTED
597
639
  }
598
640
 
@@ -29,7 +29,11 @@
29
29
  * @modified 2021-11-15 Adding more parameters tot the `text()` function: fontSize, textAlign, fontFamily, lineHeight.
30
30
  * @modified 2021-11-19 Fixing the `label(text,x,y)` position.
31
31
  * @modified 2021-11-19 Added the `color` param to the `label(...)` function.
32
- * @version 1.4.0
32
+ * @modified 2022-02-03 Added the `lineWidth` param to the `crosshair` function.
33
+ * @modified 2022-02-03 Added the `cross(...)` function.
34
+ * @modified 2022-03-26 Added the private `nodeDefs` and `bufferedNodeDefs` attributes.
35
+ * @modified 2022-03-26 Added the `texturedPoly` function to draw textures polygons.
36
+ * @version 1.6.0
33
37
  **/
34
38
 
35
39
  import { CircleSector } from "./CircleSector";
@@ -48,6 +52,8 @@ import {
48
52
  FontStyle,
49
53
  FontWeight
50
54
  } from "./interfaces";
55
+ import { Bounds } from "./Bounds";
56
+ import { UIDGenerator } from "./UIDGenerator";
51
57
 
52
58
  const RAD_TO_DEG = 180 / Math.PI;
53
59
 
@@ -98,6 +104,24 @@ export class drawutilssvg implements DrawLib<void | SVGElement> {
98
104
  */
99
105
  private nodeStyle: SVGStyleElement;
100
106
 
107
+ /**
108
+ * A style node of type `<defs>`.
109
+ * @member {SVGGelement}
110
+ * @memberof drawutilssvg
111
+ * @instance
112
+ * @private
113
+ */
114
+ private nodeDefs: SVGDefsElement;
115
+
116
+ /**
117
+ * The buffered nodeDefs.
118
+ * @member {SVGGelement}
119
+ * @memberof drawutilssvg
120
+ * @instance
121
+ * @private
122
+ */
123
+ private bufferedNodeDefs: SVGDefsElement;
124
+
101
125
  /**
102
126
  * @member {Vertex}
103
127
  * @memberof drawutilssvg
@@ -181,7 +205,9 @@ export class drawutilssvg implements DrawLib<void | SVGElement> {
181
205
  drawConfig: DrawConfig,
182
206
  isSecondary?: boolean,
183
207
  gNode?: SVGGElement,
184
- bufferGNode?: SVGGElement
208
+ bufferGNode?: SVGGElement,
209
+ nodeDefs?: SVGDefsElement,
210
+ bufferNodeDefs?: SVGDefsElement
185
211
  ) {
186
212
  this.svgNode = svgNode;
187
213
  this.offset = new Vertex(0, 0).set(offset);
@@ -195,14 +221,23 @@ export class drawutilssvg implements DrawLib<void | SVGElement> {
195
221
  if (isSecondary) {
196
222
  this.gNode = gNode;
197
223
  this.bufferGNode = bufferGNode;
224
+ this.nodeDefs = nodeDefs;
225
+ this.bufferedNodeDefs = bufferNodeDefs;
198
226
  } else {
199
227
  this.addStyleDefs(drawConfig);
228
+ this.addDefsNode();
200
229
  this.gNode = this.createSVGNode("g") as SVGGElement;
201
230
  this.bufferGNode = this.createSVGNode("g") as SVGGElement;
202
231
  this.svgNode.appendChild(this.gNode);
203
232
  }
204
233
  }
205
234
 
235
+ /**
236
+ * Adds a default style defintion based on the passed DrawConfig.
237
+ * Twaek the draw config to change default colors or line thicknesses.
238
+ *
239
+ * @param {DrawConfig} drawConfig
240
+ */
206
241
  private addStyleDefs(drawConfig: DrawConfig) {
207
242
  this.nodeStyle = this.createSVGNode("style") as SVGStyleElement;
208
243
  this.svgNode.appendChild(this.nodeStyle);
@@ -236,6 +271,16 @@ export class drawutilssvg implements DrawLib<void | SVGElement> {
236
271
  this.nodeStyle.innerHTML = rules.join("\n");
237
272
  }
238
273
 
274
+ /**
275
+ * Adds the internal <defs> node.
276
+ */
277
+ private addDefsNode() {
278
+ this.nodeDefs = this.createSVGNode("defs") as SVGDefsElement;
279
+ // this.svgNode.appendChild(this.nodeDefs);
280
+ this.bufferedNodeDefs = this.createSVGNode("defs") as SVGDefsElement;
281
+ this.svgNode.appendChild(this.nodeDefs);
282
+ }
283
+
239
284
  /**
240
285
  * This is a simple way to include custom CSS class mappings to the style defs of the generated SVG.
241
286
  *
@@ -386,7 +431,9 @@ export class drawutilssvg implements DrawLib<void | SVGElement> {
386
431
  null, // no DrawConfig
387
432
  true, // isSecondary
388
433
  this.gNode,
389
- this.bufferGNode
434
+ this.bufferGNode,
435
+ this.nodeDefs,
436
+ this.bufferedNodeDefs
390
437
  );
391
438
  return copy;
392
439
  }
@@ -470,11 +517,16 @@ export class drawutilssvg implements DrawLib<void | SVGElement> {
470
517
  // layout updates.
471
518
  // Replace the old <g>-node with the buffer node.
472
519
  // https://stackoverflow.com/questions/27442464/how-to-update-a-svg-image-without-seeing-a-blinking
520
+ this.svgNode.replaceChild(this.bufferedNodeDefs, this.nodeDefs);
473
521
  this.svgNode.replaceChild(this.bufferGNode, this.gNode);
474
522
  }
475
- let tmp: SVGGElement = this.gNode;
523
+ const tmpGNode: SVGGElement = this.gNode;
476
524
  this.gNode = this.bufferGNode;
477
- this.bufferGNode = tmp;
525
+ this.bufferGNode = tmpGNode;
526
+
527
+ const tmpDefsNode: SVGDefsElement = this.nodeDefs;
528
+ this.nodeDefs = this.bufferedNodeDefs;
529
+ this.bufferedNodeDefs = tmpDefsNode;
478
530
  }
479
531
 
480
532
  private _x(x: number): number {
@@ -574,6 +626,90 @@ export class drawutilssvg implements DrawLib<void | SVGElement> {
574
626
  return this._bindFillDraw(node, "image", null, null);
575
627
  }
576
628
 
629
+ /**
630
+ * Draw an image at the given position with the given size.<br>
631
+ * <br>
632
+ * Note: SVG images may have resizing issues at the moment.Draw a line and an arrow at the end (zB) of the given line with the specified (CSS-) color.
633
+ *
634
+ * @method texturedPoly
635
+ * @param {Image} textureImage - The image object to draw.
636
+ * @param {Bounds} textureSize - The texture size to use; these are the original bounds to map the polygon vertices to.
637
+ * @param {Polygon} polygon - The polygon to use as clip path.
638
+ * @param {Vertex} polygonPosition - The polygon's position (relative), measured at the bounding box's center.
639
+ * @param {number} rotation - The rotation to use for the polygon (and for the texture).
640
+ * @return {void}
641
+ * @instance
642
+ * @memberof drawutilssvg
643
+ **/
644
+ texturedPoly(
645
+ textureImage: HTMLImageElement,
646
+ textureSize: Bounds,
647
+ polygon: Polygon,
648
+ polygonPosition: Vertex,
649
+ rotation: number
650
+ ): SVGElement {
651
+ const basePolygonBounds: Bounds = polygon.getBounds();
652
+ const rotatedScalingOrigin = new Vertex(textureSize.min).clone().rotate(rotation, polygonPosition);
653
+ const rotationCenter = polygonPosition.clone().add(rotatedScalingOrigin.difference(textureSize.min).inv());
654
+ // Create something like this
655
+ // ...
656
+ // <defs>
657
+ // <clipPath id="shape">
658
+ // <path fill="none" d="..."/>
659
+ // </clipPath>
660
+ // </defs>
661
+ // ...
662
+ // <g clip-path="url(#shape)">
663
+ // <g transform="scale(...)">
664
+ // <image width="643" height="643" transform="rotate(...)" xlink:href="https://s3-us-west-2.amazonaws.com/s.cdpn.io/222579/beagle400.jpg" >
665
+ // </g>
666
+ // </g>
667
+ // </image>
668
+ // ...
669
+ const clipPathNode: SVGClipPathElement = this.makeNode("clipPath") as SVGClipPathElement;
670
+ const clipPathId: string = `clippath_${UIDGenerator.next()}`; // TODO: use a better UUID generator here?
671
+ clipPathNode.setAttribute("id", clipPathId);
672
+
673
+ const gNode = this.makeNode("g") as SVGGElement;
674
+ const imageNode: SVGImageElement = this.makeNode("image") as SVGImageElement;
675
+ imageNode.setAttribute("x", `${this._x(rotatedScalingOrigin.x)}`);
676
+ imageNode.setAttribute("y", `${this._y(rotatedScalingOrigin.y)}`);
677
+ imageNode.setAttribute("width", `${textureSize.width}`);
678
+ imageNode.setAttribute("height", `${textureSize.height}`);
679
+ imageNode.setAttribute("href", textureImage.src);
680
+ // imageNode.setAttribute("opacity", "0.5");
681
+ // SVG rotations in degrees
682
+ imageNode.setAttribute(
683
+ "transform",
684
+ `rotate(${rotation * RAD_TO_DEG}, ${this._x(rotatedScalingOrigin.x)}, ${this._y(rotatedScalingOrigin.y)})`
685
+ );
686
+ const pathNode: SVGPathElement = this.makeNode("path") as SVGPathElement;
687
+ const pathData: string[] = [];
688
+ if (polygon.vertices.length > 0) {
689
+ const self = this;
690
+ pathData.push("M", `${this._x(polygon.vertices[0].x)}`, `${this._y(polygon.vertices[0].y)}`);
691
+ for (var i = 1; i < polygon.vertices.length; i++) {
692
+ pathData.push("L", `${this._x(polygon.vertices[i].x)}`, `${this._y(polygon.vertices[i].y)}`);
693
+ }
694
+ }
695
+
696
+ pathNode.setAttribute("d", pathData.join(" "));
697
+ clipPathNode.appendChild(pathNode);
698
+ this.bufferedNodeDefs.appendChild(clipPathNode);
699
+
700
+ gNode.appendChild(imageNode);
701
+ gNode.setAttribute("transform-origin", `${this._x(rotatedScalingOrigin.x)} ${this._y(rotatedScalingOrigin.y)}`);
702
+ gNode.setAttribute("transform", `scale(${this.scale.x}, ${this.scale.y})`);
703
+
704
+ const clipNode: SVGGElement = this.makeNode("g") as SVGGElement;
705
+ clipNode.appendChild(gNode);
706
+ clipNode.setAttribute("clip-path", `url(#${clipPathId})`);
707
+
708
+ // TODO: check if the image class is correct here or if we should use a 'clippedImage' class here
709
+ this._bindFillDraw(clipNode, "image", null, null); // No color, no lineWidth
710
+ return clipNode;
711
+ }
712
+
577
713
  /**
578
714
  * Draw the given (cubic) bézier curve.
579
715
  *
@@ -1020,11 +1156,12 @@ export class drawutilssvg implements DrawLib<void | SVGElement> {
1020
1156
  * @param {XYCoords} center - The center of the crosshair.
1021
1157
  * @param {number} radius - The radius of the crosshair.
1022
1158
  * @param {string} color - The CSS color to draw the crosshair with.
1159
+ * @param {number=0.5} lineWidth - (optional, default=0.5) The line width to use.
1023
1160
  * @return {void}
1024
1161
  * @instance
1025
1162
  * @memberof drawutilssvg
1026
1163
  */
1027
- crosshair(center: XYCoords, radius: number, color: string) {
1164
+ crosshair(center: XYCoords, radius: number, color: string, lineWidth?: number) {
1028
1165
  const node: SVGElement = this.makeNode("path");
1029
1166
  const d: SVGPathParams = [
1030
1167
  "M",
@@ -1041,7 +1178,41 @@ export class drawutilssvg implements DrawLib<void | SVGElement> {
1041
1178
  this._y(center.y) + radius
1042
1179
  ];
1043
1180
  node.setAttribute("d", d.join(" "));
1044
- return this._bindFillDraw(node, "crosshair", color, 0.5);
1181
+ return this._bindFillDraw(node, "crosshair", color, lineWidth || 0.5);
1182
+ }
1183
+
1184
+ /**
1185
+ * Draw a cross with diagonal axes with given radius, color and lineWidth at the given position.<br>
1186
+ * <br>
1187
+ * Note that the x's radius will not be affected by scaling.
1188
+ *
1189
+ * @method crosshair
1190
+ * @param {XYCoords} center - The center of the crosshair.
1191
+ * @param {number} radius - The radius of the crosshair.
1192
+ * @param {string} color - The CSS color to draw the crosshair with.
1193
+ * @param {number=1} lineWidth - (optional, default=1.0) The line width to use.
1194
+ * @return {void}
1195
+ * @instance
1196
+ * @memberof drawutils
1197
+ */
1198
+ cross(center: XYCoords, radius: number, color: string, lineWidth?: number) {
1199
+ const node: SVGElement = this.makeNode("path");
1200
+ const d: SVGPathParams = [
1201
+ "M",
1202
+ this._x(center.x) - radius,
1203
+ this._y(center.y) - radius,
1204
+ "L",
1205
+ this._x(center.x) + radius,
1206
+ this._y(center.y) + radius,
1207
+ "M",
1208
+ this._x(center.x) - radius,
1209
+ this._y(center.y) + radius,
1210
+ "L",
1211
+ this._x(center.x) + radius,
1212
+ this._y(center.y) - radius
1213
+ ];
1214
+ node.setAttribute("d", d.join(" "));
1215
+ return this._bindFillDraw(node, "cross", color, lineWidth || 1.0);
1045
1216
  }
1046
1217
 
1047
1218
  /**
@@ -1226,14 +1397,6 @@ export class drawutilssvg implements DrawLib<void | SVGElement> {
1226
1397
  if (this.isSecondary) {
1227
1398
  return;
1228
1399
  }
1229
- // // Clearing an SVG is equivalent to removing all its child elements.
1230
- // for (var i = 0; i < this.gNode.childNodes.length; i++) {
1231
- // // Hide all nodes here. Don't throw them away.
1232
- // // We can probably re-use them in the next draw cycle.
1233
- // var child: SVGElement = this.gNode.childNodes[i] as SVGElement;
1234
- // this.cache.set(child.getAttribute("id"), child);
1235
- // }
1236
- // this.removeAllChildNodes();
1237
1400
 
1238
1401
  // Add a covering rect with the given background color
1239
1402
  this.curId = "background";
@@ -1262,6 +1425,9 @@ export class drawutilssvg implements DrawLib<void | SVGElement> {
1262
1425
  while (this.bufferGNode.lastChild) {
1263
1426
  this.bufferGNode.removeChild(this.bufferGNode.lastChild);
1264
1427
  }
1428
+ while (this.bufferedNodeDefs.lastChild) {
1429
+ this.bufferedNodeDefs.removeChild(this.bufferedNodeDefs.lastChild);
1430
+ }
1265
1431
  }
1266
1432
 
1267
1433
  /**
package/src/ts/index.ts CHANGED
@@ -16,7 +16,6 @@ export * from "./PBImage";
16
16
  export * from "./PBText";
17
17
  export * from "./PlotBoilerplate";
18
18
  export * from "./Polygon";
19
- export * from "./SVGBuilder";
20
19
  export * from "./Triangle";
21
20
  export * from "./UIDGenerator";
22
21
  export * from "./Vector";