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
@@ -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
+ };
@@ -20,7 +20,8 @@
20
20
  * @modified 2021-01-03 Changed property to `autoAdjustPaths` in the HandlerOptions interface (typo).
21
21
  * @modified 2021-01-03 Added following new functions: `addPathVertexDragStartListeners`, `removePathVertexDragStartListeners`, `addPathVertexDragEndListeners` and `removePathVertexDragEndListeners`.
22
22
  * @modified 2021-03-31 Fixed the issue with the new AlloyFinger (Typescript).
23
- * @version 1.1.1
23
+ * @modified 2022-02-03 Changing the element to catch events (eventCatcher instead of canvas).
24
+ * @version 1.1.2
24
25
  *
25
26
  * @file BezierPathInteractionHelper
26
27
  * @public
@@ -118,7 +119,11 @@ export class BezierPathInteractionHelper {
118
119
  private currentA: Vertex; // Position on the curve
119
120
  private currentB: Vertex; // mouse/touch position
120
121
 
122
+ private _mouseHandler: MouseHandler;
123
+ private _touchHandler: AlloyFinger;
121
124
  private _keyHandler: KeyHandler;
125
+ private _mouseEnterListener: () => void;
126
+ private _mouseLeaveListener: () => void;
122
127
 
123
128
  /**
124
129
  * Pre: all paths must have been added to the PlotBoilerplate's drawable buffer (use the add(Drawable) function).
@@ -173,8 +178,16 @@ export class BezierPathInteractionHelper {
173
178
  this.addPath(paths[i]);
174
179
  }
175
180
 
176
- this._installMouseListener();
177
- this._installTouchListener();
181
+ this._mouseEnterListener = () => {
182
+ this.mouseIsOver = true;
183
+ };
184
+ this._mouseLeaveListener = () => {
185
+ this.mouseIsOver = false;
186
+ this._clearMoveEvent();
187
+ };
188
+
189
+ this._mouseHandler = this._installMouseListener();
190
+ this._touchHandler = this._installTouchListener();
178
191
  this._keyHandler = this._installKeyListener();
179
192
 
180
193
  // Paths might have changed by auto-adjustment.
@@ -249,6 +262,28 @@ export class BezierPathInteractionHelper {
249
262
  this._handleMoveEvent(this.currentB.x, this.currentB.y);
250
263
  }
251
264
 
265
+ /**
266
+ * This function should invalidate any installed listeners and invalidate this object.
267
+ * After calling this function the object might not hold valid data any more and
268
+ * should not be used any more.
269
+ *
270
+ * @method destroy
271
+ * @instance
272
+ * @memberof BezierPathInteractionHelper
273
+ * @return {void}
274
+ **/
275
+ destroy(): void {
276
+ for (var i = 0; i < this.paths.length; i++) {
277
+ this._removeDefaultPathListeners(this.paths[i]);
278
+ // removePathVertexDragStartListeners(this.paths[i], listener);
279
+ }
280
+ this.paths = [];
281
+ this.pb.eventCatcher.removeEventListener("mouseenter", this._mouseEnterListener);
282
+ this.pb.eventCatcher.removeEventListener("mouseleave", this._mouseLeaveListener);
283
+ this._mouseHandler.destroy();
284
+ this._keyHandler.destroy();
285
+ }
286
+
252
287
  // +---------------------------------------------------------------------------------
253
288
  // | A helper function to locate a given path instance inside the array.
254
289
  // |
@@ -335,6 +370,7 @@ export class BezierPathInteractionHelper {
335
370
  curStartControl = path.bezierCurves[i + 1].startControlPoint;
336
371
  }
337
372
  } else {
373
+ // TODO: remove drag listener from removed vertex!
338
374
  deletedVertIndices.push(i);
339
375
  }
340
376
  }
@@ -361,6 +397,7 @@ export class BezierPathInteractionHelper {
361
397
  private _replacePathAt(pathIndex: number, newPath: BezierPath): void {
362
398
  const oldPath: BezierPath = this.paths[pathIndex];
363
399
  this.pb.remove(oldPath, false, true); // Remove with vertices
400
+ oldPath.destroy();
364
401
  this._removeDefaultPathListeners(oldPath);
365
402
  BezierPathInteractionHelper.setPathAutoAdjust(newPath);
366
403
  this.paths[pathIndex] = newPath;
@@ -396,36 +433,37 @@ export class BezierPathInteractionHelper {
396
433
  // +---------------------------------------------------------------------------------
397
434
  // | Called once upon initialization.
398
435
  // +-------------------------------
399
- private _installTouchListener(): void {
436
+ private _installTouchListener(): AlloyFinger {
400
437
  var _self: BezierPathInteractionHelper = this;
401
438
  const afProps = {
402
439
  // Todo: which event types does AlloyFinger use?
403
- touchStart: function (e: TouchEvent) {
440
+ touchStart: function (_event: TouchEvent) {
404
441
  _self.mouseIsOver = true;
405
442
  },
406
- touchMove: function (e: TouchEvent) {
407
- if (_self.pb.getDraggedElementCount() == 0 && e.touches.length > 0) {
443
+ touchMove: function (event: TouchEvent) {
444
+ if (_self.pb.getDraggedElementCount() == 0 && event.touches.length > 0) {
408
445
  // console.log('touchmove');
409
- _self._handleMoveEvent(e.touches[0].clientX, e.touches[0].clientY);
446
+ _self._handleMoveEvent(event.touches[0].clientX, event.touches[0].clientY);
410
447
  }
411
448
  },
412
- touchEnd: function (e: TouchEvent) {
449
+ touchEnd: function (_event: TouchEvent) {
413
450
  _self.mouseIsOver = false;
414
451
  _self._clearMoveEvent();
415
452
  }
416
453
  };
417
- // 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.eventCatcher, afProps);
456
+ } else {
457
+ return new AlloyFinger(this.pb.eventCatcher ? this.pb.eventCatcher : this.pb.eventCatcher, 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.eventCatcher)
429
467
  .up(function (e: XMouseEvent) {
430
468
  if (e.params.wasDragged) return;
431
469
  if (_self._keyHandler.isDown("shift")) return;
@@ -464,19 +502,13 @@ export class BezierPathInteractionHelper {
464
502
  _self.onVertexInserted(_self.currentPathIndex, leftPath.bezierCurves.length, newPath, oldPath);
465
503
  })
466
504
  .move(function (e: XMouseEvent) {
467
- // console.log('moved');
468
- // if( _self.pb.getDraggedElementCount() == 0 )
469
505
  _self.mouseIsOver = true;
470
506
  _self._handleMoveEvent(e.params.pos.x, e.params.pos.y);
471
507
  });
472
508
 
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
- });
509
+ this.pb.eventCatcher.addEventListener("mouseenter", this._mouseEnterListener);
510
+ this.pb.eventCatcher.addEventListener("mouseleave", this._mouseLeaveListener);
511
+ return mouseHandler;
480
512
  }
481
513
 
482
514
  // +---------------------------------------------------------------------------------
@@ -491,16 +523,6 @@ export class BezierPathInteractionHelper {
491
523
  });
492
524
  }
493
525
 
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
526
  // +---------------------------------------------------------------------------------
505
527
  // | Removes vertex listeners from all path points.
506
528
  // |
@@ -0,0 +1,178 @@
1
+ /**
2
+ * A basic IO handler for file drop (Drag-and-drop).
3
+ *
4
+ * Example use:
5
+ * ```javascript
6
+ * var body = document.getElememtByTagName("body")[0];
7
+ * var fileDrop = new FileDrop(body);
8
+ * fileDrop.onFileJSONDropped(function (jsonObject) {
9
+ * console.log("jsonObject", jsonObject);
10
+ * });
11
+ * ```
12
+ *
13
+ * @author Ikaros Kappler
14
+ * @date 2021-10-13
15
+ * @modified 2022-01-31 (ported from the ngdg project, then generalized)
16
+ * @version 2.0.0
17
+ */
18
+
19
+ type IDroppedCallbackJSON = (jsonData: object) => void;
20
+ type IDroppedCallbackText = (textData: string) => void;
21
+ type IDroppedCallbackBinary = (binaryData: Blob, file: File) => void;
22
+
23
+ export class FileDrop {
24
+ /**
25
+ * The 'dropzone' element.
26
+ * @private
27
+ * @memberof FileDrop
28
+ * @member {HTMLElement}
29
+ */
30
+ private element: HTMLElement;
31
+
32
+ /**
33
+ * The JSON file drop callback.
34
+ * @private
35
+ * @memberof FileDrop
36
+ * @member {HTMLElement}
37
+ */
38
+ private fileDroppedCallbackJSON: IDroppedCallbackJSON | null;
39
+
40
+ /**
41
+ * The text file drop callback.
42
+ * @private
43
+ * @memberof FileDrop
44
+ * @member {HTMLElement}
45
+ */
46
+ private fileDroppedCallbackText: IDroppedCallbackText | null;
47
+
48
+ /**
49
+ * The binary file drop callback.
50
+ * @private
51
+ * @memberof FileDrop
52
+ * @member {HTMLElement}
53
+ */
54
+ private fileDroppedCallbackBinary: IDroppedCallbackBinary | null;
55
+
56
+ /**
57
+ *
58
+ * @param {HTMLElement} element - The element you wish to operate as the drop zone (like <body/>).
59
+ */
60
+ constructor(element: HTMLElement) {
61
+ this.element = element;
62
+
63
+ // Init the drop listeners
64
+ element.addEventListener("drop", this.handleDropEvent.bind(this));
65
+ element.addEventListener("dragover", this.handleDragOverEvent.bind(this));
66
+ element.addEventListener("dragleave", this.handleDragLeaveEvent.bind(this));
67
+ }
68
+
69
+ /**
70
+ * Install the JSON (MIME type json*) file drop callback. Note than only one callback can be installed
71
+ * in this implementation. Calling this method multiple times will overwrite previously
72
+ * installed listeners.
73
+ *
74
+ * The callback will receive the dropped file content as an object (parsed JSON).
75
+ *
76
+ * @param {(data:object)=>void} callback
77
+ */
78
+ onFileJSONDropped(callback: IDroppedCallbackJSON) {
79
+ this.fileDroppedCallbackJSON = callback;
80
+ }
81
+
82
+ /**
83
+ * Install the text file (MIME type text/plain) drop callback. Note than only one callback can be installed
84
+ * in this implementation. Calling this method multiple times will overwrite previously
85
+ * installed listeners.
86
+ *
87
+ * The callback will receive the dropped file content as a string.
88
+ *
89
+ * @param {(data:object)=>void} callback
90
+ */
91
+ onFileTextDropped(callback: IDroppedCallbackText) {
92
+ this.fileDroppedCallbackText = callback;
93
+ }
94
+
95
+ /**
96
+ * Internally handle a drop event.
97
+ *
98
+ * @param {DragEvent} event
99
+ * @returns {void}
100
+ */
101
+ private handleDropEvent = (event: DragEvent) => {
102
+ event.preventDefault();
103
+ event.stopPropagation();
104
+ this.element.style.opacity = "1.0";
105
+
106
+ if (!event.dataTransfer.files || event.dataTransfer.files.length === 0) {
107
+ // No files were dropped
108
+ return;
109
+ }
110
+ if (event.dataTransfer.files.length > 1) {
111
+ // Multiple file drop is not nupported
112
+ return;
113
+ }
114
+ if (!this.fileDroppedCallbackJSON) {
115
+ // No handling callback defined.
116
+ return;
117
+ }
118
+ if (event.dataTransfer.files[0]) {
119
+ const file: File = event.dataTransfer.files[0];
120
+ // console.log("file", file);
121
+ if (file.type.match(/json.*/) && this.fileDroppedCallbackJSON) {
122
+ var reader = new FileReader();
123
+ reader.onload = (readEvent: ProgressEvent<FileReader>) => {
124
+ // Finished reading file data.
125
+ const jsonObject: object = JSON.parse(readEvent.target.result as string);
126
+ // TODO: what happens on fail?
127
+ this.fileDroppedCallbackJSON(jsonObject);
128
+ };
129
+ reader.readAsText(file); // start reading the file data.
130
+ } else if (file.type.match(/text\/plain.*/) && this.fileDroppedCallbackText) {
131
+ var reader = new FileReader();
132
+ reader.onload = (readEvent: ProgressEvent<FileReader>) => {
133
+ // Finished reading file data.
134
+ this.fileDroppedCallbackText(readEvent.target.result as string);
135
+ };
136
+ reader.readAsText(file); // start reading the file data.
137
+ } else if (this.fileDroppedCallbackBinary) {
138
+ var reader = new FileReader();
139
+ reader.onload = (readEvent: ProgressEvent<FileReader>) => {
140
+ // Finished reading file data.
141
+ this.fileDroppedCallbackBinary(new Blob([readEvent.target.result]), file);
142
+ };
143
+ reader.readAsBinaryString(file); // start reading the file data.
144
+ }
145
+ }
146
+ };
147
+
148
+ /**
149
+ * Toggles the drop sensitive element's opacity to 0.5.
150
+ *
151
+ * @param {DragEvent} event - The event.
152
+ */
153
+ private handleDragOverEvent = (event: DragEvent) => {
154
+ event.preventDefault();
155
+ event.stopPropagation();
156
+ this.element.style.opacity = "0.5";
157
+ };
158
+
159
+ /**
160
+ * Restored the drop sensitive element's opacity back to 1.0.
161
+ *
162
+ * @param {DragEvent} event - The event.
163
+ */
164
+ private handleDragLeaveEvent = (event: DragEvent) => {
165
+ event.preventDefault();
166
+ event.stopPropagation();
167
+ this.element.style.opacity = "1.0";
168
+ };
169
+
170
+ /**
171
+ * Removes all listeners (drop, dragover and dragleave).
172
+ */
173
+ destroy() {
174
+ this.element.removeEventListener("drop", this.handleDropEvent);
175
+ this.element.removeEventListener("dragover", this.handleDragOverEvent);
176
+ this.element.removeEventListener("dragleave", this.handleDragLeaveEvent);
177
+ }
178
+ }
@@ -1,109 +0,0 @@
1
- /**
2
- * Todos:
3
- * + use a Drawable interface
4
- * + use a SVGSerializable interface
5
- *
6
- * @require Vertex
7
- *
8
- * @deprecated THIS CLASS IS DEPRECATED. Please use the new `drawutilssvg` instead.
9
- *
10
- * @author Ikaros Kappler
11
- * @date 2018-12-04
12
- * @modified 2019-11-07 Added the 'Triangle' style class.
13
- * @modified 2019-11-13 Added the <?xml ...?> tag.
14
- * @modified 2020-03-25 Ported this class from vanilla-JS to Typescript.
15
- * @modified 2020-12-17 Added Circle and CircleSection style classes.
16
- * @modified 2021-01-26 DEPRECATION
17
- * @version 1.0.5
18
- **/
19
-
20
-
21
- import { Vertex } from "./Vertex";
22
- import { SVGSerializable } from "./interfaces";
23
-
24
-
25
- /**
26
- * @classdesc A default SVG builder.
27
- *
28
- * @requires SVGSerializable
29
- * @requires Vertex
30
- */
31
- export class SVGBuilder {
32
-
33
- /**
34
- * @constructor
35
- **/
36
- constructor() {
37
- console.warn("THIS CLASS IS DEPRECATED. Please use the new 'drawutilssvg' instead.")
38
- };
39
-
40
-
41
- /**
42
- * Builds the SVG code from the given list of drawables.
43
- *
44
- * @param {object[]} drawables - The drawable elements (should implement Drawable) to be converted (each must have a toSVGString-function).
45
- * @param {object} options - { canvasSize, zoom, offset }
46
- * @return {string}
47
- **/
48
- build( drawables:Array<SVGSerializable>,
49
- options:{canvasSize:{width:number,height:number},zoom:Vertex,offset:Vertex}
50
- ) : string {
51
-
52
- var nl : string = '\n';
53
- var indent : string = ' ';
54
- var buffer : Array<string> = [];
55
-
56
- buffer.push( '<?xml version="1.0" encoding="UTF-8"?>' + nl );
57
- buffer.push( '<svg width="'+options.canvasSize.width+'" height="'+options.canvasSize.height+'"' );
58
- buffer.push( ' viewBox="' );
59
- buffer.push( '0' );
60
- buffer.push( ' ' );
61
- buffer.push( '0' );
62
- buffer.push( ' ' );
63
- buffer.push( options.canvasSize.width.toString() );
64
- buffer.push( ' ' );
65
- buffer.push( options.canvasSize.height.toString() );
66
- buffer.push( '"' );
67
-
68
- buffer.push( ' xmlns="http://www.w3.org/2000/svg">' + nl );
69
-
70
- buffer.push( indent + '<defs>' + nl );
71
- buffer.push( indent + '<style>' + nl);
72
-
73
- buffer.push( indent + indent + ' .Vertex { fill : blue; stroke : none; } ' + nl );
74
- buffer.push( indent + indent + ' .Triangle { fill : none; stroke : turquoise; stroke-width : 1px; } ' + nl );
75
- buffer.push( indent + indent + ' .Polygon { fill : none; stroke : green; stroke-width : 2px; } ' + nl );
76
- buffer.push( indent + indent + ' .BezierPath { fill : none; stroke : blue; stroke-width : 2px; } ' + nl );
77
- buffer.push( indent + indent + ' .VEllipse { fill : none; stroke : black; stroke-width : 1px; } ' + nl );
78
- buffer.push( indent + indent + ' .Line { fill : none; stroke : purple; stroke-width : 1px; } ' + nl );
79
- buffer.push( indent + indent + ' .Circle { fill : none; stroke : purple; stroke-width : 1px; } ' + nl );
80
- buffer.push( indent + indent + ' .CircleSector { fill : none; stroke : purple; stroke-width : 1px; } ' + nl );
81
- buffer.push( indent + '</style>' + nl );
82
- buffer.push( indent + '</defs>' + nl );
83
-
84
- buffer.push( indent + '<g class="main-g"' );
85
- if( options.zoom || options.offset ) {
86
- buffer.push( ' transform="' );
87
- if( options.zoom )
88
- buffer.push( 'scale(' + options.zoom.x + ',' + options.zoom.y + ')' );
89
- if( options.offset )
90
- buffer.push( ' translate(' + options.offset.x + ',' + options.offset.y + ')' );
91
- buffer.push( '"' );
92
- }
93
- buffer.push( '>' + nl );
94
-
95
- for( var i in drawables ) {
96
- var d : SVGSerializable = drawables[i];
97
- if( typeof d.toSVGString == 'function' ) {
98
- buffer.push( indent + indent );
99
- buffer.push( d.toSVGString( { 'className' : d.className } ) );
100
- buffer.push( nl );
101
- } else {
102
- console.warn( 'Unrecognized drawable type has no toSVGString()-function. Ignoring: ' + d.className );
103
- }
104
- }
105
- buffer.push( indent + '</g>' + nl );
106
- buffer.push( '</svg>' + nl );
107
- return buffer.join('');
108
- };
109
- }
@@ -1,59 +0,0 @@
1
-
2
- interface XYCoords {
3
- x : number;
4
- y : number;
5
- }
6
-
7
- const inside = ( cp1 : XYCoords, cp2 : XYCoords, p : XYCoords) : boolean => {
8
- return (cp2.x-cp1.x)*(p.y-cp1.y) > (cp2.y-cp1.y)*(p.x-cp1.x);
9
- };
10
-
11
- const intersection = ( cp1 : XYCoords ,cp2 : XYCoords ,s : XYCoords, e : XYCoords ) : XYCoords => {
12
- const dc = {
13
- x: cp1.x - cp2.x,
14
- y : cp1.y - cp2.y
15
- },
16
- dp = { x: s.x - e.x,
17
- y : s.y - e.y
18
- },
19
- n1 = cp1.x * cp2.y - cp1.y * cp2.x,
20
- n2 = s.x * e.y - s.y * e.x,
21
- n3 = 1.0 / (dc.x * dp.y - dc.y * dp.x);
22
- return { x : (n1*dp.x - n2*dc.x) * n3,
23
- y : (n1*dp.y - n2*dc.y) * n3
24
- };
25
- };
26
-
27
- export const sutherlandHodgman = ( subjectPolygon : Array<XYCoords>,
28
- clipPolygon : Array<XYCoords> ) : Array<XYCoords> => {
29
-
30
- let cp1 : XYCoords = clipPolygon[clipPolygon.length-1];
31
- let cp2 : XYCoords;
32
- let s : XYCoords;
33
- let e : XYCoords;
34
-
35
- let outputList : Array<XYCoords> = subjectPolygon;
36
-
37
- for( var j in clipPolygon ) {
38
- cp2 = clipPolygon[j];
39
- var inputList = outputList;
40
- outputList = [];
41
- s = inputList[inputList.length - 1]; // last on the input list
42
- for (var i in inputList) {
43
- e = inputList[i];
44
- if (inside(cp1,cp2,e)) {
45
- if (!inside(cp1,cp2,s)) {
46
- outputList.push(intersection(cp1,cp2,s,e));
47
- }
48
- outputList.push(e);
49
- }
50
- else if (inside(cp1,cp2,s)) {
51
- outputList.push(intersection(cp1,cp2,s,e));
52
- }
53
- s = e;
54
- }
55
- cp1 = cp2;
56
- }
57
- return outputList
58
- }
59
-