/
draw.ts
1396 lines (1329 loc) · 53.8 KB
/
draw.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
/**
* @author Ikaros Kappler
* @date 2018-04-22
* @modified 2018-08-16 Added the curve() function to draw cubic bézier curves.
* @modified 2018-10-23 Recognizing the offset param in the circle() function.
* @modified 2018-11-27 Added the diamondHandle() function.
* @modified 2018-11-28 Added the grid() function and the ellipse() function.
* @modified 2018-11-30 Renamed the text() function to label() as it is not scaling.
* @modified 2018-12-06 Added a test function for drawing arc in SVG style.
* @modified 2018-12-09 Added the dot(Vertex,color) function (copied from Feigenbaum-plot-script).
* @modified 2019-01-30 Added the arrow(Vertex,Vertex,color) function for drawing arrow heads.
* @modified 2019-01-30 Added the image(Image,Vertex,Vertex) function for drawing images.
* @modified 2019-04-27 Fixed a severe drawing bug in the arrow(...) function. Scaling arrows did not work properly.
* @modified 2019-04-28 Added Math.round to the dot() drawing parameters to really draw a singlt dot.
* @modified 2019-06-07 Fixed an issue in the cubicBezier() function. Paths were always closed.
* @modified 2019-10-03 Added the beginDrawCycle hook.
* @modified 2019-10-25 Polygons are no longer drawn with dashed lines (solid lines instead).
* @modified 2019-11-18 Added the polyline function.
* @modified 2019-11-22 Added a second workaround for th drawImage bug in Safari.
* @modified 2019-12-07 Added the 'lineWidth' param to the line(...) function.
* @modified 2019-12-07 Added the 'lineWidth' param to the cubicBezier(...) function.
* @modified 2019-12-11 Added the 'color' param to the label(...) function.
* @modified 2019-12-18 Added the quadraticBezier(...) function (for the sake of approximating Lissajous curves).
* @modified 2019-12-20 Added the 'lineWidth' param to the polyline(...) function.
* @modified 2020-01-09 Added the 'lineWidth' param to the ellipse(...) function.
* @modified 2020-03-25 Ported this class from vanilla-JS to Typescript.
* @modified 2020-05-05 Added the 'lineWidth' param to the circle(...) function.
* @modified 2020-05-12 Drawing any handles (square, circle, diamond) with lineWidth 1 now; this was not reset before.
* @modified 2020-06-22 Added a context.clearRect() call to the clear() function; clearing with alpha channel did not work as expected.
* @modified 2020-09-07 Added the circleArc(...) function to draw sections of circles.
* @modified 2020-10-06 Removed the .closePath() instruction from the circleArc function.
* @modified 2020-10-15 Re-added the text() function.
* @modified 2020-10-28 Added the path(Path2D) function.
* @modified 2020-12-28 Added the `singleSegment` mode (test).
* @modified 2021-01-05 Added the image-loaded/broken check.
* @modified 2021-01-24 Added the `setCurrentId` function from the `DrawLib` interface.
* @modified 2021-02-22 Added the `path` drawing function to draw SVG path data.
* @modified 2021-03-31 Added the `endDrawCycle` function from `DrawLib`.
* @modified 2021-05-31 Added the `setConfiguration` function from `DrawLib`.
* @modified 2021-11-12 Adding more parameters tot the `text()` function: fontSize, textAlign, fontFamily, lineHeight.
* @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-27 Added the `texturedPoly` function.
* @modified 2022-06-01 Tweaked the `polyline` function; lineWidth now scales with scale.x.
* @modified 2022-07-26 Adding `alpha` to the `image(...)` function.
* @modified 2022-08-23 Fixed a type issue in the `polyline` function.
* @modified 2022-08-23 Fixed a type issue in the `setConfiguration` function.
* @modified 2022-08-23 Fixed a type issue in the `path` function.
* @modified 2023-02-10 The methods `setCurrentClassName` and `setCurrentId` also accept `null` now.
* @modified 2023-09-29 Removed unused method stub for texturedPoly helper function (cleanup).
* @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-09-29 Added the `lineDashes` attribute.
* @modified 2023-09-30 Adding `strokeOptions` param to these draw function: line, arrow, cubicBezierArrow, cubicBezier, cubicBezierPath, circle, circleArc, ellipse, square, rect, polygon, polyline.
* @modified 2023-10-07 Adding the optional `arrowHeadBasePositionBuffer` param to the arrowHead(...) method.
* @version 1.13.0
**/
import { CubicBezierCurve } from "./CubicBezierCurve";
import { Polygon } from "./Polygon";
import { Vertex } from "./Vertex";
import { DrawLib, SVGPathParams, XYCoords, UID, DrawLibConfiguration, FontStyle, FontWeight, StrokeOptions } from "./interfaces";
import { drawutilssvg } from "./drawutilssvg";
import { Bounds } from "./Bounds";
import { Vector } from "./Vector";
// Todo: rename this class to Drawutils?
/**
* @classdesc A wrapper class for basic drawing operations.
*
* @requires CubicBzierCurvce
* @requires Polygon
* @requires Vertex
* @requires XYCoords
*/
export class drawutils implements DrawLib<void> {
/**
* @member {CanvasRenderingContext2D}
* @memberof drawutils
* @type {CanvasRenderingContext2D}
* @instance
*/
ctx: CanvasRenderingContext2D;
/**
* @member {Vertex}
* @memberof drawutils
* @type {Vertex}
* @instance
*/
readonly offset: Vertex;
/**
* @member {Vertex}
* @memberof drawutils
* @type {Vertex}
* @instance
*/
readonly scale: Vertex;
/**
* @member {boolean}
* @memberof drawutils
* @type {boolean}
* @instance
*/
fillShapes: boolean;
/**
* The constructor.
*
* @constructor
* @name drawutils
* @param {anvasRenderingContext2D} context - The drawing context.
* @param {boolean} fillShaped - Indicates if the constructed drawutils should fill all drawn shapes (if possible).
**/
constructor(context: CanvasRenderingContext2D, fillShapes: boolean) {
this.ctx = context;
// this.lineDash = [];
this.offset = new Vertex(0, 0);
this.scale = new Vertex(1, 1);
this.fillShapes = fillShapes;
}
/**
* A private helper method to apply stroke options to the current
* context.
* @param {StrokeOptions=} strokeOptions -
*/
private applyStrokeOpts(strokeOptions?: StrokeOptions) {
this.ctx.setLineDash(
(strokeOptions?.dashArray ?? []).map((dashArrayElem: number) => {
// Note assume scale.x === scale.y
// Invariant scale makes funny stuff anyway.
return dashArrayElem * this.scale.x;
})
);
this.ctx.lineDashOffset = (strokeOptions?.dashOffset ?? 0) * this.scale.x;
}
// +---------------------------------------------------------------------------------
// | This is the final helper function for drawing and filling stuff. 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.
// |
// | @param color A stroke/fill color to use.
// +-------------------------------
// TODO: convert this to a STATIC function.
_fillOrDraw(color: string) {
if (this.fillShapes) {
this.ctx.fillStyle = color;
this.ctx.fill();
} else {
this.ctx.strokeStyle = color;
this.ctx.stroke();
}
}
/**
* Called before each draw cycle.
* @param {UID=} uid - (optional) A UID identifying the currently drawn element(s).
**/
beginDrawCycle(renderTime: number) {
// NOOP
}
/**
* 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) {
// NOOP
}
/**
* 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.ctx.globalCompositeOperation = configuration.blendMode || "source-over";
}
// /**
// * 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(lineDash: Array<number>) {
// this.lineDash = lineDash;
// }
/**
* 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).
**/
setCurrentId(uid: UID | null): void {
// NOOP
}
/**
* 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.
**/
setCurrentClassName(className: string | null): void {
// NOOP
}
/**
* 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} lineWidth? - [optional] The line's width.
* @param {StrokeOptions=} strokeOptions - (optional) Stroke settings to use.
*
* @return {void}
* @instance
* @memberof drawutils
**/
line(zA: XYCoords, zB: XYCoords, color: string, lineWidth?: number, strokeOptions?: StrokeOptions) {
this.ctx.save();
this.ctx.beginPath();
this.applyStrokeOpts(strokeOptions);
this.ctx.moveTo(this.offset.x + zA.x * this.scale.x, this.offset.y + zA.y * this.scale.y);
this.ctx.lineTo(this.offset.x + zB.x * this.scale.x, this.offset.y + zB.y * this.scale.y);
this.ctx.strokeStyle = color;
this.ctx.lineWidth = lineWidth || 1;
this.ctx.stroke();
this.ctx.restore();
}
/**
* 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 drawutils
**/
arrow(zA: XYCoords, zB: XYCoords, color: string, lineWidth?: number, headLength: number = 8, strokeOptions?: StrokeOptions) {
const arrowHeadBasePosition = new Vertex(0, 0);
this.arrowHead(zA, zB, color, lineWidth, headLength, undefined, arrowHeadBasePosition); // Will NOT use dash configuration
this.line(zA, arrowHeadBasePosition, color, lineWidth, strokeOptions); // Will use dash configuration
}
/**
* 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,
strokeOptions?: StrokeOptions
) {
const arrowHeadBasePosition = new Vertex(0, 0);
// Will NOT use dash configuration
this.arrowHead(endControlPoint, endPoint, color, lineWidth, headLength, undefined, arrowHeadBasePosition);
const diff = arrowHeadBasePosition.difference(endPoint);
// Will use dash configuration
this.cubicBezier(
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
);
}
/**
* 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.
* @param {XYCoords=} arrowHeadBasePositionBuffer - (optional) If not null, then this position will contain the arrow head's start point (after execution). Some sort of OUT variable.
*
* @return {void}
* @instance
* @memberof DrawLib
**/
arrowHead(
zA: XYCoords,
zB: XYCoords,
color: string,
lineWidth?: number,
headLength: number = 8,
strokeOptions?: StrokeOptions,
arrowHeadBasePositionBuffer?: XYCoords
) {
// var headLength: number = 8; // length of head in pixels
this.ctx.save();
this.ctx.beginPath();
this.applyStrokeOpts(strokeOptions);
var vertices: Array<Vertex> = Vector.utils.buildArrowHead(zA, zB, headLength, this.scale.x, this.scale.y);
if (arrowHeadBasePositionBuffer) {
arrowHeadBasePositionBuffer.x = vertices[0].x / this.scale.x;
arrowHeadBasePositionBuffer.y = vertices[0].y / this.scale.y;
}
this.ctx.moveTo(this.offset.x + vertices[0].x, this.offset.y + vertices[0].y);
for (var i = 0; i < vertices.length; i++) {
this.ctx.lineTo(this.offset.x + vertices[i].x, this.offset.y + vertices[i].y);
}
this.ctx.lineTo(this.offset.x + vertices[0].x, this.offset.y + vertices[0].y);
this.ctx.lineWidth = lineWidth || 1;
this._fillOrDraw(color);
this.ctx.restore();
}
/**
* 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 drawutils
**/
image(image: HTMLImageElement, position: XYCoords, size: XYCoords, alpha: number = 1.0): void {
if (!image.complete || !image.naturalWidth) {
// Avoid drawing un-unloaded or broken images
return;
}
this.ctx.save();
this.ctx.globalAlpha = alpha;
// Note that there is a Safari bug with the 3 or 5 params variant.
// Only the 9-param varaint works.
this.ctx.drawImage(
image,
0,
0,
image.naturalWidth - 1, // There is this horrible Safari bug (fixed in newer versions)
image.naturalHeight - 1, // To avoid errors substract 1 here.
this.offset.x + position.x * this.scale.x,
this.offset.y + position.y * this.scale.y,
size.x * this.scale.x,
size.y * this.scale.y
);
this.ctx.restore();
}
/**
* 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).
* @param {XYCoords={x:0,y:0}} rotationCenter - (optional) The rotational center; default is center of bounding box.
* @return {void}
* @instance
* @memberof drawutils
**/
texturedPoly(
textureImage: HTMLImageElement,
textureSize: Bounds,
polygon: Polygon,
polygonPosition: XYCoords,
rotation: number
): void {
var basePolygonBounds = polygon.getBounds();
// var targetCenterDifference = polygonPosition.clone().difference(basePolygonBounds.getCenter());
var targetCenterDifference = new Vertex(polygonPosition.x, polygonPosition.y).difference(basePolygonBounds.getCenter());
// var tileCenter = basePolygonBounds.getCenter().sub(targetCenterDifference);
// Get the position offset of the polygon
var targetTextureSize = new Vertex(textureSize.width, textureSize.height);
// var targetTextureOffset = new Vertex(-textureSize.width / 2, -textureSize.height / 2).sub(targetCenterDifference);
var targetTextureOffset = new Vertex(textureSize.min.x, textureSize.min.y).sub(polygonPosition);
this.ctx.save();
// this.ctx.translate(this.offset.x + rotationCenter.x * this.scale.x, this.offset.y + rotationCenter.y * this.scale.y);
this.ctx.translate(this.offset.x + polygonPosition.x * this.scale.x, this.offset.y + polygonPosition.y * this.scale.y);
drawutils.helpers.clipPoly(
this.ctx,
{
x: -polygonPosition.x * this.scale.x,
y: -polygonPosition.y * this.scale.y
},
this.scale,
polygon.vertices
);
this.ctx.scale(this.scale.x, this.scale.y);
this.ctx.rotate(rotation);
this.ctx.drawImage(
textureImage,
0,
0,
textureImage.naturalWidth - 1, // There is this horrible Safari bug (fixed in newer versions)
textureImage.naturalHeight - 1, // To avoid errors substract 1 here.
targetTextureOffset.x, // * this.scale.x,
targetTextureOffset.y, // * this.scale.y,
targetTextureSize.x, // * this.scale.x,
targetTextureSize.y // * this.scale.y
);
this.ctx.restore();
}
/*
_texturedPoly(
textureImage: HTMLImageElement,
textureSize: Bounds,
polygon: Polygon,
polygonPosition: XYCoords,
rotation: number,
rotationCenter: XYCoords = { x: 0, y: 0 }
): void {
var basePolygonBounds = polygon.getBounds();
var targetCenterDifference = polygonPosition.clone().difference(basePolygonBounds.getCenter());
var rotationalOffset = rotationCenter ? polygonPosition.difference(rotationCenter) : { x: 0, y: 0 };
// var rotationalOffset = { x: 0, y: 0 };
var tileCenter = basePolygonBounds.getCenter().sub(targetCenterDifference);
// Get the position offset of the polygon
var targetTextureSize = new Vertex(textureSize.width, textureSize.height);
var targetTextureOffset = new Vertex(-textureSize.width / 2, -textureSize.height / 2).sub(targetCenterDifference);
this.ctx.save();
// this.ctx.translate(
// this.offset.x + (tileCenter.x - rotationalOffset.x * 0 + targetTextureOffset.x * 0.0) * this.scale.x,
// this.offset.y + (tileCenter.y - rotationalOffset.y * 0 + targetTextureOffset.y * 0.0) * this.scale.y
// );
this.ctx.translate(
this.offset.x + (tileCenter.x - rotationalOffset.x * 0 + targetTextureOffset.x * 0.0) * this.scale.x,
this.offset.y + (tileCenter.y - rotationalOffset.y * 0 + targetTextureOffset.y * 0.0) * this.scale.y
);
this.ctx.rotate(rotation);
drawutils.helpers.clipPoly(
this.ctx,
{
x: (-targetCenterDifference.x * 1 - tileCenter.x - rotationalOffset.x) * this.scale.x,
y: (-targetCenterDifference.y * 1 - tileCenter.y - rotationalOffset.y) * this.scale.y
},
this.scale,
polygon.vertices
);
this.ctx.drawImage(
textureImage,
0,
0,
textureImage.naturalWidth - 1, // There is this horrible Safari bug (fixed in newer versions)
textureImage.naturalHeight - 1, // To avoid errors substract 1 here.
(-polygonPosition.x + targetTextureOffset.x * 1 - rotationalOffset.x * 1) * this.scale.x,
(-polygonPosition.y + targetTextureOffset.y * 1 - rotationalOffset.y * 1) * this.scale.y,
targetTextureSize.x * this.scale.x,
targetTextureSize.y * this.scale.y
);
// const scaledTextureSize = new Bounds(
// new Vertex(
// -polygonPosition.x + targetTextureOffset.x - rotationalOffset.x,
// -polygonPosition.y + targetTextureOffset.y - rotationalOffset.y
// ).scaleXY(this.scale, rotationCenter),
// new Vertex(
// -polygonPosition.x + targetTextureOffset.x - rotationalOffset.x + targetTextureSize.x,
// -polygonPosition.y + targetTextureOffset.y - rotationalOffset.y + targetTextureSize.y
// ).scaleXY(this.scale, rotationCenter)
// );
// this.ctx.drawImage(
// textureImage,
// 0,
// 0,
// textureImage.naturalWidth - 1, // There is this horrible Safari bug (fixed in newer versions)
// textureImage.naturalHeight - 1, // To avoid errors substract 1 here.
// scaledTextureSize.min.x,
// scaledTextureSize.min.y,
// scaledTextureSize.width,
// scaledTextureSize.height
// );
this.ctx.restore();
}
*/
/**
* Draw a rectangle.
*
* @param {XYCoords} position - The upper left corner of the rectangle.
* @param {number} width - The width of the rectangle.
* @param {number} height - The height of the rectangle.
* @param {string} color - The color to use.
* @param {number=1} lineWidth - (optional) The line with to use (default is 1).
* @param {StrokeOptions=} strokeOptions - (optional) Stroke settings to use.
*
* @return {void}
* @instance
* @memberof drawutils
**/
rect(
position: XYCoords,
width: number,
height: number,
color: string,
lineWidth?: number,
strokeOptions?: StrokeOptions
): void {
this.ctx.save();
this.ctx.beginPath();
this.applyStrokeOpts(strokeOptions);
this.ctx.moveTo(this.offset.x + position.x * this.scale.x, this.offset.y + position.y * this.scale.y);
this.ctx.lineTo(this.offset.x + (position.x + width) * this.scale.x, this.offset.y + position.y * this.scale.y);
this.ctx.lineTo(this.offset.x + (position.x + width) * this.scale.x, this.offset.y + (position.y + height) * this.scale.y);
this.ctx.lineTo(this.offset.x + position.x * this.scale.x, this.offset.y + (position.y + height) * this.scale.y);
// this.ctx.lineTo( this.offset.x+position.x*this.scale.x, this.offset.y+position.y*this.scale.y );
this.ctx.closePath();
this.ctx.lineWidth = lineWidth || 1;
this._fillOrDraw(color);
this.ctx.restore();
}
/**
* 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 drawutils
*/
cubicBezier(
startPoint: XYCoords,
endPoint: XYCoords,
startControlPoint: XYCoords,
endControlPoint: XYCoords,
color: string,
lineWidth?: number,
strokeOptions?: StrokeOptions
) {
if (startPoint instanceof CubicBezierCurve) {
this.cubicBezier(
startPoint.startPoint,
startPoint.endPoint,
startPoint.startControlPoint,
startPoint.endControlPoint,
color,
lineWidth
);
return;
}
// Draw curve
this.ctx.save();
this.ctx.beginPath();
this.applyStrokeOpts(strokeOptions);
this.ctx.moveTo(this.offset.x + startPoint.x * this.scale.x, this.offset.y + startPoint.y * this.scale.y);
this.ctx.bezierCurveTo(
this.offset.x + startControlPoint.x * this.scale.x,
this.offset.y + startControlPoint.y * this.scale.y,
this.offset.x + endControlPoint.x * this.scale.x,
this.offset.y + endControlPoint.y * this.scale.y,
this.offset.x + endPoint.x * this.scale.x,
this.offset.y + endPoint.y * this.scale.y
);
//this.ctx.closePath();
this.ctx.lineWidth = lineWidth || 2;
this._fillOrDraw(color);
this.ctx.restore();
}
/**
* Draw the given (quadratic) bézier curve.
*
* @method quadraticBezier
* @param {XYCoords} startPoint - The start point of the cubic Bézier curve
* @param {XYCoords} controlPoint - The control point the cubic Bézier curve.
* @param {XYCoords} endPoint - The end control point the cubic Bézier curve.
* @param {string} color - The CSS color to draw the curve with.
* @param {number|string} lineWidth - (optional) The line width to use.
* @param {StrokeOptions=} strokeOptions - (optional) Stroke settings to use.
*
* @return {void}
* @instance
* @memberof drawutils
*/
quadraticBezier(
startPoint: XYCoords,
controlPoint: XYCoords,
endPoint: XYCoords,
color: string,
lineWidth?: number,
strokeOptions?: StrokeOptions
) {
// Draw curve
this.ctx.save();
this.ctx.beginPath();
this.applyStrokeOpts(strokeOptions);
this.ctx.moveTo(this.offset.x + startPoint.x * this.scale.x, this.offset.y + startPoint.y * this.scale.y);
this.ctx.quadraticCurveTo(
this.offset.x + controlPoint.x * this.scale.x,
this.offset.y + controlPoint.y * this.scale.y,
this.offset.x + endPoint.x * this.scale.x,
this.offset.y + endPoint.y * this.scale.y
);
this.ctx.lineWidth = lineWidth || 2;
this._fillOrDraw(color);
this.ctx.restore();
}
/**
* 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 drawutils
*/
cubicBezierPath(path: Array<XYCoords>, color: string, lineWidth?: number, strokeOptions?: StrokeOptions) {
if (!path || path.length == 0) {
return;
}
// Draw curve
this.ctx.save();
this.ctx.beginPath();
var endPoint: XYCoords;
var startControlPoint: XYCoords;
var endControlPoint: XYCoords;
this.applyStrokeOpts(strokeOptions);
this.ctx.moveTo(this.offset.x + path[0].x * this.scale.x, this.offset.y + path[0].y * this.scale.y);
for (var i = 1; i < path.length; i += 3) {
startControlPoint = path[i];
endControlPoint = path[i + 1];
endPoint = path[i + 2];
this.ctx.bezierCurveTo(
this.offset.x + startControlPoint.x * this.scale.x,
this.offset.y + startControlPoint.y * this.scale.y,
this.offset.x + endControlPoint.x * this.scale.x,
this.offset.y + endControlPoint.y * this.scale.y,
this.offset.x + endPoint.x * this.scale.x,
this.offset.y + endPoint.y * this.scale.y
);
}
this.ctx.closePath();
this.ctx.lineWidth = lineWidth || 1;
this._fillOrDraw(color);
this.ctx.restore();
}
/**
* Draw the given handle and handle point (used to draw interactive Bézier curves).
*
* The colors for this are fixed and cannot be specified.
*
* @method handle
* @param {XYCoords} startPoint - The start of the handle.
* @param {XYCoords} endPoint - The end point of the handle.
* @return {void}
* @instance
* @memberof drawutils
*/
handle(startPoint: XYCoords, endPoint: XYCoords) {
// Draw handles
// (No need to save and restore here)
this.point(startPoint, "rgb(0,32,192)");
this.square(endPoint, 5, "rgba(0,128,192,0.5)");
}
/**
* Draw a handle line (with a light grey).
*
* @method handleLine
* @param {XYCoords} startPoint - The start point to draw the handle at.
* @param {XYCoords} endPoint - The end point to draw the handle at.
* @return {void}
* @instance
* @memberof drawutils
*/
handleLine(startPoint: XYCoords, endPoint: XYCoords) {
// Draw handle lines
this.line(startPoint, endPoint, "rgba(128,128,128, 0.5)", undefined);
}
/**
* Draw a 1x1 dot with the specified (CSS-) color.
*
* @method dot
* @param {XYCoords} p - The position to draw the dot at.
* @param {string} color - The CSS color to draw the dot with.
* @return {void}
* @instance
* @memberof drawutils
*/
dot(p: XYCoords, color: string) {
this.ctx.save();
this.ctx.beginPath();
this.ctx.setLineDash([]); // Clear line-dash settings
this.ctx.moveTo(Math.round(this.offset.x + this.scale.x * p.x), Math.round(this.offset.y + this.scale.y * p.y));
this.ctx.lineTo(Math.round(this.offset.x + this.scale.x * p.x + 1), Math.round(this.offset.y + this.scale.y * p.y + 1));
this.ctx.closePath();
this.ctx.lineWidth = 1;
this._fillOrDraw(color);
this.ctx.restore();
}
/**
* Draw the given point with the specified (CSS-) color and radius 3.
*
* @method point
* @param {XYCoords} p - The position to draw the point at.
* @param {string} color - The CSS color to draw the point with.
* @return {void}
* @instance
* @memberof drawutils
*/
point(p: XYCoords, color: string) {
var radius: number = 3;
this.ctx.setLineDash([]); // Clear line-dash settings
this.ctx.beginPath();
this.ctx.arc(this.offset.x + p.x * this.scale.x, this.offset.y + p.y * this.scale.y, radius, 0, 2 * Math.PI, false);
this.ctx.closePath();
this.ctx.lineWidth = 1;
this._fillOrDraw(color);
}
/**
* Draw a circle with the specified (CSS-) color and radius.<br>
* <br>
* Note that if the x- and y- scales are different the result will be an ellipse rather than a circle.
*
* @method circle
* @param {XYCoords} center - The center of the circle.
* @param {number} radius - The radius of the circle.
* @param {string} color - The CSS color to draw the circle with.
* @param {number} lineWidth - The line width (optional, default=1).
* @param {StrokeOptions=} strokeOptions - (optional) Stroke settings to use.
*
* @return {void}
* @instance
* @memberof drawutils
*/
circle(center: XYCoords, radius: number, color: string, lineWidth?: number, strokeOptions?: StrokeOptions) {
this.applyStrokeOpts(strokeOptions);
this.ctx.beginPath();
this.ctx.ellipse(
this.offset.x + center.x * this.scale.x,
this.offset.y + center.y * this.scale.y,
radius * this.scale.x,
radius * this.scale.y,
0.0,
0.0,
Math.PI * 2
);
this.ctx.closePath();
this.ctx.lineWidth = lineWidth || 1;
this._fillOrDraw(color);
}
/**
* Draw a circular arc (section of a circle) with the given CSS color.
*
* @method circleArc
* @param {XYCoords} center - The center of the circle.
* @param {number} radius - The radius of the circle.
* @param {number} startAngle - The angle to start at.
* @param {number} endAngle - The angle to end at.
* @param {string=#000000} color - The CSS color to draw the circle with.
* @param {number=1} lineWidth - The line width to use
* @param {boolean=false} options.asSegment - If `true` then no beginPath and no draw will be applied (as part of larger path).
* @param {number=} options.dashOffset - (optional) `See StrokeOptions`.
* @param {number=[]} options.dashArray - (optional) `See StrokeOptions`.
*
* @return {void}
* @instance
* @memberof drawutils
*/
circleArc(
center: XYCoords,
radius: number,
startAngle: number,
endAngle: number,
color?: string,
lineWidth?: number,
options?: { asSegment?: boolean } & StrokeOptions
) {
if (!options || !options.asSegment) {
this.ctx.beginPath();
}
this.applyStrokeOpts(options);
this.ctx.ellipse(
this.offset.x + center.x * this.scale.x,
this.offset.y + center.y * this.scale.y,
radius * this.scale.x,
radius * this.scale.y,
0.0,
startAngle,
endAngle,
false
);
if (!options || !options.asSegment) {
// this.ctx.closePath();
this.ctx.lineWidth = lineWidth || 1;
this._fillOrDraw(color || "#000000");
}
}
/**
* Draw an ellipse with the specified (CSS-) color and thw two radii.
*
* @method ellipse
* @param {XYCoords} center - The center of the ellipse.
* @param {number} radiusX - The radius of the ellipse.
* @param {number} radiusY - The radius of the ellipse.
* @param {string} color - The CSS color to draw the ellipse with.
* @param {number} lineWidth=1 - An optional line width param (default is 1).
* @param {number=} rotation - (optional, default=0) The rotation of the ellipse.
* @param {StrokeOptions=} strokeOptions - (optional) Stroke settings to use.
*
* @return {void}
* @instance
* @memberof drawutils
*/
ellipse(
center: XYCoords,
radiusX: number,
radiusY: number,
color: string,
lineWidth?: number,
rotation?: number,
strokeOptions?: StrokeOptions
) {
if (typeof rotation === "undefined") {
rotation = 0.0;
}
this.applyStrokeOpts(strokeOptions);
this.ctx.beginPath();
this.ctx.ellipse(
this.offset.x + center.x * this.scale.x,
this.offset.y + center.y * this.scale.y,
radiusX * this.scale.x,
radiusY * this.scale.y,
rotation,
0.0,
Math.PI * 2
);
this.ctx.closePath();
this.ctx.lineWidth = lineWidth || 1;
this._fillOrDraw(color);
}
/**
* Draw square at the given center, size and with the specified (CSS-) color.<br>
* <br>
* Note that if the x-scale and the y-scale are different the result will be a rectangle rather than a square.
*
* @method square
* @param {XYCoords} center - The center of the square.
* @param {number} size - The size of the square.
* @param {string} color - The CSS color to draw the square with.
* @param {number} lineWidth - The line with to use (optional, default is 1).
* @param {StrokeOptions=} strokeOptions - (optional) Stroke settings to use.
*
* @return {void}
* @instance
* @memberof drawutils
*/
square(center: XYCoords, size: number, color: string, lineWidth?: number, strokeOptions?: StrokeOptions) {
this.applyStrokeOpts(strokeOptions);
this.ctx.beginPath();
this.ctx.rect(
this.offset.x + (center.x - size / 2.0) * this.scale.x,
this.offset.y + (center.y - size / 2.0) * this.scale.y,
size * this.scale.x,
size * this.scale.y
);
this.ctx.closePath();
this.ctx.lineWidth = lineWidth || 1;
this._fillOrDraw(color);
}
/**
* Draw a grid of horizontal and vertical lines with the given (CSS-) color.
*
* @method grid
* @param {XYCoords} center - The center of the grid.
* @param {number} width - The total width of the grid (width/2 each to the left and to the right).
* @param {number} height - The total height of the grid (height/2 each to the top and to the bottom).
* @param {number} sizeX - The horizontal grid size.
* @param {number} sizeY - The vertical grid size.
* @param {string} color - The CSS color to draw the grid with.
* @return {void}
* @instance
* @memberof drawutils
*/
grid(center: XYCoords, width: number, height: number, sizeX: number, sizeY: number, color: string) {
this.ctx.setLineDash([]); // Clear line-dash settings
this.ctx.beginPath();
var yMin: number = -Math.ceil((height * 0.5) / sizeY) * sizeY;
var yMax: number = height / 2;
for (var x = -Math.ceil((width * 0.5) / sizeX) * sizeX; x < width / 2; x += sizeX) {
this.ctx.moveTo(this.offset.x + (center.x + x) * this.scale.x, this.offset.y + (center.y + yMin) * this.scale.y);
this.ctx.lineTo(this.offset.x + (center.x + x) * this.scale.x, this.offset.y + (center.y + yMax) * this.scale.y);
}
var xMin: number = -Math.ceil((width * 0.5) / sizeX) * sizeX; // -Math.ceil((height*0.5)/sizeY)*sizeY;
var xMax: number = width / 2; // height/2;
for (var y = -Math.ceil((height * 0.5) / sizeY) * sizeY; y < height / 2; y += sizeY) {
this.ctx.moveTo(this.offset.x + (center.x + xMin) * this.scale.x - 4, this.offset.y + (center.y + y) * this.scale.y);
this.ctx.lineTo(this.offset.x + (center.x + xMax) * this.scale.x + 4, this.offset.y + (center.y + y) * this.scale.y);
}
this.ctx.strokeStyle = color;
this.ctx.lineWidth = 1.0;
this.ctx.stroke();
this.ctx.closePath();
}
/**
* Draw a raster of crosshairs in the given grid.<br>
*