plotboilerplate 1.12.8 → 1.14.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 (98) hide show
  1. package/README.md +4 -1
  2. package/changelog.md +26 -0
  3. package/dist/index.esm.js +292 -51
  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 +306 -50
  10. package/dist/plotboilerplate.min.js +1 -1
  11. package/package.json +1 -1
  12. package/src/cjs/BezierPath.d.ts +12 -1
  13. package/src/cjs/BezierPath.js +34 -5
  14. package/src/cjs/BezierPath.js.map +1 -1
  15. package/src/cjs/PBText.d.ts +116 -0
  16. package/src/cjs/PBText.js +66 -0
  17. package/src/cjs/PBText.js.map +1 -0
  18. package/src/cjs/PlotBoilerplate.d.ts +2 -1
  19. package/src/cjs/PlotBoilerplate.js +41 -14
  20. package/src/cjs/PlotBoilerplate.js.map +1 -1
  21. package/src/cjs/Vertex.d.ts +18 -4
  22. package/src/cjs/Vertex.js +24 -7
  23. package/src/cjs/Vertex.js.map +1 -1
  24. package/src/cjs/draw.d.ts +30 -2
  25. package/src/cjs/draw.js +51 -6
  26. package/src/cjs/draw.js.map +1 -1
  27. package/src/cjs/drawgl.d.ts +29 -2
  28. package/src/cjs/drawgl.js +21 -4
  29. package/src/cjs/drawgl.js.map +1 -1
  30. package/src/cjs/drawutilssvg.d.ts +25 -7
  31. package/src/cjs/drawutilssvg.js +62 -14
  32. package/src/cjs/drawutilssvg.js.map +1 -1
  33. package/src/cjs/entry.js +1 -0
  34. package/src/cjs/index.d.ts +1 -0
  35. package/src/cjs/index.js +1 -0
  36. package/src/cjs/index.js.map +1 -1
  37. package/src/cjs/interfaces/DrawLib.d.ts +27 -6
  38. package/src/cjs/interfaces/DrawLib.js +3 -0
  39. package/src/cjs/interfaces/DrawLib.js.map +1 -1
  40. package/src/cjs/interfaces/core.d.ts +9 -1
  41. package/src/cjs/interfaces/core.js +1 -0
  42. package/src/cjs/interfaces/core.js.map +1 -1
  43. package/src/cjs/utils/datastructures/Color.d.ts +28 -15
  44. package/src/cjs/utils/datastructures/Color.js +276 -88
  45. package/src/cjs/utils/datastructures/Color.js.map +1 -1
  46. package/src/esm/BezierPath.d.ts +12 -1
  47. package/src/esm/BezierPath.js +34 -5
  48. package/src/esm/BezierPath.js.map +1 -1
  49. package/src/esm/PBText.d.ts +116 -0
  50. package/src/esm/PBText.js +61 -0
  51. package/src/esm/PBText.js.map +1 -0
  52. package/src/esm/PlotBoilerplate.d.ts +2 -1
  53. package/src/esm/PlotBoilerplate.js +41 -14
  54. package/src/esm/PlotBoilerplate.js.map +1 -1
  55. package/src/esm/Vertex.d.ts +18 -4
  56. package/src/esm/Vertex.js +24 -7
  57. package/src/esm/Vertex.js.map +1 -1
  58. package/src/esm/draw.d.ts +30 -2
  59. package/src/esm/draw.js +51 -6
  60. package/src/esm/draw.js.map +1 -1
  61. package/src/esm/drawgl.d.ts +29 -2
  62. package/src/esm/drawgl.js +21 -4
  63. package/src/esm/drawgl.js.map +1 -1
  64. package/src/esm/drawutilssvg.d.ts +25 -7
  65. package/src/esm/drawutilssvg.js +62 -14
  66. package/src/esm/drawutilssvg.js.map +1 -1
  67. package/src/esm/entry.js +1 -0
  68. package/src/esm/index.d.ts +1 -0
  69. package/src/esm/index.js +1 -0
  70. package/src/esm/index.js.map +1 -1
  71. package/src/esm/interfaces/DrawLib.d.ts +27 -6
  72. package/src/esm/interfaces/DrawLib.js +3 -0
  73. package/src/esm/interfaces/DrawLib.js.map +1 -1
  74. package/src/esm/interfaces/core.d.ts +9 -1
  75. package/src/esm/interfaces/core.js +1 -0
  76. package/src/esm/interfaces/core.js.map +1 -1
  77. package/src/esm/utils/datastructures/Color.d.ts +28 -15
  78. package/src/esm/utils/datastructures/Color.js +277 -89
  79. package/src/esm/utils/datastructures/Color.js.map +1 -1
  80. package/src/ts/BezierPath.ts +37 -5
  81. package/src/ts/PBText.ts +147 -0
  82. package/src/ts/PlotBoilerplate.ts +44 -18
  83. package/src/ts/Vertex.ts +26 -7
  84. package/src/ts/draw.ts +71 -8
  85. package/src/ts/drawgl.ts +48 -8
  86. package/src/ts/drawutilssvg.ts +94 -17
  87. package/src/ts/index.ts +1 -0
  88. package/src/ts/interfaces/DrawLib.ts +30 -4
  89. package/src/ts/interfaces/core.ts +9 -0
  90. package/src/ts/utils/datastructures/Color.ts +695 -508
  91. package/dist/plotboilerplate.browser.min.js.LICENSE.txt +0 -8
  92. package/src/ts/utils/algorithms/clearPolygonDuplicateVertices.ts~ +0 -5
  93. package/src/ts/utils/algorithms/findPolygonSelfIntersections.ts~ +0 -45
  94. package/src/ts/utils/algorithms/splitPolygonToNonIntersecting.ts~ +0 -113
  95. package/src/ts/utils/algorithms/sutherlandHodgman.ts~ +0 -88
  96. package/src/ts/utils/datastructures/Color.ts~ +0 -331
  97. package/src/ts/utils/datastructures/Girih.ts~ +0 -151
  98. package/src/ts/utils/datastructures/GirihTile.ts~ +0 -419
@@ -529,6 +529,38 @@ export class BezierPath implements SVGSerializable {
529
529
  return this;
530
530
  }
531
531
 
532
+ /**
533
+ * Scale the whole bezier path by the given uniform factor.
534
+ *
535
+ * @method scale
536
+ * @param {Vertex} anchor - The scale origin to scale from.
537
+ * @param {number} scaleFactor - The scalar to be multiplied with.
538
+ * @instance
539
+ * @memberof BezierPath
540
+ * @return {BezierPath} this for chaining.
541
+ **/
542
+ scale(anchor: Vertex, scaleFactor: number): BezierPath {
543
+ // var scaleFactors : XYCoords = { x : scaleFactor, y : scaleFactor };
544
+ // for (var i = 0; i < this.bezierCurves.length; i++) {
545
+ // var curve = this.bezierCurves[i];
546
+ // curve.getStartPoint().scale(scaleFactor, anchor);
547
+ // curve.getStartControlPoint().scale(scaleFactor, anchor);
548
+ // curve.getEndControlPoint().scale(scaleFactor, anchor);
549
+ // // Do NOT scale the end point here!
550
+ // // Don't forget that the curves are connected and on curve's end point
551
+ // // the the successor's start point (same instance)!
552
+ // }
553
+
554
+ // // Finally move the last end point (was not scaled yet)
555
+ // if (this.bezierCurves.length > 0 && !this.adjustCircular) {
556
+ // this.bezierCurves[this.bezierCurves.length - 1].getEndPoint().scale(scaleFactor, anchor);
557
+ // }
558
+
559
+ // this.updateArcLengths();
560
+ // return this;
561
+ return this.scaleXY({ x: scaleFactor, y: scaleFactor }, anchor);
562
+ }
563
+
532
564
  /**
533
565
  * Scale the whole bezier path by the given (x,y)-factors.
534
566
  *
@@ -539,12 +571,12 @@ export class BezierPath implements SVGSerializable {
539
571
  * @memberof BezierPath
540
572
  * @return {BezierPath} this for chaining.
541
573
  **/
542
- scale(anchor: Vertex, scaling: number): BezierPath {
574
+ scaleXY(scaleFactors: XYCoords, anchor?: XYCoords): BezierPath {
543
575
  for (var i = 0; i < this.bezierCurves.length; i++) {
544
576
  var curve = this.bezierCurves[i];
545
- curve.getStartPoint().scale(scaling, anchor);
546
- curve.getStartControlPoint().scale(scaling, anchor);
547
- curve.getEndControlPoint().scale(scaling, anchor);
577
+ curve.getStartPoint().scaleXY(scaleFactors, anchor);
578
+ curve.getStartControlPoint().scaleXY(scaleFactors, anchor);
579
+ curve.getEndControlPoint().scaleXY(scaleFactors, anchor);
548
580
  // Do NOT scale the end point here!
549
581
  // Don't forget that the curves are connected and on curve's end point
550
582
  // the the successor's start point (same instance)!
@@ -552,7 +584,7 @@ export class BezierPath implements SVGSerializable {
552
584
 
553
585
  // Finally move the last end point (was not scaled yet)
554
586
  if (this.bezierCurves.length > 0 && !this.adjustCircular) {
555
- this.bezierCurves[this.bezierCurves.length - 1].getEndPoint().scale(scaling, anchor);
587
+ this.bezierCurves[this.bezierCurves.length - 1].getEndPoint().scaleXY(scaleFactors, anchor);
556
588
  }
557
589
 
558
590
  this.updateArcLengths();
@@ -0,0 +1,147 @@
1
+ /**
2
+ * @author Ikaros Kappler
3
+ * @date 2021-11-16
4
+ * @version 1.0.0
5
+ **/
6
+
7
+ import { UIDGenerator } from "./UIDGenerator";
8
+ import { Vertex } from "./Vertex";
9
+ import { FontOptions, FontStyle, FontWeight, SVGSerializable, UID } from "./interfaces";
10
+
11
+ /**
12
+ * @classdesc A simple text element: position, fontSize, fontFamily, color, textAlign, lineHeight and rotation.
13
+ *
14
+ * @requires FontOptions
15
+ * @requires FontSize
16
+ * @requires FontStyle
17
+ * @requires FontWeight
18
+ * @requires Vertex
19
+ * @requires SVGSerializale
20
+ * @requires UID
21
+ * @requires UIDGenerator
22
+ **/
23
+ export class PBText implements SVGSerializable, FontOptions {
24
+ /**
25
+ * Required to generate proper CSS classes and other class related IDs.
26
+ **/
27
+ readonly className: string = "PBText";
28
+
29
+ /**
30
+ * The UID of this drawable object.
31
+ *
32
+ * @member {UID}
33
+ * @memberof PBText
34
+ * @instance
35
+ * @readonly
36
+ */
37
+ readonly uid: UID;
38
+
39
+ /**
40
+ * @member {string}
41
+ * @memberof PBText
42
+ * @instance
43
+ */
44
+ text: string;
45
+
46
+ /**
47
+ * @member {Vertex}
48
+ * @memberof PBText
49
+ * @instance
50
+ */
51
+ anchor: Vertex;
52
+
53
+ /**
54
+ * @member {number}
55
+ * @memberof PBText
56
+ * @instance
57
+ */
58
+ fontSize: number | undefined;
59
+
60
+ /**
61
+ * @member {number}
62
+ * @memberof PBText
63
+ * @instance
64
+ */
65
+ lineHeight: number | undefined;
66
+
67
+ /**
68
+ * @member {FontWeight}
69
+ * @memberof PBText
70
+ * @instance
71
+ */
72
+ fontWeight: FontWeight | undefined;
73
+
74
+ /**
75
+ * @member {FontStyle}
76
+ * @memberof PBText
77
+ * @instance
78
+ */
79
+ fontStyle: FontStyle | undefined;
80
+
81
+ /**
82
+ * @member {CanvasRenderingContext2D["textAlign"]}
83
+ * @memberof PBText
84
+ * @instance
85
+ */
86
+ textAlign: CanvasRenderingContext2D["textAlign"] | undefined;
87
+
88
+ /**
89
+ * @member {number}
90
+ * @memberof PBText
91
+ * @instance
92
+ */
93
+ rotation: number | undefined;
94
+
95
+ /**
96
+ * @member {number}
97
+ * @memberof PBText
98
+ * @instance
99
+ */
100
+ fontFamily: string | undefined;
101
+
102
+ /**
103
+ * @member {number}
104
+ * @memberof PBText
105
+ * @instance
106
+ */
107
+ color: string | undefined;
108
+
109
+ /**
110
+ * Create a new circle with given center point and radius.
111
+ *
112
+ * @constructor
113
+ * @name Circle
114
+ * @param {Vertex} center - The center point of the circle.
115
+ * @param {number} radius - The radius of the circle.
116
+ */
117
+ constructor(text: string, anchor?: Vertex, options?: FontOptions) {
118
+ this.uid = UIDGenerator.next();
119
+ this.text = text;
120
+ this.anchor = anchor ?? new Vertex();
121
+ this.color = options.color;
122
+ this.fontFamily = options.fontFamily;
123
+ this.fontSize = options.fontSize;
124
+ this.fontStyle = options.fontStyle;
125
+ this.fontWeight = options.fontWeight;
126
+ this.lineHeight = options.lineHeight;
127
+ this.textAlign = options.textAlign;
128
+ this.rotation = options.rotation;
129
+ }
130
+
131
+ /**
132
+ * Create an SVG representation of this circle.
133
+ *
134
+ * @deprecated DEPRECATION Please use the drawutilssvg library and an XMLSerializer instead.
135
+ * @method toSVGString
136
+ * @param {object=} options - An optional set of options, like 'className'.
137
+ * @return {string} A string representing the SVG code for this vertex.
138
+ * @instance
139
+ * @memberof Circle
140
+ */
141
+ toSVGString(options: { className?: string }) {
142
+ console.warn(
143
+ "[PBText.toSVGString()] This function is not implemented as it defines a deprecated method. Use the 'drawutilssvg.text()' method instead."
144
+ );
145
+ return "";
146
+ }
147
+ } // END class
@@ -73,7 +73,8 @@
73
73
  * @modified 2021-03-03 Added the `VEllipseSector` drawable.
74
74
  * @modified 2021-03-29 Clearing `currentClassName` and `currentId` after drawing each drawable.
75
75
  * @modified 2021-04-25 Extending `remove` to accept arrays of drawables.
76
- * @version 1.14.0
76
+ * @modified 2021-11-16 Adding the `PBText` drawable.
77
+ * @version 1.15.0
77
78
  *
78
79
  * @file PlotBoilerplate
79
80
  * @fileoverview The main class.
@@ -118,6 +119,8 @@ import {
118
119
  XYDimension,
119
120
  DatGuiProps
120
121
  } from "./interfaces";
122
+ import { PBText } from "./PBText";
123
+ import { AlloyFingerOptions } from "alloyfinger-typescript/src/cjs/alloy_finger";
121
124
 
122
125
  /**
123
126
  * @classdesc The main class of the PlotBoilerplate.
@@ -521,6 +524,12 @@ export class PlotBoilerplate {
521
524
  image: {
522
525
  color: "#a8a8a8",
523
526
  lineWidth: 1
527
+ },
528
+ text: {
529
+ color: "rgba(192,0,128,0.5)",
530
+ lineWidth: 1,
531
+ fill: true,
532
+ anchor: true
524
533
  }
525
534
  }; // END drawConfig
526
535
 
@@ -656,7 +665,7 @@ export class PlotBoilerplate {
656
665
  var blob = new Blob([svgCode], { type: "image/svg;charset=utf-8" });
657
666
  // See documentation for FileSaver.js for usage.
658
667
  // https://github.com/eligrey/FileSaver.js
659
- if (typeof globalThis["saveAs"] != "function")
668
+ if (typeof globalThis["saveAs"] !== "function")
660
669
  throw "Cannot save file; did you load the ./utils/savefile helper function and the eligrey/SaveFile library?";
661
670
  var _saveAs = globalThis["saveAs"];
662
671
  _saveAs(blob, "plotboilerplate.svg");
@@ -768,7 +777,6 @@ export class PlotBoilerplate {
768
777
  add(drawable: Drawable | Array<Drawable>, redraw?: boolean) {
769
778
  if (Array.isArray(drawable)) {
770
779
  const arr: Array<Drawable> = drawable as Array<Drawable>;
771
- // for( var i in arr )
772
780
  for (var i = 0; i < arr.length; i++) {
773
781
  this.add(arr[i], false);
774
782
  }
@@ -806,8 +814,9 @@ export class PlotBoilerplate {
806
814
  this.drawables.push(drawable);
807
815
  } else if (drawable instanceof Polygon) {
808
816
  this.drawables.push(drawable);
809
- // for( var i in drawable.vertices )
810
- for (var i = 0; i < drawable.vertices.length; i++) this.vertices.push(drawable.vertices[i]);
817
+ for (var i = 0; i < drawable.vertices.length; i++) {
818
+ this.vertices.push(drawable.vertices[i]);
819
+ }
811
820
  } else if (drawable instanceof Triangle) {
812
821
  this.drawables.push(drawable);
813
822
  this.vertices.push(drawable.a);
@@ -817,7 +826,9 @@ export class PlotBoilerplate {
817
826
  this.drawables.push(drawable);
818
827
  const bezierPath: BezierPath = drawable as BezierPath;
819
828
  for (var i = 0; i < bezierPath.bezierCurves.length; i++) {
820
- if (!drawable.adjustCircular && i == 0) this.vertices.push(bezierPath.bezierCurves[i].startPoint);
829
+ if (!drawable.adjustCircular && i == 0) {
830
+ this.vertices.push(bezierPath.bezierCurves[i].startPoint);
831
+ }
821
832
  this.vertices.push(bezierPath.bezierCurves[i].endPoint);
822
833
  this.vertices.push(bezierPath.bezierCurves[i].startControlPoint);
823
834
  this.vertices.push(bezierPath.bezierCurves[i].endControlPoint);
@@ -834,6 +845,10 @@ export class PlotBoilerplate {
834
845
  drawable.lowerRight.add(e.params.dragAmount);
835
846
  });
836
847
  drawable.lowerRight.attr.selectable = false;
848
+ } else if (drawable instanceof PBText) {
849
+ this.vertices.push(drawable.anchor);
850
+ this.drawables.push(drawable);
851
+ drawable.anchor.attr.selectable = false;
837
852
  } else {
838
853
  throw "Cannot add drawable of unrecognized type: " + typeof drawable + ".";
839
854
  }
@@ -923,6 +938,8 @@ export class PlotBoilerplate {
923
938
  } else if (drawable instanceof PBImage) {
924
939
  this.removeVertex(drawable.upperLeft, false);
925
940
  this.removeVertex(drawable.lowerRight, false);
941
+ } else if (drawable instanceof PBText) {
942
+ this.removeVertex(drawable.anchor, false);
926
943
  }
927
944
  } // END removeWithVertices
928
945
 
@@ -947,7 +964,9 @@ export class PlotBoilerplate {
947
964
  for (var i = 0; i < this.vertices.length; i++) {
948
965
  if (this.vertices[i] === vert) {
949
966
  this.vertices.splice(i, 1);
950
- if (redraw) this.redraw();
967
+ if (redraw) {
968
+ this.redraw();
969
+ }
951
970
  return;
952
971
  }
953
972
  }
@@ -1320,6 +1339,15 @@ export class PlotBoilerplate {
1320
1339
  draw.circleHandle(d.lowerRight, 3, this.drawConfig.image.color);
1321
1340
  d.lowerRight.attr.renderTime = renderTime;
1322
1341
  }
1342
+ } else if (d instanceof PBText) {
1343
+ fill.setCurrentId(d.uid);
1344
+ fill.text(d.text, d.anchor.x, d.anchor.y, d);
1345
+ if (this.drawConfig.text.anchor) {
1346
+ draw.setCurrentId(`${d.uid}_a0`);
1347
+ draw.setCurrentClassName(`${d.className}-anchor`);
1348
+ (this.drawConfig.text.fill ? fill : draw).point(d.anchor, this.drawConfig.text.color);
1349
+ }
1350
+ d.anchor.attr.renderTime = renderTime;
1323
1351
  } else {
1324
1352
  console.error("Cannot draw object. Unknown class.");
1325
1353
  }
@@ -1617,7 +1645,6 @@ export class PlotBoilerplate {
1617
1645
  // Apply the zoom (the tolerant area should not shrink or grow when zooming)
1618
1646
  tolerance /= _self.draw.scale.x;
1619
1647
  // Search in vertices
1620
- // for( var vindex in _self.vertices ) {
1621
1648
  for (var vindex = 0; vindex < _self.vertices.length; vindex++) {
1622
1649
  var vert: Vertex = _self.vertices[vindex];
1623
1650
  if ((vert.attr.draggable || vert.attr.selectable) && vert.distance(point) < tolerance) {
@@ -1638,10 +1665,7 @@ export class PlotBoilerplate {
1638
1665
  * @return {void}
1639
1666
  **/
1640
1667
  private handleClick(e: XMouseEvent) {
1641
- // x:number,y:number) {
1642
1668
  const _self: PlotBoilerplate = this;
1643
- // const x:number = e.params.pos.x;
1644
- //const y:number = e.params.pos.y;
1645
1669
  var p: IDraggable = this.locatePointNear(
1646
1670
  _self.transformMousePosition(e.params.pos.x, e.params.pos.y),
1647
1671
  PlotBoilerplate.DEFAULT_CLICK_TOLERANCE / Math.min(_self.config.cssScaleX, _self.config.cssScaleY)
@@ -1657,9 +1681,7 @@ export class PlotBoilerplate {
1657
1681
  if (vert.attr.selectable) vert.attr.isSelected = !vert.attr.isSelected;
1658
1682
  }
1659
1683
  _self.redraw();
1660
- } else if (
1661
- this.keyHandler.isDown("y") /* && p.type=='bpath' && (p.pid==BezierPath.START_POINT || p.pid==BezierPath.END_POINT) */
1662
- ) {
1684
+ } else if (this.keyHandler.isDown("y")) {
1663
1685
  _self.vertices[p.vindex].attr.bezierAutoAdjust = !_self.vertices[p.vindex].attr.bezierAutoAdjust;
1664
1686
  _self.redraw();
1665
1687
  }
@@ -1961,7 +1983,8 @@ export class PlotBoilerplate {
1961
1983
  multiTouchStartScale = null;
1962
1984
  _self.draggedElements = [];
1963
1985
  };
1964
- const afProps = {
1986
+ const afProps: AlloyFingerOptions = {
1987
+ // touchStart: (evt: TouchEvent) => {
1965
1988
  touchStart: (evt: TouchEvent) => {
1966
1989
  if (evt.touches.length == 1) {
1967
1990
  touchMovePos = new Vertex(relPos({ x: evt.touches[0].clientX, y: evt.touches[0].clientY }));
@@ -2065,10 +2088,13 @@ export class PlotBoilerplate {
2065
2088
  _self.setZoom(multiTouchStartScale.x * evt.zoom, multiTouchStartScale.y * evt.zoom, center);
2066
2089
  _self.redraw();
2067
2090
  }
2068
- }; // END afProps
2069
- if (window["createAlloyFinger"])
2091
+ } as unknown as AlloyFingerOptions; // END afProps
2092
+ if (window["createAlloyFinger"]) {
2070
2093
  window["createAlloyFinger"](this.eventCatcher ? this.eventCatcher : this.canvas, afProps);
2071
- else new AlloyFinger(this.eventCatcher ? this.eventCatcher : this.canvas, afProps);
2094
+ } else {
2095
+ /* tslint:disable-next-line */
2096
+ new AlloyFinger(this.eventCatcher ? this.eventCatcher : this.canvas, afProps);
2097
+ }
2072
2098
  } catch (e) {
2073
2099
  console.error("Failed to initialize AlloyFinger!");
2074
2100
  console.error(e);
package/src/ts/Vertex.ts CHANGED
@@ -22,7 +22,9 @@
22
22
  * @modified 2020-05-26 Added functions addX(number) and addY(number).
23
23
  * @modified 2020-10-30 Changed the warnings in `sub(...)` and `add(...)` into real errors.
24
24
  * @modified 2021-03-01 Changed the second param `center` in the `rotate` function from Vertex to XYCoords.
25
- * @version 2.4.2
25
+ * @modified 2021-12-01 Changed the type of param of `scale` to XYCoords.
26
+ * @modified 2021-12-01 Added function `scaleXY` for non uniform scaling.
27
+ * @version 2.5.0
26
28
  *
27
29
  * @file Vertex
28
30
  * @public
@@ -406,19 +408,36 @@ export class Vertex implements XYCoords, SVGSerializable {
406
408
 
407
409
  /**
408
410
  * This is a vector-like behavior and 'scales' this vertex
409
- * towards/from a given center.
411
+ * towards/from a given center by one uniform scale factor.
410
412
  *
411
413
  * @method scale
412
414
  * @param {number} factor - The factor to 'scale' this vertex; 1.0 means no change.
413
- * @param {Vertex=} center - The origin of scaling; default is (0,0).
415
+ * @param {XYCoords=} center - The origin of scaling; default is (0,0).
414
416
  * @return {Vertex} this
415
417
  * @instance
416
418
  * @memberof Vertex
417
419
  **/
418
- scale(factor: number, center?: Vertex): Vertex {
419
- if (!center || typeof center === "undefined") center = new Vertex(0, 0);
420
- this.x = center.x + (this.x - center.x) * factor;
421
- this.y = center.y + (this.y - center.y) * factor;
420
+ scale(factor: number, center?: XYCoords): Vertex {
421
+ return this.scaleXY({ x: factor, y: factor }, center);
422
+ }
423
+
424
+ /**
425
+ * This is a vector-like behavior and 'scales' this vertex
426
+ * towards/from a given center by two independent x- and y- scale factors.
427
+ *
428
+ * @method scale
429
+ * @param {number} factor - The factor to 'scale' this vertex; 1.0 means no change.
430
+ * @param {XYCoords=} center - The origin of scaling; default is (0,0).
431
+ * @return {Vertex} this
432
+ * @instance
433
+ * @memberof Vertex
434
+ **/
435
+ scaleXY(factors: XYCoords, center?: XYCoords): Vertex {
436
+ if (!center || typeof center === "undefined") {
437
+ center = { x: 0, y: 0 };
438
+ }
439
+ this.x = center.x + (this.x - center.x) * factors.x;
440
+ this.y = center.y + (this.y - center.y) * factors.y;
422
441
  return this;
423
442
  }
424
443
 
package/src/ts/draw.ts CHANGED
@@ -37,13 +37,15 @@
37
37
  * @modified 2021-02-22 Added the `path` drawing function to draw SVG path data.
38
38
  * @modified 2021-03-31 Added the `endDrawCycle` function from `DrawLib`.
39
39
  * @modified 2021-05-31 Added the `setConfiguration` function from `DrawLib`.
40
- * @version 1.9.0
40
+ * @modified 2021-11-12 Adding more parameters tot the `text()` function: fontSize, textAlign, fontFamily, lineHeight.
41
+ * @modified 2021-11-19 Added the `color` param to the `label(...)` function.
42
+ * @version 1.10.0
41
43
  **/
42
44
 
43
45
  import { CubicBezierCurve } from "./CubicBezierCurve";
44
46
  import { Polygon } from "./Polygon";
45
47
  import { Vertex } from "./Vertex";
46
- import { DrawLib, SVGPathParams, XYCoords, UID, DrawLibConfiguration } from "./interfaces";
48
+ import { DrawLib, SVGPathParams, XYCoords, UID, DrawLibConfiguration, FontStyle, FontWeight } from "./interfaces";
47
49
  import { drawutilssvg } from "./drawutilssvg";
48
50
 
49
51
  // Todo: rename this class to Drawutils?
@@ -845,19 +847,77 @@ export class drawutils implements DrawLib<void> {
845
847
  this.ctx.restore();
846
848
  }
847
849
 
848
- text(text: string, x: number, y: number, options?: { color?: string }) {
850
+ /**
851
+ * Draw a text at the given relative position.
852
+ *
853
+ * @method text
854
+ * @param {string} text - The text to draw.
855
+ * @param {number} x - The x-position to draw the text at.
856
+ * @param {number} y - The y-position to draw the text at.
857
+ * @param {string=} options.color - The Color to use.
858
+ * @param {string=} options.fontFamily - The font family to use.
859
+ * @param {number=} options.fontSize - The font size (in pixels) to use.
860
+ * @param {FontStyle=} options.fontStyle - The font style to use.
861
+ * @param {FontWeight=} options.fontWeight - The font weight to use.
862
+ * @param {number=} options.lineHeight - The line height (in pixels) to use.
863
+ * @param {number=} options.rotation - The (optional) rotation in radians.
864
+ * @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"`.
865
+ * @return {void}
866
+ * @instance
867
+ * @memberof drawutils
868
+ */
869
+ text(
870
+ text: string,
871
+ x: number,
872
+ y: number,
873
+ options?: {
874
+ color?: string;
875
+ fontFamily?: string;
876
+ fontSize?: number;
877
+ fontStyle?: FontStyle;
878
+ fontWeight?: FontWeight;
879
+ lineHeight?: number;
880
+ textAlign?: CanvasRenderingContext2D["textAlign"];
881
+ rotation?: number;
882
+ }
883
+ ) {
884
+ // See https://stackoverflow.com/a/23523697
885
+
849
886
  options = options || {};
850
887
  this.ctx.save();
851
- x = this.offset.x + x * this.scale.x;
852
- y = this.offset.y + y * this.scale.y;
888
+ let relX = this.offset.x + x * this.scale.x;
889
+ let relY = this.offset.y + y * this.scale.y;
853
890
  const color: string = options.color || "black";
891
+ if (options.fontSize || options.fontFamily) {
892
+ // Scaling of text only works in uniform mode
893
+ this.ctx.font =
894
+ (options.fontWeight ? options.fontWeight + " " : "") +
895
+ (options.fontStyle ? options.fontStyle + " " : "") +
896
+ (options.fontSize ? options.fontSize * this.scale.x + "px " : " ") +
897
+ (options.fontFamily
898
+ ? options.fontFamily.indexOf(" ") === -1
899
+ ? options.fontFamily
900
+ : `"${options.fontFamily}"`
901
+ : "Arial");
902
+ }
903
+ if (options.textAlign) {
904
+ this.ctx.textAlign = options.textAlign;
905
+ }
906
+
907
+ const rotation = options.rotation ?? 0.0;
908
+ const lineHeight = (options.lineHeight ?? options.fontSize ?? 0) * this.scale.x;
909
+ this.ctx.translate(relX, relY);
910
+ this.ctx.rotate(rotation);
911
+
854
912
  if (this.fillShapes) {
855
913
  this.ctx.fillStyle = color;
856
- this.ctx.fillText(text, x, y);
914
+ this.ctx.fillText(text, 0, lineHeight / 2);
857
915
  } else {
858
916
  this.ctx.strokeStyle = color;
859
- this.ctx.strokeText(text, x, y);
917
+ this.ctx.strokeText(text, 0, lineHeight / 2);
860
918
  }
919
+ // this.ctx.translate(-relX, -relY);
920
+ // this.ctx.rotate(-rotation); // is this necessary before 'restore()'?
861
921
  this.ctx.restore();
862
922
  }
863
923
 
@@ -878,8 +938,11 @@ export class drawutils implements DrawLib<void> {
878
938
  */
879
939
  label(text: string, x: number, y: number, rotation?: number, color?: string) {
880
940
  this.ctx.save();
941
+
942
+ this.ctx.font = "lighter 9pt Arial";
943
+
881
944
  this.ctx.translate(x, y);
882
- if (typeof rotation != "undefined") this.ctx.rotate(rotation);
945
+ if (typeof rotation !== "undefined") this.ctx.rotate(rotation);
883
946
  this.ctx.fillStyle = color || "black";
884
947
  if (this.fillShapes) {
885
948
  this.ctx.fillText(text, 0, 0);
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