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
@@ -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