plotboilerplate 1.12.9 → 1.15.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 (113) hide show
  1. package/README.md +5 -1
  2. package/changelog.md +34 -0
  3. package/dist/index.esm.js +435 -92
  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 +449 -91
  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 +43 -15
  20. package/src/cjs/PlotBoilerplate.js.map +1 -1
  21. package/src/cjs/Polygon.d.ts +22 -1
  22. package/src/cjs/Polygon.js +102 -39
  23. package/src/cjs/Polygon.js.map +1 -1
  24. package/src/cjs/Vertex.d.ts +42 -4
  25. package/src/cjs/Vertex.js +62 -7
  26. package/src/cjs/Vertex.js.map +1 -1
  27. package/src/cjs/draw.d.ts +30 -2
  28. package/src/cjs/draw.js +51 -6
  29. package/src/cjs/draw.js.map +1 -1
  30. package/src/cjs/drawgl.d.ts +29 -2
  31. package/src/cjs/drawgl.js +21 -4
  32. package/src/cjs/drawgl.js.map +1 -1
  33. package/src/cjs/drawutilssvg.d.ts +25 -7
  34. package/src/cjs/drawutilssvg.js +62 -14
  35. package/src/cjs/drawutilssvg.js.map +1 -1
  36. package/src/cjs/entry.js +1 -0
  37. package/src/cjs/index.d.ts +1 -0
  38. package/src/cjs/index.js +1 -0
  39. package/src/cjs/index.js.map +1 -1
  40. package/src/cjs/interfaces/DrawLib.d.ts +27 -6
  41. package/src/cjs/interfaces/DrawLib.js +3 -0
  42. package/src/cjs/interfaces/DrawLib.js.map +1 -1
  43. package/src/cjs/interfaces/core.d.ts +9 -1
  44. package/src/cjs/interfaces/core.js +1 -0
  45. package/src/cjs/interfaces/core.js.map +1 -1
  46. package/src/cjs/utils/datastructures/Color.d.ts +28 -15
  47. package/src/cjs/utils/datastructures/Color.js +276 -88
  48. package/src/cjs/utils/datastructures/Color.js.map +1 -1
  49. package/src/cjs/utils/findInVertexArray.d.ts +19 -0
  50. package/src/cjs/utils/findInVertexArray.js +29 -0
  51. package/src/cjs/utils/findInVertexArray.js.map +1 -0
  52. package/src/esm/BezierPath.d.ts +12 -1
  53. package/src/esm/BezierPath.js +34 -5
  54. package/src/esm/BezierPath.js.map +1 -1
  55. package/src/esm/PBText.d.ts +116 -0
  56. package/src/esm/PBText.js +61 -0
  57. package/src/esm/PBText.js.map +1 -0
  58. package/src/esm/PlotBoilerplate.d.ts +2 -1
  59. package/src/esm/PlotBoilerplate.js +43 -15
  60. package/src/esm/PlotBoilerplate.js.map +1 -1
  61. package/src/esm/Polygon.d.ts +22 -1
  62. package/src/esm/Polygon.js +102 -39
  63. package/src/esm/Polygon.js.map +1 -1
  64. package/src/esm/Vertex.d.ts +42 -4
  65. package/src/esm/Vertex.js +62 -7
  66. package/src/esm/Vertex.js.map +1 -1
  67. package/src/esm/draw.d.ts +30 -2
  68. package/src/esm/draw.js +51 -6
  69. package/src/esm/draw.js.map +1 -1
  70. package/src/esm/drawgl.d.ts +29 -2
  71. package/src/esm/drawgl.js +21 -4
  72. package/src/esm/drawgl.js.map +1 -1
  73. package/src/esm/drawutilssvg.d.ts +25 -7
  74. package/src/esm/drawutilssvg.js +62 -14
  75. package/src/esm/drawutilssvg.js.map +1 -1
  76. package/src/esm/entry.js +1 -0
  77. package/src/esm/index.d.ts +1 -0
  78. package/src/esm/index.js +1 -0
  79. package/src/esm/index.js.map +1 -1
  80. package/src/esm/interfaces/DrawLib.d.ts +27 -6
  81. package/src/esm/interfaces/DrawLib.js +3 -0
  82. package/src/esm/interfaces/DrawLib.js.map +1 -1
  83. package/src/esm/interfaces/core.d.ts +9 -1
  84. package/src/esm/interfaces/core.js +1 -0
  85. package/src/esm/interfaces/core.js.map +1 -1
  86. package/src/esm/utils/datastructures/Color.d.ts +28 -15
  87. package/src/esm/utils/datastructures/Color.js +277 -89
  88. package/src/esm/utils/datastructures/Color.js.map +1 -1
  89. package/src/esm/utils/findInVertexArray.d.ts +19 -0
  90. package/src/esm/utils/findInVertexArray.js +25 -0
  91. package/src/esm/utils/findInVertexArray.js.map +1 -0
  92. package/src/ts/BezierPath.ts +37 -5
  93. package/src/ts/PBText.ts +147 -0
  94. package/src/ts/PlotBoilerplate.ts +47 -19
  95. package/src/ts/Polygon.ts +493 -451
  96. package/src/ts/Vertex.ts +66 -7
  97. package/src/ts/draw.ts +71 -8
  98. package/src/ts/drawgl.ts +48 -8
  99. package/src/ts/drawutilssvg.ts +94 -17
  100. package/src/ts/index.ts +1 -0
  101. package/src/ts/interfaces/DrawLib.ts +30 -4
  102. package/src/ts/interfaces/core.ts +9 -0
  103. package/src/ts/utils/datastructures/Color.ts +695 -508
  104. package/src/ts/utils/findInVertexArray.ts +28 -0
  105. package/dist/plotboilerplate.browser.min.js.LICENSE.txt +0 -8
  106. package/src/ts/utils/algorithms/clearPolygonDuplicateVertices.ts~ +0 -5
  107. package/src/ts/utils/algorithms/findPolygonSelfIntersections.ts~ +0 -45
  108. package/src/ts/utils/algorithms/sh-tmp.ts~ +0 -59
  109. package/src/ts/utils/algorithms/splitPolygonToNonIntersecting.ts~ +0 -113
  110. package/src/ts/utils/algorithms/sutherlandHodgman.ts~ +0 -88
  111. package/src/ts/utils/datastructures/Color.ts~ +0 -331
  112. package/src/ts/utils/datastructures/Girih.ts~ +0 -151
  113. package/src/ts/utils/datastructures/GirihTile.ts~ +0 -419
package/src/ts/Vertex.ts CHANGED
@@ -22,7 +22,10 @@
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
+ * @modified 2021-12-17 Added the functions `lerp` and `lerpAbs` for linear interpolations.
28
+ * @version 2.6.0
26
29
  *
27
30
  * @file Vertex
28
31
  * @public
@@ -406,19 +409,75 @@ export class Vertex implements XYCoords, SVGSerializable {
406
409
 
407
410
  /**
408
411
  * This is a vector-like behavior and 'scales' this vertex
409
- * towards/from a given center.
412
+ * towards/from a given center by one uniform scale factor.
410
413
  *
411
414
  * @method scale
412
415
  * @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).
416
+ * @param {XYCoords=} center - The origin of scaling; default is (0,0).
414
417
  * @return {Vertex} this
415
418
  * @instance
416
419
  * @memberof Vertex
417
420
  **/
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;
421
+ scale(factor: number, center?: XYCoords): Vertex {
422
+ return this.scaleXY({ x: factor, y: factor }, center);
423
+ }
424
+
425
+ /**
426
+ * Perform a linear interpolation towards the given target vertex.
427
+ * The amount value `t` is relative, `t=0.0` means no change, `t=1.0`
428
+ * means this point will be moved to the exact target position.
429
+ *
430
+ * `t=0.5` will move this point to the middle of the connecting
431
+ * linear segment.
432
+ *
433
+ * @param {XYCoords} target - The target position to lerp this vertex to.
434
+ * @param {number} t - The relative amount, usually in [0..1], but other values will work, too.
435
+ * @returns
436
+ */
437
+ lerp(target: XYCoords, t: number): Vertex {
438
+ var diff = this.difference(target);
439
+ // return new Vertex(this.x + diff.x * t, this.y + diff.y * t);
440
+ this.x += diff.x * t;
441
+ this.y += diff.y * t;
442
+ return this;
443
+ }
444
+
445
+ /**
446
+ * Perform a linear interpolation towards the given target vertex (absolute variant).
447
+ * The amount value `t` is absolute, which means the lerp amount is a direct distance
448
+ * value. This point will have move the amount of the passed distance `u`.
449
+ *
450
+ * @param {XYCoords} target - The target position to lerp this vertex to.
451
+ * @param {number} t - The absolute move amount to use to lerping.
452
+ * @returns
453
+ */
454
+ lerpAbs(target: XYCoords, u: number): Vertex {
455
+ var dist = this.distance(target);
456
+ var diff = this.difference(target);
457
+ var step = { x: diff.x / dist, y: diff.y / dist };
458
+ // return new Vertex(this.x + step.x * u, this.y + step.y * u);
459
+ this.x += step.x * u;
460
+ this.y += step.y * u;
461
+ return this;
462
+ }
463
+
464
+ /**
465
+ * This is a vector-like behavior and 'scales' this vertex
466
+ * towards/from a given center by two independent x- and y- scale factors.
467
+ *
468
+ * @method scale
469
+ * @param {number} factor - The factor to 'scale' this vertex; 1.0 means no change.
470
+ * @param {XYCoords=} center - The origin of scaling; default is (0,0).
471
+ * @return {Vertex} this
472
+ * @instance
473
+ * @memberof Vertex
474
+ **/
475
+ scaleXY(factors: XYCoords, center?: XYCoords): Vertex {
476
+ if (!center || typeof center === "undefined") {
477
+ center = { x: 0, y: 0 };
478
+ }
479
+ this.x = center.x + (this.x - center.x) * factors.x;
480
+ this.y = center.y + (this.y - center.y) * factors.y;
422
481
  return this;
423
482
  }
424
483
 
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.
@@ -13,6 +13,7 @@
13
13
  * @modified 2021-05-31 Added the `drawLib.setConfiguration` function.
14
14
  * @modified 2021-05-31 Splitted the large interfaces.ts file into this one and others.
15
15
  * @modified 2021-06-21 Added `IBounds.getCenter()`.
16
+ * @modified 2021-11-16 Added `text` options to the `DrawConfig`.
16
17
  **/
17
18
 
18
19
  import { Vertex } from "../Vertex";
@@ -28,6 +29,7 @@ import { BezierPath } from "../BezierPath";
28
29
  import { Line } from "../Line";
29
30
  import { PlotBoilerplate } from "../PlotBoilerplate";
30
31
  import { DrawLib } from "./DrawLib";
32
+ import { PBText } from "../PBText";
31
33
 
32
34
  /**
33
35
  * @classdesc Coordinates (x,y) on the plane.
@@ -73,6 +75,7 @@ export type Drawable =
73
75
  | Circle
74
76
  | CircleSector
75
77
  | PBImage
78
+ | PBText
76
79
  | VEllipse
77
80
  | VEllipseSector
78
81
  | Polygon
@@ -189,6 +192,12 @@ export interface DrawConfig {
189
192
  line: DrawSettings;
190
193
  vector: DrawSettings;
191
194
  image: DrawSettings;
195
+ text: {
196
+ color: string;
197
+ lineWidth: number;
198
+ fill?: boolean;
199
+ anchor?: boolean;
200
+ };
192
201
  }
193
202
 
194
203
  /** This is a fix for the problem, that the constructor's "name" attribute is not