/
drawutilssvg.ts
2062 lines (1945 loc) · 73.4 KB
/
drawutilssvg.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Draws elements into an SVG node.
*
* Note that this library uses buffers and draw cycles. To draw onto an SVG canvas, do this:
* const drawLib = new drawutilssvg( svgNode, ... );
* const fillLib = drawLib.copyInstance(true);
* // Begin draw cycle
* drawLib.beginDrawCycle(time);
* // ... draw or fill your stuff ...
* drawLib.endDrawCycle(time); // Here the elements become visible
*
* @author Ikaros Kappler
* @date 2021-01-03
* @modified 2021-01-24 Fixed the `fillShapes` attribute in the copyInstance function.
* @modified 2021-01-26 Changed the `isPrimary` (default true) attribute to `isSecondary` (default false).
* @modified 2021-02-03 Added the static `createSvg` function.
* @modified 2021-02-03 Fixed the currentId='background' bug on the clear() function.
* @modified 2021-02-03 Fixed CSSProperty `stroke-width` (was line-width before, which is wrong).
* @modified 2021-02-03 Added the static `HEAD_XML` attribute.
* @modified 2021-02-19 Added the static helper function `transformPathData(...)` for svg path transformations (scale and translate).
* @modified 2021-02-22 Added the static helper function `copyPathData(...)`.
* @modified 2021-02-22 Added the `path` drawing function to draw SVG path data.
* @modified 2021-03-01 Fixed a bug in the `clear` function (curClassName was not cleared).
* @modified 2021-03-29 Fixed a bug in the `text` function (second y param was wrong, used x here).
* @modified 2021-03-29 Moved this file from `src/ts/utils/helpers/` to `src/ts/`.
* @modified 2021-03-31 Added 'ellipseSector' the the class names.
* @modified 2021-03-31 Implemented buffering using a buffer <g> node and the beginDrawCycle and endDrawCycle methods.
* @modified 2021-05-31 Added the `setConfiguration` function from `DrawLib`.
* @modified 2021-11-15 Adding more parameters tot the `text()` function: fontSize, textAlign, fontFamily, lineHeight.
* @modified 2021-11-19 Fixing the `label(text,x,y)` position.
* @modified 2021-11-19 Added the `color` param to the `label(...)` function.
* @modified 2022-02-03 Added the `lineWidth` param to the `crosshair` function.
* @modified 2022-02-03 Added the `cross(...)` function.
* @modified 2022-03-26 Added the private `nodeDefs` and `bufferedNodeDefs` attributes.
* @modified 2022-03-26 Added the `texturedPoly` function to draw textures polygons.
* @modified 2022-07-26 Adding `alpha` to the `image(...)` function.
* @modified 2022-11-10 Tweaking some type issues.
* @modified 2023-02-04 Fixed a typo in the CSS classname for cubic Bézier paths: cubicBezier (was cubierBezier).
* @modified 2023-02-10 The methods `setCurrentClassName` and `setCurrentId` also accept `null` now.
* @modified 2023-09-29 Added initialization checks for null parameters.
* @modified 2023-09-29 Added a missing implementation to the `drawurilssvg.do(XYCoords,string)` function. Didn't draw anything.
* @modified 2023-09-29 Downgrading all `Vertex` param type to the more generic `XYCoords` type in these render functions: line, arrow, texturedPoly, cubicBezier, cubicBezierPath, handle, handleLine, dot, point, circle, circleArc, ellipse, grid, raster.
* @modified 2023-09-29 Added the `headLength` parameter to the 'DrawLib.arrow()` function.
* @modified 2023-09-29 Added the `arrowHead(...)` function to the 'DrawLib.arrow()` interface.
* @modified 2023-09-29 Added the `cubicBezierArrow(...)` function to the 'DrawLib.arrow()` interface.
* @modified 2023-10-04 Adding `strokeOptions` param to these draw function: line, arrow, cubicBezierArrow, cubicBezier, cubicBezierPath, circle, circleArc, ellipse, square, rect, polygon, polyline.
*
* @version 1.6.7
**/
import { CircleSector } from "./CircleSector";
import { CubicBezierCurve } from "./CubicBezierCurve";
import { Polygon } from "./Polygon";
import { Vertex } from "./Vertex";
import {
DrawConfig,
DrawLib,
DrawSettings,
XYCoords,
XYDimension,
SVGPathParams,
UID,
DrawLibConfiguration,
FontStyle,
FontWeight,
StrokeOptions
} from "./interfaces";
import { Bounds } from "./Bounds";
import { UIDGenerator } from "./UIDGenerator";
import { Vector } from "./Vector";
const RAD_TO_DEG = 180 / Math.PI;
/**
* @classdesc A helper class for basic SVG drawing operations. This class should
* be compatible to the default 'draw' class.
*
* @requires CubicBzierCurvce
* @requires Polygon
* @requires Vertex
* @requires XYCoords
*/
export class drawutilssvg implements DrawLib<void | SVGElement> {
static HEAD_XML = [
'<?xml version="1.0" encoding="UTF-8" standalone="no"?>',
'<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" ',
' "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">',
""
].join("\n");
/**
* @member {SVGGElement}
* @memberof drawutilssvg
* @instance
*/
svgNode: SVGElement;
/**
* The root elements container <g> in the svgNode.
*/
private gNode: SVGGElement;
/**
* To avoid flickering the lib draws on a buffer, which is replacing the old <g> node at the end of the draw cycle.
* @member {SVGGelement}
* @memberof drawutilssvg
* @instance
* @private
*/
private bufferGNode: SVGGElement;
/**
* A style node of type `<style>`.
* @member {SVGGelement}
* @memberof drawutilssvg
* @instance
* @private
*/
private nodeStyle: SVGStyleElement;
/**
* A style node of type `<defs>`.
* @member {SVGGelement}
* @memberof drawutilssvg
* @instance
* @private
*/
private nodeDefs: SVGDefsElement;
/**
* The buffered nodeDefs.
* @member {SVGGelement}
* @memberof drawutilssvg
* @instance
* @private
*/
private bufferedNodeDefs: SVGDefsElement;
/**
* @member {Vertex}
* @memberof drawutilssvg
* @instance
*/
scale: Vertex;
/**
* @member {Vertex}
* @memberof drawutilssvg
* @instance
*/
offset: Vertex;
/**
* @member {boolean}
* @memberof drawutilssvg
* @instance
*/
fillShapes: boolean;
/**
* @member {XYDimension}
* @memberof drawutilssvg
* @instance
*/
canvasSize: XYDimension;
/**
* The current drawlib configuration to be used for all upcoming draw operations.
* @member {DrawLibConfiguration}
* @memberof drawutilssvg
* @instance
*/
drawlibConfiguration: DrawLibConfiguration;
/**
* The current drawable-ID. This can be any unique ID identifying the following drawn element.
*
* @member {UID|null}
* @memberof drawutilssvg
* @instance
*/
private curId: UID | null;
/**
* The current drawable-classname.
*/
private curClassName: string | null;
/**
* The SVG element cache. On clear() all elements are kept for possible re-use on next draw cycle.
*/
private cache: Map<UID, SVGElement>;
/**
* Indicates if this library is the primary or seconday instance (draw an fill share the same DOM nodes).
*/
private isSecondary: boolean;
/**
* The constructor.
*
* @constructor
* @name drawutilssvg
* @param {SVGElement} svgNode - The SVG node to use.
* @param {XYCoords} offset - The draw offset to use.
* @param {XYCoords} scale - The scale factors to use.
* @param {XYDimension} canvasSize - The initial canvas size (use setSize to change).
* @param {boolean} fillShapes - Indicates if the constructed drawutils should fill all drawn shapes (if possible).
* @param {DrawConfig} drawConfig - The default draw config to use for CSS fallback styles.
* @param {boolean=} isSecondary - (optional) Indicates if this is the primary or secondary instance. Only primary instances manage child nodes.
* @param {SVGGElement=} gNode - (optional) Primary and seconday instances share the same <g> node.
**/
constructor(
svgNode: SVGElement,
offset: XYCoords,
scale: XYCoords,
canvasSize: XYDimension,
fillShapes: boolean,
drawConfig: DrawConfig,
isSecondary?: boolean,
gNode?: SVGGElement,
bufferGNode?: SVGGElement,
nodeDefs?: SVGDefsElement,
bufferNodeDefs?: SVGDefsElement
) {
this.svgNode = svgNode;
this.offset = new Vertex(0, 0).set(offset);
this.scale = new Vertex(1, 1).set(scale);
this.fillShapes = fillShapes;
this.isSecondary = Boolean(isSecondary);
this.drawlibConfiguration = {} as DrawLibConfiguration;
this.cache = new Map<UID, SVGElement>();
this.setSize(canvasSize);
if (isSecondary) {
if (!gNode || !bufferGNode || !nodeDefs || !bufferNodeDefs) {
throw "Cannot create secondary svg draw lib with undefinde gNode|bufferGNode|nodeDefs|bufferNodeDefs.";
}
this.gNode = gNode;
this.bufferGNode = bufferGNode;
this.nodeDefs = nodeDefs;
this.bufferedNodeDefs = bufferNodeDefs;
} else {
this.addStyleDefs(drawConfig);
this.addDefsNode();
this.gNode = this.createSVGNode("g") as SVGGElement;
this.bufferGNode = this.createSVGNode("g") as SVGGElement;
this.svgNode.appendChild(this.gNode);
}
}
/**
* Adds a default style defintion based on the passed DrawConfig.
* Twaek the draw config to change default colors or line thicknesses.
*
* @param {DrawConfig} drawConfig
*/
private addStyleDefs(drawConfig: DrawConfig) {
this.nodeStyle = this.createSVGNode("style") as SVGStyleElement;
this.svgNode.appendChild(this.nodeStyle);
// Which default styles to add? -> All from the DrawConfig.
// Compare with DrawConfig interface
const keys = {
// "bezier": "CubicBezierCurve", // TODO: is this correct?
"bezierPath": "BezierPath",
"polygon": "Polygon",
"triangle": "Triangle",
"ellipse": "Ellipse",
"ellipseSector": "EllipseSector",
"circle": "Circle",
"circleSector": "CircleSector",
"vertex": "Vertex",
"line": "Line",
"vector": "Vector",
"image": "Image",
"text": "Text"
};
// Question: why isn't this working if the svgNode is created dynamically? (nodeStyle.sheet is null)
const rules: Array<string> = [];
for (var k in keys) {
const className: string = keys[k];
const drawSettings: DrawSettings | undefined = drawConfig[k];
if (drawSettings) {
rules.push(`.${className} { fill : none; stroke: ${drawSettings.color}; stroke-width: ${drawSettings.lineWidth}px }`);
} else {
console.warn(`Warning: your draw config is missing the key '${k}' which is required.`);
}
}
this.nodeStyle.innerHTML = rules.join("\n");
}
/**
* Adds the internal <defs> node.
*/
private addDefsNode() {
this.nodeDefs = this.createSVGNode("defs") as SVGDefsElement;
// this.svgNode.appendChild(this.nodeDefs);
this.bufferedNodeDefs = this.createSVGNode("defs") as SVGDefsElement;
this.svgNode.appendChild(this.nodeDefs);
}
/**
* This is a simple way to include custom CSS class mappings to the style defs of the generated SVG.
*
* The mapping should be of the form
* [style-class] -> [style-def-string]
*
* Example:
* "rect.red" -> "fill: #ff0000; border: 1px solid red"
*
* @param {Map<string,string>} defs
*/
addCustomStyleDefs(defs: Map<string, string>) {
const buffer: string[] = [];
defs.forEach((value: string, key: string) => {
buffer.push(key + " { " + value + " }");
});
this.nodeStyle.innerHTML += "\n/* Custom styles */\n" + buffer.join("\n");
}
/**
* Retieve an old (cached) element.
* Only if both – key and nodeName – match, the element will be returned (null otherwise).
*
* @method findElement
* @private
* @memberof drawutilssvg
* @instance
* @param {UID} key - The key of the desired element (used when re-drawing).
* @param {string} nodeName - The expected node name.
*/
private findElement(key: UID | null, nodeName: string): SVGElement | null {
if (!key) {
return null;
}
var node: SVGElement | undefined = this.cache.get(key);
if (node && node.nodeName.toUpperCase() === nodeName.toUpperCase()) {
this.cache.delete(key);
return node;
}
return null;
}
/**
* Create a new DOM node <svg> in the SVG namespace.
*
* @method createSVGNode
* @private
* @memberof drawutilssvg
* @instance
* @param {string} nodeName - The node name (tag-name).
* @return {SVGElement} A new element in the SVG namespace with the given node name.
*/
private createSVGNode(nodeName: string): SVGElement {
return document.createElementNS("http://www.w3.org/2000/svg", nodeName);
}
/**
* Make a new SVG node (or recycle an old one) with the given node name (circle, path, line, rect, ...).
*
* This function is used in draw cycles to re-use old DOM nodes (in hope to boost performance).
*
* @method makeNode
* @private
* @instance
* @memberof drawutilssvg
* @param {string} nodeName - The node name.
* @return {SVGElement} The new node, which is not yet added to any document.
*/
private makeNode(nodeName: string): SVGElement {
// Try to find node in current DOM cache.
// Unique node keys are strictly necessary.
// Try to recycle an old element from cache.
var node: SVGElement | null = this.findElement(this.curId, nodeName);
if (!node) {
// If no such old elements exists (key not found, tag name not matching),
// then create a new one.
node = this.createSVGNode(nodeName);
}
if (this.drawlibConfiguration.blendMode) {
node.style["mix-blend-mode"] = this.drawlibConfiguration.blendMode;
}
// if (this.lineDashEnabled && this.lineDash && this.lineDash.length > 0 && drawutilssvg.nodeSupportsLineDash(nodeName)) {
// node.setAttribute("stroke-dasharray", this.lineDash.join(" "));
// }
return node;
}
/**
* This is the final helper function for drawing and filling stuff and binding new
* nodes to the SVG document.
* It is not intended to be used from the outside.
*
* When in draw mode it draws the current shape.
* When in fill mode it fills the current shape.
*
* This function is usually only called internally.
*
* @method _bindFillDraw
* @private
* @instance
* @memberof drawutilssvg
* @param {SVGElement} node - The node to draw/fill and bind.
* @param {string} className - The class name(s) to use.
* @param {string} color - A stroke/fill color to use.
* @param {number=1} lineWidth - (optional) A line width to use for drawing (default is 1).
* @return {SVGElement} The node itself (for chaining).
*/
private _bindFillDraw(
node: SVGElement,
className: string,
color?: string | null,
lineWidth?: number | null,
strokeOptions?: StrokeOptions
): SVGElement {
this._configureNode(node, className, this.fillShapes, color, lineWidth, strokeOptions);
return this._bindNode(node, undefined);
}
/**
* Bind this given node to a parent. If no parent is passed then the global
* node buffer will be used.
*
* @method _bindNode
* @private
* @instance
* @memberof drawutilssvg
* @param {SVGElement} node - The SVG node to bind.
* @param {SVGElement=} bindingParent - (optional) You may pass node other than the glober buffer node.
* @returns {SVGElement} The passed node itself.
*/
private _bindNode(node: SVGElement, bindingParent?: SVGElement): SVGElement {
if (!node.parentNode) {
// Attach to DOM only if not already attached
(bindingParent ?? this.bufferGNode).appendChild(node);
}
return node;
}
/**
* Add custom CSS class names and the globally defined CSS classname to the
* given node.
*
* @method addCSSClasses
* @private
* @instance
* @memberof drawutilssvg
* @param {SVGElement} node - The SVG node to bind.
* @param {string} className - The additional custom classname to add.
* @returns {void}
*/
private _addCSSClasses(node: SVGElement, className: string) {
if (this.curClassName) {
node.setAttribute("class", `${className} ${this.curClassName}`);
} else {
node.setAttribute("class", className);
}
}
private _configureNode(
node: SVGElement,
className: string,
fillMode: boolean,
color?: string | null,
lineWidth?: number | null,
strokeOptions?: StrokeOptions
): SVGElement {
this._addCSSClasses(node, className);
node.setAttribute("fill", fillMode && color ? color : "none");
node.setAttribute("stroke", fillMode ? "none" : color || "none");
node.setAttribute("stroke-width", `${lineWidth || 1}`);
if (this.curId) {
node.setAttribute("id", `${this.curId}`); // Maybe React-style 'key' would be better?
}
this.applyStrokeOpts(node, strokeOptions);
return node;
}
/**
* Sets the size and view box of the document. Call this if canvas size changes.
*
* @method setSize
* @instance
* @memberof drawutilssvg
* @param {XYDimension} canvasSize - The new canvas size.
*/
setSize(canvasSize: XYDimension) {
this.canvasSize = canvasSize;
this.svgNode.setAttribute("viewBox", `0 0 ${this.canvasSize.width} ${this.canvasSize.height}`);
this.svgNode.setAttribute("width", `${this.canvasSize.width}`);
this.svgNode.setAttribute("height", `${this.canvasSize.height}`);
}
/**
* Creates a 'shallow' (non deep) copy of this instance. This implies
* that under the hood the same gl context and gl program will be used.
*/
copyInstance(fillShapes: boolean): drawutilssvg {
var copy: drawutilssvg = new drawutilssvg(
this.svgNode,
this.offset,
this.scale,
this.canvasSize,
fillShapes,
null as any as DrawConfig, // no DrawConfig – this will work as long as `isSecondary===true`
true, // isSecondary
this.gNode,
this.bufferGNode,
this.nodeDefs,
this.bufferedNodeDefs
);
return copy;
}
/**
* Set the current drawlib configuration.
*
* @name setConfiguration
* @method
* @param {DrawLibConfiguration} configuration - The new configuration settings to use for the next render methods.
*/
setConfiguration(configuration: DrawLibConfiguration): void {
this.drawlibConfiguration = configuration;
}
// /**
// * Set or clear the line-dash configuration. Pass `null` for un-dashed lines.
// *
// * See https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/stroke-dasharray
// * and https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/setLineDash
// * for how line dashes work.
// *
// * @method
// * @param {Array<number> lineDashes - The line-dash array configuration.
// * @returns {void}
// */
// setLineDash(lineDashes: Array<number>) {
// this.lineDash = lineDashes;
// }
/**
* This method shouled be called each time the currently drawn `Drawable` changes.
* It is used by some libraries for identifying elemente on re-renders.
*
* @name setCurrentId
* @method
* @param {UID|null} uid - A UID identifying the currently drawn element(s).
* @instance
* @memberof drawutilssvg
**/
setCurrentId(uid: UID | null): void {
this.curId = uid;
}
/**
* This method shouled be called each time the currently drawn `Drawable` changes.
* Determine the class name for further usage here.
*
* @name setCurrentClassName
* @method
* @param {string|null} className - A class name for further custom use cases.
* @instance
* @memberof drawutilssvg
**/
setCurrentClassName(className: string | null): void {
this.curClassName = className;
}
/**
* Called before each draw cycle.
* This is required for compatibility with other draw classes in the library.
*
* @name beginDrawCycle
* @method
* @param {UID=} uid - (optional) A UID identifying the currently drawn element(s).
* @instance
* @memberof drawutilssvg
**/
beginDrawCycle(renderTime: number) {
// Clear non-recycable elements from last draw cycle.
this.cache.clear();
// Clearing an SVG is equivalent to removing all its child elements.
for (var i = 0; i < this.bufferGNode.childNodes.length; i++) {
// Hide all nodes here. Don't throw them away.
// We can probably re-use them in the next draw cycle.
var child: SVGElement = this.bufferGNode.childNodes[i] as SVGElement;
this.cache.set(child.getAttribute("id") as string, child);
}
this.removeAllChildNodes();
}
/**
* Called after each draw cycle.
*
* This is required for compatibility with other draw classes in the library (like drawgl).
*
* @name endDrawCycle
* @method
* @param {number} renderTime
* @instance
**/
endDrawCycle(renderTime: number) {
if (!this.isSecondary) {
// All elements are drawn into the buffer; they are NOT yet visible, not did the browser perform any
// layout updates.
// Replace the old <g>-node with the buffer node.
// https://stackoverflow.com/questions/27442464/how-to-update-a-svg-image-without-seeing-a-blinking
this.svgNode.replaceChild(this.bufferedNodeDefs, this.nodeDefs);
this.svgNode.replaceChild(this.bufferGNode, this.gNode);
}
const tmpGNode: SVGGElement = this.gNode;
this.gNode = this.bufferGNode;
this.bufferGNode = tmpGNode;
const tmpDefsNode: SVGDefsElement = this.nodeDefs;
this.nodeDefs = this.bufferedNodeDefs;
this.bufferedNodeDefs = tmpDefsNode;
}
/**
* A private helper method to apply stroke options to the current
* context.
* @param {StrokeOptions=} strokeOptions -
*/
private applyStrokeOpts(node: SVGElement, strokeOptions?: StrokeOptions) {
if (
strokeOptions &&
strokeOptions.dashArray &&
strokeOptions.dashArray.length > 0 &&
drawutilssvg.nodeSupportsLineDash(node.tagName)
) {
node.setAttribute(
"stroke-dasharray",
strokeOptions.dashArray
.map((dashArayElem: number) => {
return dashArayElem * this.scale.x;
})
.join(" ")
);
if (strokeOptions.dashOffset) {
node.setAttribute("stroke-dashoffset", `${strokeOptions.dashOffset * this.scale.x}`);
}
}
}
private _x(x: number): number {
return this.offset.x + this.scale.x * x;
}
private _y(y: number): number {
return this.offset.y + this.scale.y * y;
}
/**
* Draw the line between the given two points with the specified (CSS-) color.
*
* @method line
* @param {XYCoords} zA - The start point of the line.
* @param {XYCoords} zB - The end point of the line.
* @param {string} color - Any valid CSS color string.
* @param {number=1} lineWidth? - [optional] The line's width.
* @param {StrokeOptions=} strokeOptions - (optional) Stroke settings to use.
*
* @return {void}
* @instance
* @memberof drawutilssvg
**/
line(zA: XYCoords, zB: XYCoords, color: string, lineWidth?: number, strokeOptions?: StrokeOptions): SVGElement {
// const line: SVGElement = this.makeNode("line");
// this.applyStrokeOpts(line, strokeOptions);
// line.setAttribute("x1", `${this._x(zA.x)}`);
// line.setAttribute("y1", `${this._y(zA.y)}`);
// line.setAttribute("x2", `${this._x(zB.x)}`);
// line.setAttribute("y2", `${this._y(zB.y)}`);
const line = this.makeLineNode(zA, zB, color, lineWidth, strokeOptions);
return this._bindFillDraw(line, "line", color, lineWidth || 1, strokeOptions);
}
/**
* Draw a line and an arrow at the end (zB) of the given line with the specified (CSS-) color.
*
* @method arrow
* @param {XYCoords} zA - The start point of the arrow-line.
* @param {XYCoords} zB - The end point of the arrow-line.
* @param {string} color - Any valid CSS color string.
* @param {number=} lineWidth - (optional) The line width to use; default is 1.
* @param {headLength=8} headLength - (optional) The length of the arrow head (default is 8 units).
* @param {StrokeOptions=} strokeOptions - (optional) Stroke settings to use.
*
* @return {void}
* @instance
* @memberof drawutilssvg
**/
arrow(
zA: XYCoords,
zB: XYCoords,
color: string,
lineWidth?: number,
headLength: number = 8,
strokeOptions?: StrokeOptions
): SVGElement {
const group: SVGElement = this.makeNode("g");
const arrowHeadBasePosition: XYCoords = { x: 0, y: 0 };
// Just create the child nodes, don't bind them to the root node.
const arrowHead: SVGElement = this.makeArrowHeadNode(zA, zB, color, lineWidth, headLength, undefined, arrowHeadBasePosition);
const line: SVGElement = this.makeLineNode(zA, arrowHeadBasePosition, color, lineWidth, strokeOptions);
group.appendChild(line);
group.appendChild(arrowHead);
this._addCSSClasses(group, "linear-arrow");
this._bindNode(group, undefined);
return group;
}
/**
* Draw a cubic Bézier curve and and an arrow at the end (endControlPoint) of the given line width the specified (CSS-) color and arrow size.
*
* @method cubicBezierArrow
* @param {XYCoords} startPoint - The start point of the cubic Bézier curve
* @param {XYCoords} endPoint - The end point the cubic Bézier curve.
* @param {XYCoords} startControlPoint - The start control point the cubic Bézier curve.
* @param {XYCoords} endControlPoint - The end control point the cubic Bézier curve.
* @param {string} color - The CSS color to draw the curve with.
* @param {number} lineWidth - (optional) The line width to use.
* @param {headLength=8} headLength - (optional) The length of the arrow head (default is 8 units).
* @param {StrokeOptions=} strokeOptions - (optional) Stroke settings to use.
*
* @return {void}
* @instance
* @memberof DrawLib
*/
cubicBezierArrow(
startPoint: XYCoords,
endPoint: XYCoords,
startControlPoint: XYCoords,
endControlPoint: XYCoords,
color: string,
lineWidth?: number,
headLength: number = 8,
strokeOptions?: StrokeOptions
): SVGElement {
const group: SVGElement = this.makeNode("g");
// Just create the child nodes, don't bind them to the root node.
const arrowHeadBasePosition = new Vertex(0, 0);
const arrowHead: SVGElement = this.makeArrowHeadNode(
endControlPoint,
endPoint,
color,
lineWidth,
headLength,
undefined,
arrowHeadBasePosition
);
const diff = arrowHeadBasePosition.difference(endPoint);
const bezier: SVGElement = this.makeCubicBezierNode(
startPoint,
{ x: endPoint.x - diff.x, y: endPoint.y - diff.y },
startControlPoint,
{ x: endControlPoint.x - diff.x, y: endControlPoint.y - diff.y },
color,
lineWidth,
strokeOptions
);
group.appendChild(bezier);
group.appendChild(arrowHead);
this._addCSSClasses(group, "cubicbezier-arrow");
this._bindNode(group, undefined);
return group;
}
/**
* Draw just an arrow head a the end of an imaginary line (zB) of the given line width the specified (CSS-) color and size.
*
* @method arrow
* @param {XYCoords} zA - The start point of the arrow-line.
* @param {XYCoords} zB - The end point of the arrow-line.
* @param {string} color - Any valid CSS color string.
* @param {number=1} lineWidth - (optional) The line width to use; default is 1.
* @param {number=8} headLength - (optional) The length of the arrow head (default is 8 pixels).
* @param {StrokeOptions=} strokeOptions - (optional) Stroke settings to use.
*
* @return {void}
* @instance
* @memberof DrawLib
**/
arrowHead(
zA: XYCoords,
zB: XYCoords,
color: string,
lineWidth?: number,
headLength: number = 8,
strokeOptions?: StrokeOptions
): SVGElement {
const node: SVGElement = this.makeArrowHeadNode(zA, zB, color, lineWidth, headLength, strokeOptions);
return this._bindFillDraw(node, "arrowhead", color, lineWidth || 1, strokeOptions);
}
/**
* Draw an image at the given position with the given size.<br>
* <br>
* Note: SVG images may have resizing issues at the moment.Draw a line and an arrow at the end (zB) of the given line with the specified (CSS-) color.
*
* @method image
* @param {Image} image - The image object to draw.
* @param {XYCoords} position - The position to draw the the upper left corner at.
* @param {XYCoords} size - The x/y-size to draw the image with.
* @param {number=0.0} alpha - (optional, default=0.0) The transparency (1.0=opaque, 0.0=transparent).
* @return {void}
* @instance
* @memberof drawutilssvg
**/
image(image: HTMLImageElement, position: XYCoords, size: XYCoords, alpha: number = 1.0) {
const node: SVGElement = this.makeNode("image");
// We need to re-adjust the image if it was not yet fully loaded before.
const setImageSize = (image: HTMLImageElement) => {
if (image.naturalWidth) {
const ratioX = size.x / image.naturalWidth;
const ratioY = size.y / image.naturalHeight;
node.setAttribute("width", `${image.naturalWidth * this.scale.x}`);
node.setAttribute("height", `${image.naturalHeight * this.scale.y}`);
node.setAttribute("display", null as any as string); // Dislay when loaded
// if (alpha) {
node.setAttribute("opacity", `${alpha}`);
// }
node.setAttribute("transform", `translate(${this._x(position.x)} ${this._y(position.y)}) scale(${ratioX} ${ratioY})`);
}
};
image.addEventListener("load", event => {
setImageSize(image);
});
// Safari has a transform-origin bug.
// Use x=0, y=0 and translate/scale instead (see above)
node.setAttribute("x", `${0}`);
node.setAttribute("y", `${0}`);
node.setAttribute("display", "none"); // Hide before loaded
setImageSize(image);
node.setAttribute("href", image.src);
return this._bindFillDraw(node, "image", null, null);
}
/**
* Draw an image at the given position with the given size.<br>
* <br>
* Note: SVG images may have resizing issues at the moment.Draw a line and an arrow at the end (zB) of the given line with the specified (CSS-) color.
*
* @method texturedPoly
* @param {Image} textureImage - The image object to draw.
* @param {Bounds} textureSize - The texture size to use; these are the original bounds to map the polygon vertices to.
* @param {Polygon} polygon - The polygon to use as clip path.
* @param {XYCoords} polygonPosition - The polygon's position (relative), measured at the bounding box's center.
* @param {number} rotation - The rotation to use for the polygon (and for the texture).
* @return {void}
* @instance
* @memberof drawutilssvg
**/
texturedPoly(
textureImage: HTMLImageElement,
textureSize: Bounds,
polygon: Polygon,
polygonPosition: XYCoords,
rotation: number
): SVGElement {
// const basePolygonBounds: Bounds = polygon.getBounds();
const rotatedScalingOrigin = new Vertex(textureSize.min).clone().rotate(rotation, polygonPosition);
// const rotationCenter = polygonPosition.clone().add(rotatedScalingOrigin.difference(textureSize.min).inv());
// Create something like this
// ...
// <defs>
// <clipPath id="shape">
// <path fill="none" d="..."/>
// </clipPath>
// </defs>
// ...
// <g clip-path="url(#shape)">
// <g transform="scale(...)">
// <image width="643" height="643" transform="rotate(...)" xlink:href="https://s3-us-west-2.amazonaws.com/s.cdpn.io/222579/beagle400.jpg" >
// </g>
// </g>
// </image>
// ...
const clipPathNode: SVGClipPathElement = this.makeNode("clipPath") as SVGClipPathElement;
const clipPathId: string = `clippath_${UIDGenerator.next()}`; // TODO: use a better UUID generator here?
clipPathNode.setAttribute("id", clipPathId);
const gNode = this.makeNode("g") as SVGGElement;
const imageNode: SVGImageElement = this.makeNode("image") as SVGImageElement;
imageNode.setAttribute("x", `${this._x(rotatedScalingOrigin.x)}`);
imageNode.setAttribute("y", `${this._y(rotatedScalingOrigin.y)}`);
imageNode.setAttribute("width", `${textureSize.width}`);
imageNode.setAttribute("height", `${textureSize.height}`);
imageNode.setAttribute("href", textureImage.src);
// imageNode.setAttribute("opacity", "0.5");
// SVG rotations in degrees
imageNode.setAttribute(
"transform",
`rotate(${rotation * RAD_TO_DEG}, ${this._x(rotatedScalingOrigin.x)}, ${this._y(rotatedScalingOrigin.y)})`
);
const pathNode: SVGPathElement = this.makeNode("path") as SVGPathElement;
const pathData: string[] = [];
if (polygon.vertices.length > 0) {
const self = this;
pathData.push("M", `${this._x(polygon.vertices[0].x)}`, `${this._y(polygon.vertices[0].y)}`);
for (var i = 1; i < polygon.vertices.length; i++) {
pathData.push("L", `${this._x(polygon.vertices[i].x)}`, `${this._y(polygon.vertices[i].y)}`);
}
}
pathNode.setAttribute("d", pathData.join(" "));
clipPathNode.appendChild(pathNode);
this.bufferedNodeDefs.appendChild(clipPathNode);
gNode.appendChild(imageNode);
gNode.setAttribute("transform-origin", `${this._x(rotatedScalingOrigin.x)} ${this._y(rotatedScalingOrigin.y)}`);
gNode.setAttribute("transform", `scale(${this.scale.x}, ${this.scale.y})`);
const clipNode: SVGGElement = this.makeNode("g") as SVGGElement;
clipNode.appendChild(gNode);
clipNode.setAttribute("clip-path", `url(#${clipPathId})`);
// TODO: check if the image class is correct here or if we should use a 'clippedImage' class here
this._bindFillDraw(clipNode, "image", null, null); // No color, no lineWidth
return clipNode;
}
/**
* Draw the given (cubic) bézier curve.
*
* @method cubicBezier
* @param {XYCoords} startPoint - The start point of the cubic Bézier curve
* @param {XYCoords} endPoint - The end point the cubic Bézier curve.
* @param {XYCoords} startControlPoint - The start control point the cubic Bézier curve.
* @param {XYCoords} endControlPoint - The end control point the cubic Bézier curve.
* @param {string} color - The CSS color to draw the curve with.
* @param {number} lineWidth - (optional) The line width to use.
* @param {StrokeOptions=} strokeOptions - (optional) Stroke settings to use.
*
* @return {void}
* @instance
* @memberof drawutilssvg
*/
cubicBezier(
startPoint: XYCoords,
endPoint: XYCoords,
startControlPoint: XYCoords,
endControlPoint: XYCoords,
color: string,
lineWidth?: number,
strokeOptions?: StrokeOptions
): SVGElement {
const node: SVGElement = this.makeCubicBezierNode(
startPoint,
endPoint,
startControlPoint,
endControlPoint,
color,
lineWidth,
strokeOptions
);
return this._bindNode(node, undefined);
}
/**
* Draw the given (cubic) Bézier path.
*
* The given path must be an array with n*3+1 vertices, where n is the number of
* curves in the path:
* <pre> [ point1, point1_startControl, point2_endControl, point2, point2_startControl, point3_endControl, point3, ... pointN_endControl, pointN ]</pre>
*
* @method cubicBezierPath
* @param {XYCoords[]} path - The cubic bezier path as described above.
* @param {string} color - The CSS colot to draw the path with.
* @param {number=1} lineWidth - (optional) The line width to use.
* @param {StrokeOptions=} strokeOptions - (optional) Stroke settings to use.
*
* @return {void}
* @instance
* @memberof drawutilssvg
*/
cubicBezierPath(path: Array<XYCoords>, color: string, lineWidth?: number, strokeOptions?: StrokeOptions): SVGElement {
const node: SVGElement = this.makeNode("path");
this.applyStrokeOpts(node, strokeOptions);
if (!path || path.length == 0) {
return node;
}
// Draw curve
const d: Array<string | number> = ["M", this._x(path[0].x), this._y(path[0].y)];
// Draw curve path
var endPoint: XYCoords;
var startControlPoint: XYCoords;
var endControlPoint: XYCoords;
for (var i = 1; i < path.length; i += 3) {
startControlPoint = path[i];
endControlPoint = path[i + 1];
endPoint = path[i + 2];
d.push(