plotboilerplate 1.12.10 → 1.13.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 (73) hide show
  1. package/README.md +4 -1
  2. package/changelog.md +13 -0
  3. package/dist/index.esm.js +220 -26
  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 +234 -25
  10. package/dist/plotboilerplate.min.js +1 -1
  11. package/package.json +1 -1
  12. package/src/cjs/PBText.d.ts +116 -0
  13. package/src/cjs/PBText.js +66 -0
  14. package/src/cjs/PBText.js.map +1 -0
  15. package/src/cjs/PlotBoilerplate.d.ts +2 -1
  16. package/src/cjs/PlotBoilerplate.js +27 -1
  17. package/src/cjs/PlotBoilerplate.js.map +1 -1
  18. package/src/cjs/draw.d.ts +30 -2
  19. package/src/cjs/draw.js +51 -6
  20. package/src/cjs/draw.js.map +1 -1
  21. package/src/cjs/drawgl.d.ts +29 -2
  22. package/src/cjs/drawgl.js +21 -4
  23. package/src/cjs/drawgl.js.map +1 -1
  24. package/src/cjs/drawutilssvg.d.ts +25 -7
  25. package/src/cjs/drawutilssvg.js +62 -14
  26. package/src/cjs/drawutilssvg.js.map +1 -1
  27. package/src/cjs/entry.js +1 -0
  28. package/src/cjs/index.d.ts +1 -0
  29. package/src/cjs/index.js +1 -0
  30. package/src/cjs/index.js.map +1 -1
  31. package/src/cjs/interfaces/DrawLib.d.ts +27 -6
  32. package/src/cjs/interfaces/DrawLib.js +3 -0
  33. package/src/cjs/interfaces/DrawLib.js.map +1 -1
  34. package/src/cjs/interfaces/core.d.ts +9 -1
  35. package/src/cjs/interfaces/core.js +1 -0
  36. package/src/cjs/interfaces/core.js.map +1 -1
  37. package/src/esm/PBText.d.ts +116 -0
  38. package/src/esm/PBText.js +61 -0
  39. package/src/esm/PBText.js.map +1 -0
  40. package/src/esm/PlotBoilerplate.d.ts +2 -1
  41. package/src/esm/PlotBoilerplate.js +27 -1
  42. package/src/esm/PlotBoilerplate.js.map +1 -1
  43. package/src/esm/draw.d.ts +30 -2
  44. package/src/esm/draw.js +51 -6
  45. package/src/esm/draw.js.map +1 -1
  46. package/src/esm/drawgl.d.ts +29 -2
  47. package/src/esm/drawgl.js +21 -4
  48. package/src/esm/drawgl.js.map +1 -1
  49. package/src/esm/drawutilssvg.d.ts +25 -7
  50. package/src/esm/drawutilssvg.js +62 -14
  51. package/src/esm/drawutilssvg.js.map +1 -1
  52. package/src/esm/entry.js +1 -0
  53. package/src/esm/index.d.ts +1 -0
  54. package/src/esm/index.js +1 -0
  55. package/src/esm/index.js.map +1 -1
  56. package/src/esm/interfaces/DrawLib.d.ts +27 -6
  57. package/src/esm/interfaces/DrawLib.js +3 -0
  58. package/src/esm/interfaces/DrawLib.js.map +1 -1
  59. package/src/esm/interfaces/core.d.ts +9 -1
  60. package/src/esm/interfaces/core.js +1 -0
  61. package/src/esm/interfaces/core.js.map +1 -1
  62. package/src/ts/PBText.ts +147 -0
  63. package/src/ts/PlotBoilerplate.ts +24 -1
  64. package/src/ts/draw.ts +71 -8
  65. package/src/ts/drawgl.ts +48 -8
  66. package/src/ts/drawutilssvg.ts +94 -17
  67. package/src/ts/index.ts +1 -0
  68. package/src/ts/interfaces/DrawLib.ts +30 -4
  69. package/src/ts/interfaces/core.ts +9 -0
  70. package/src/ts/utils/algorithms/clearPolygonDuplicateVertices.ts~ +0 -5
  71. package/src/ts/utils/algorithms/findPolygonSelfIntersections.ts~ +0 -45
  72. package/src/ts/utils/algorithms/splitPolygonToNonIntersecting.ts~ +0 -113
  73. package/src/ts/utils/algorithms/sutherlandHodgman.ts~ +0 -88
@@ -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,7 @@ import {
118
119
  XYDimension,
119
120
  DatGuiProps
120
121
  } from "./interfaces";
122
+ import { PBText } from "./PBText";
121
123
 
122
124
  /**
123
125
  * @classdesc The main class of the PlotBoilerplate.
@@ -521,6 +523,12 @@ export class PlotBoilerplate {
521
523
  image: {
522
524
  color: "#a8a8a8",
523
525
  lineWidth: 1
526
+ },
527
+ text: {
528
+ color: "rgba(192,0,128,0.5)",
529
+ lineWidth: 1,
530
+ fill: true,
531
+ anchor: true
524
532
  }
525
533
  }; // END drawConfig
526
534
 
@@ -834,6 +842,10 @@ export class PlotBoilerplate {
834
842
  drawable.lowerRight.add(e.params.dragAmount);
835
843
  });
836
844
  drawable.lowerRight.attr.selectable = false;
845
+ } else if (drawable instanceof PBText) {
846
+ this.vertices.push(drawable.anchor);
847
+ this.drawables.push(drawable);
848
+ drawable.anchor.attr.selectable = false;
837
849
  } else {
838
850
  throw "Cannot add drawable of unrecognized type: " + typeof drawable + ".";
839
851
  }
@@ -923,6 +935,8 @@ export class PlotBoilerplate {
923
935
  } else if (drawable instanceof PBImage) {
924
936
  this.removeVertex(drawable.upperLeft, false);
925
937
  this.removeVertex(drawable.lowerRight, false);
938
+ } else if (drawable instanceof PBText) {
939
+ this.removeVertex(drawable.anchor, false);
926
940
  }
927
941
  } // END removeWithVertices
928
942
 
@@ -1320,6 +1334,15 @@ export class PlotBoilerplate {
1320
1334
  draw.circleHandle(d.lowerRight, 3, this.drawConfig.image.color);
1321
1335
  d.lowerRight.attr.renderTime = renderTime;
1322
1336
  }
1337
+ } else if (d instanceof PBText) {
1338
+ fill.setCurrentId(d.uid);
1339
+ fill.text(d.text, d.anchor.x, d.anchor.y, d);
1340
+ if (this.drawConfig.text.anchor) {
1341
+ draw.setCurrentId(`${d.uid}_a0`);
1342
+ draw.setCurrentClassName(`${d.className}-anchor`);
1343
+ (this.drawConfig.text.fill ? fill : draw).point(d.anchor, this.drawConfig.text.color);
1344
+ }
1345
+ d.anchor.attr.renderTime = renderTime;
1323
1346
  } else {
1324
1347
  console.error("Cannot draw object. Unknown class.");
1325
1348
  }
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
 
@@ -26,14 +26,30 @@
26
26
  * @modified 2021-03-31 Added 'ellipseSector' the the class names.
27
27
  * @modified 2021-03-31 Implemented buffering using a buffer <g> node and the beginDrawCycle and endDrawCycle methods.
28
28
  * @modified 2021-05-31 Added the `setConfiguration` function from `DrawLib`.
29
- * @version 1.3.0
29
+ * @modified 2021-11-15 Adding more parameters tot the `text()` function: fontSize, textAlign, fontFamily, lineHeight.
30
+ * @modified 2021-11-19 Fixing the `label(text,x,y)` position.
31
+ * @modified 2021-11-19 Added the `color` param to the `label(...)` function.
32
+ * @version 1.4.0
30
33
  **/
31
34
 
32
35
  import { CircleSector } from "./CircleSector";
33
36
  import { CubicBezierCurve } from "./CubicBezierCurve";
34
37
  import { Polygon } from "./Polygon";
35
38
  import { Vertex } from "./Vertex";
36
- import { DrawConfig, DrawLib, DrawSettings, XYCoords, XYDimension, SVGPathParams, UID, DrawLibConfiguration } from "./interfaces";
39
+ import {
40
+ DrawConfig,
41
+ DrawLib,
42
+ DrawSettings,
43
+ XYCoords,
44
+ XYDimension,
45
+ SVGPathParams,
46
+ UID,
47
+ DrawLibConfiguration,
48
+ FontStyle,
49
+ FontWeight
50
+ } from "./interfaces";
51
+
52
+ const RAD_TO_DEG = 180 / Math.PI;
37
53
 
38
54
  /**
39
55
  * @classdesc A helper class for basic SVG drawing operations. This class should
@@ -203,7 +219,8 @@ export class drawutilssvg implements DrawLib<void | SVGElement> {
203
219
  "vertex": "Vertex",
204
220
  "line": "Line",
205
221
  "vector": "Vector",
206
- "image": "Image"
222
+ "image": "Image",
223
+ "text": "Text"
207
224
  };
208
225
  // Question: why isn't this working if the svgNode is created dynamically? (nodeStyle.sheet is null)
209
226
  const rules = [];
@@ -1065,29 +1082,82 @@ export class drawutilssvg implements DrawLib<void | SVGElement> {
1065
1082
  }
1066
1083
  if (!isOpen) d.push("Z");
1067
1084
  node.setAttribute("d", d.join(" "));
1068
- return this._bindFillDraw(node, "polyline", color, lineWidth || 1);
1085
+ return this._bindFillDraw(node, "polygon", color, lineWidth || 1);
1069
1086
  }
1070
1087
 
1071
1088
  /**
1072
- * Draw a text label at the given relative position.
1089
+ * Draw a text at the given relative position.
1073
1090
  *
1074
- * @method label
1091
+ * @method text
1075
1092
  * @param {string} text - The text to draw.
1076
1093
  * @param {number} x - The x-position to draw the text at.
1077
1094
  * @param {number} y - The y-position to draw the text at.
1078
- * @param {number=} rotation - The (optional) rotation in radians.
1095
+ * @param {string=} options.color - The Color to use.
1096
+ * @param {string=} options.fontFamily - The font family to use.
1097
+ * @param {number=} options.fontSize - The font size (in pixels) to use.
1098
+ * @param {FontStyle=} options.fontStyle - The font style to use.
1099
+ * @param {FontWeight=} options.fontWeight - The font weight to use.
1100
+ * @param {number=} options.lineHeight - The line height (in pixels) to use.
1101
+ * @param {number=} options.rotation - The (optional) rotation in radians.
1102
+ * @param {string=} options.textAlign - The text align to use. According to the specifiactions (https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/textAlign) valid values are `"left" || "right" || "center" || "start" || "end"`.
1079
1103
  * @return {void}
1080
1104
  * @instance
1081
- * @memberof drawutilssvg
1105
+ * @memberof drawutils
1082
1106
  */
1083
- text(text: string, x: number, y: number, options?: { color?: string }): SVGElement {
1107
+ text(
1108
+ text: string,
1109
+ x: number,
1110
+ y: number,
1111
+ options?: {
1112
+ color?: string;
1113
+ fontFamily?: string;
1114
+ fontSize?: number;
1115
+ fontStyle?: FontStyle;
1116
+ fontWeight?: FontWeight;
1117
+ lineHeight?: number;
1118
+ textAlign?: CanvasRenderingContext2D["textAlign"];
1119
+ rotation?: number;
1120
+ }
1121
+ ): SVGElement {
1084
1122
  options = options || {};
1085
1123
  const color: string = options.color || "black";
1086
-
1087
- const node: SVGElement = this.makeNode("text");
1088
- node.setAttribute("x", `${this._x(x)}`);
1089
- node.setAttribute("y", `${this._y(y)}`);
1090
- node.innerHTML = text;
1124
+ const lineHeight = (options.lineHeight ?? options.fontSize ?? 0) * this.scale.x;
1125
+ // https://www.w3.org/TR/SVG/text.html#TextAnchorProperty
1126
+ // start | middle | end
1127
+ const textAlign: "start" | "middle" | "end" =
1128
+ options.textAlign === "left" || options.textAlign === "start"
1129
+ ? "start"
1130
+ : options.textAlign === "center"
1131
+ ? "middle"
1132
+ : options.textAlign === "right" || options.textAlign === "end"
1133
+ ? "end"
1134
+ : "start";
1135
+ const transformOrigin = `${this._x(x)}px ${this._y(y)}px`;
1136
+ const translate = `translate(${this._x(x)} ${this._y(y) + lineHeight / 2})`;
1137
+ // Safari has a transform-origin/rotation bug.
1138
+ // It's essential to use rotate(r,x,y) here. "rotate(r)"" with transform-origin(x,y) won't do the job.
1139
+ // And rotate and translate cannot be used is combination on a text object.
1140
+ // So wrap the text inside a <g>, translate the <g>, and rotate the text inside.
1141
+ const rotate = options.rotation ? `rotate(${options.rotation * RAD_TO_DEG} 0 0)` : ``;
1142
+
1143
+ const node: SVGElement = this.makeNode("g");
1144
+ const curId = this.curId;
1145
+ this.curId = curId + "_text";
1146
+ const textNode: SVGElement = this.makeNode("text");
1147
+ node.appendChild(textNode);
1148
+ textNode.setAttribute("font-family", options.fontFamily); // May be undefined
1149
+ textNode.setAttribute("font-size", options.fontSize ? `${options.fontSize * this.scale.x}` : null);
1150
+ textNode.setAttribute("font-style", options.fontStyle ? `${options.fontStyle}` : null);
1151
+ textNode.setAttribute("font-weight", options.fontWeight ? `${options.fontWeight}` : null);
1152
+ textNode.setAttribute("text-anchor", textAlign);
1153
+ textNode.setAttribute("transform-origin", "0 0");
1154
+ textNode.setAttribute("transform", rotate);
1155
+ node.setAttribute("transform-origin", transformOrigin);
1156
+ node.setAttribute("transform", translate);
1157
+
1158
+ textNode.innerHTML = text;
1159
+ // Restore old ID
1160
+ this.curId = curId;
1091
1161
  return this._bindFillDraw(node, "text", color, 1);
1092
1162
  }
1093
1163
 
@@ -1099,16 +1169,23 @@ export class drawutilssvg implements DrawLib<void | SVGElement> {
1099
1169
  * @param {number} x - The x-position to draw the text at.
1100
1170
  * @param {number} y - The y-position to draw the text at.
1101
1171
  * @param {number=} rotation - The (optional) rotation in radians.
1172
+ * @param {string="black"} color - The color to use (default is black).
1102
1173
  * @return {void}
1103
1174
  * @instance
1104
1175
  * @memberof drawutilssvg
1105
1176
  */
1106
- label(text: string, x: number, y: number, rotation: number): SVGElement {
1177
+ label(text: string, x: number, y: number, rotation?: number, color?: string): SVGElement {
1107
1178
  const node: SVGElement = this.makeNode("text");
1108
1179
  // For some strange reason SVG rotation transforms use degrees instead of radians
1109
- node.setAttribute("transform", `translate(${this.offset.x},${this.offset.y}), rotate(${(rotation / Math.PI) * 180})`);
1180
+ node.setAttribute("transform", `translate(${x},${y}), rotate(${((rotation || 0) / Math.PI) * 180})`);
1181
+
1182
+ node.setAttribute("font-family", "Arial");
1183
+ node.setAttribute("font-size", "9pt");
1184
+ node.setAttribute("font-style", "normal");
1185
+ node.setAttribute("font-weight", "lighter");
1186
+
1110
1187
  node.innerHTML = text;
1111
- return this._bindFillDraw(node, "label", "black", null);
1188
+ return this._bindFillDraw(node, "label", color || "black", null);
1112
1189
  }
1113
1190
 
1114
1191
  /**
package/src/ts/index.ts CHANGED
@@ -13,6 +13,7 @@ export * from "./KeyHandler";
13
13
  export * from "./Line";
14
14
  export * from "./MouseHandler";
15
15
  export * from "./PBImage";
16
+ export * from "./PBText";
16
17
  export * from "./PlotBoilerplate";
17
18
  export * from "./Polygon";
18
19
  export * from "./SVGBuilder";
@@ -12,6 +12,9 @@
12
12
  * @modified 2021-03-30 Added the `endDrawCycle` function to `DrawLib`.
13
13
  * @modified 2021-05-31 Added the `drawLib.setConfiguration` function.
14
14
  * @modified 2021-05-31 Splitted the large interfaces.ts file into this one and others.
15
+ * @modified 2021-11-12 Added `text()` params fontSize, fontFamily, rotation, textAlign.
16
+ * @modified 2021-11-16 Added `text()` params fontWeight and fontStyle.
17
+ * @modified 2021-11-19 Added the `color` param to the `label(...)` function.
15
18
  **/
16
19
 
17
20
  import { Polygon } from "../Polygon";
@@ -27,6 +30,21 @@ export interface DrawLibConfiguration {
27
30
  blendMode?: 'source-over' | 'source-in' | 'source-out' | 'source-atop' | 'destination-over' | 'destination-in' | 'destination-out' | 'destination-atop' | 'lighter' | 'copy' | 'xor' | 'multiply' | 'screen' | 'overlay' | 'darken' | 'lighten' | 'color-dodge' | 'color-burn' | 'hard-light' | 'soft-light' | 'difference' | 'exclusion' | 'hue' | 'saturation' | 'color' | 'luminosity' | null;
28
31
  }
29
32
 
33
+ export type FontWeight = "normal" | "bold" | "bolder" | "lighter" | 100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900;
34
+
35
+ export type FontStyle = "normal" | "italic" | "oblique";
36
+
37
+ export interface FontOptions {
38
+ color?: string;
39
+ fontFamily?: string;
40
+ fontSize?: number;
41
+ fontStyle?: FontStyle;
42
+ fontWeight?: FontWeight;
43
+ lineHeight?: number;
44
+ textAlign?: CanvasRenderingContext2D["textAlign"];
45
+ rotation?: number;
46
+ }
47
+
30
48
  /**
31
49
  * An interface all drawing libraries must implement to be used with PlotBoilerplate.
32
50
  *
@@ -432,18 +450,25 @@ export interface DrawLib<R> {
432
450
  polyline: (vertices: Array<Vertex>, isOpen: boolean, color: string, lineWidth?: number) => R;
433
451
 
434
452
  /**
435
- * Draw a text label at the given relative position.
453
+ * Draw a text at the given relative position.
436
454
  *
437
455
  * @method text
438
456
  * @param {string} text - The text to draw.
439
457
  * @param {number} x - The x-position to draw the text at.
440
458
  * @param {number} y - The y-position to draw the text at.
441
- * @param {number=} rotation - The (optional) rotation in radians.
459
+ * @param {string=} options.color - The Color to use.
460
+ * @param {string=} options.fontFamily - The font family to use.
461
+ * @param {number=} options.fontSize - The font size (in pixels) to use.
462
+ * @param {FontStyle=} options.fontStyle - The font style to use.
463
+ * @param {FontWeight=} options.fontWeight - The font weight to use.
464
+ * @param {number=} options.lineHeight - The line height (in pixels) to use.
465
+ * @param {number=} options.rotation - The (optional) rotation in radians.
466
+ * @param {string=} options.textAlign - The text align to use. According to the specifiactions (https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/textAlign) valid values are `"left" || "right" || "center" || "start" || "end"`.
442
467
  * @return {void}
443
468
  * @instance
444
469
  * @memberof drawutils
445
470
  */
446
- text: (text: string, x: number, y: number, options?: { color?: string }) => R;
471
+ text: (text: string, x: number, y: number, options?: FontOptions) => R;
447
472
 
448
473
  /**
449
474
  * Draw a non-scaling text label at the given absolute position.
@@ -453,11 +478,12 @@ export interface DrawLib<R> {
453
478
  * @param {number} x - The x-position to draw the text at.
454
479
  * @param {number} y - The y-position to draw the text at.
455
480
  * @param {number=} rotation - The (aoptional) rotation in radians.
481
+ * @param {string="black"} color - The color to use (default is black).
456
482
  * @return {void}
457
483
  * @instance
458
484
  * @memberof drawutils
459
485
  */
460
- label: (text: string, x: number, y: number, rotation: number) => R;
486
+ label: (text: string, x: number, y: number, rotation?: number, color?: string) => R;
461
487
 
462
488
  /**
463
489
  * Draw an SVG-like path given by the specified path data.