-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
p5.Renderer2D.js
1341 lines (1199 loc) · 37.2 KB
/
p5.Renderer2D.js
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
import p5 from './main';
import * as constants from './constants';
import './p5.Renderer';
/**
* p5.Renderer2D
* The 2D graphics canvas renderer class.
* extends p5.Renderer
*/
const styleEmpty = 'rgba(0,0,0,0)';
// const alphaThreshold = 0.00125; // minimum visible
class Renderer2D extends p5.Renderer {
constructor(elt, pInst, isMainCanvas) {
super(elt, pInst, isMainCanvas);
this.drawingContext = this.canvas.getContext('2d');
this._pInst._setProperty('drawingContext', this.drawingContext);
}
getFilterGraphicsLayer() {
// create hidden webgl renderer if it doesn't exist
if (!this.filterGraphicsLayer) {
// the real _pInst is buried when this is a secondary p5.Graphics
const pInst =
this._pInst instanceof p5.Graphics ?
this._pInst._pInst :
this._pInst;
// create secondary layer
this.filterGraphicsLayer =
new p5.Graphics(
this.width,
this.height,
constants.WEBGL,
pInst
);
}
if (
this.filterGraphicsLayer.width !== this.width ||
this.filterGraphicsLayer.height !== this.height
) {
// Resize the graphics layer
this.filterGraphicsLayer.resizeCanvas(this.width, this.height);
}
if (
this.filterGraphicsLayer.pixelDensity() !== this._pInst.pixelDensity()
) {
this.filterGraphicsLayer.pixelDensity(this._pInst.pixelDensity());
}
return this.filterGraphicsLayer;
}
_applyDefaults() {
this._cachedFillStyle = this._cachedStrokeStyle = undefined;
this._cachedBlendMode = constants.BLEND;
this._setFill(constants._DEFAULT_FILL);
this._setStroke(constants._DEFAULT_STROKE);
this.drawingContext.lineCap = constants.ROUND;
this.drawingContext.font = 'normal 12px sans-serif';
}
resize(w, h) {
super.resize(w, h);
this.drawingContext.scale(
this._pInst._pixelDensity,
this._pInst._pixelDensity
);
}
//////////////////////////////////////////////
// COLOR | Setting
//////////////////////////////////////////////
background(...args) {
this.drawingContext.save();
this.resetMatrix();
if (args[0] instanceof p5.Image) {
if (args[1] >= 0) {
// set transparency of background
const img = args[0];
this.drawingContext.globalAlpha = args[1] / 255;
this._pInst.image(img, 0, 0, this.width, this.height);
} else {
this._pInst.image(args[0], 0, 0, this.width, this.height);
}
} else {
const curFill = this._getFill();
// create background rect
const color = this._pInst.color(...args);
//accessible Outputs
if (this._pInst._addAccsOutput()) {
this._pInst._accsBackground(color.levels);
}
const newFill = color.toString();
this._setFill(newFill);
if (this._isErasing) {
this.blendMode(this._cachedBlendMode);
}
this.drawingContext.fillRect(0, 0, this.width, this.height);
// reset fill
this._setFill(curFill);
if (this._isErasing) {
this._pInst.erase();
}
}
this.drawingContext.restore();
}
clear() {
this.drawingContext.save();
this.resetMatrix();
this.drawingContext.clearRect(0, 0, this.width, this.height);
this.drawingContext.restore();
}
fill(...args) {
const color = this._pInst.color(...args);
this._setFill(color.toString());
//accessible Outputs
if (this._pInst._addAccsOutput()) {
this._pInst._accsCanvasColors('fill', color.levels);
}
}
stroke(...args) {
const color = this._pInst.color(...args);
this._setStroke(color.toString());
//accessible Outputs
if (this._pInst._addAccsOutput()) {
this._pInst._accsCanvasColors('stroke', color.levels);
}
}
erase(opacityFill, opacityStroke) {
if (!this._isErasing) {
// cache the fill style
this._cachedFillStyle = this.drawingContext.fillStyle;
const newFill = this._pInst.color(255, opacityFill).toString();
this.drawingContext.fillStyle = newFill;
// cache the stroke style
this._cachedStrokeStyle = this.drawingContext.strokeStyle;
const newStroke = this._pInst.color(255, opacityStroke).toString();
this.drawingContext.strokeStyle = newStroke;
// cache blendMode
const tempBlendMode = this._cachedBlendMode;
this.blendMode(constants.REMOVE);
this._cachedBlendMode = tempBlendMode;
this._isErasing = true;
}
}
noErase() {
if (this._isErasing) {
this.drawingContext.fillStyle = this._cachedFillStyle;
this.drawingContext.strokeStyle = this._cachedStrokeStyle;
this.blendMode(this._cachedBlendMode);
this._isErasing = false;
}
}
beginClip(options = {}) {
super.beginClip(options);
// cache the fill style
this._cachedFillStyle = this.drawingContext.fillStyle;
const newFill = this._pInst.color(255, 0).toString();
this.drawingContext.fillStyle = newFill;
// cache the stroke style
this._cachedStrokeStyle = this.drawingContext.strokeStyle;
const newStroke = this._pInst.color(255, 0).toString();
this.drawingContext.strokeStyle = newStroke;
// cache blendMode
const tempBlendMode = this._cachedBlendMode;
this.blendMode(constants.BLEND);
this._cachedBlendMode = tempBlendMode;
// Start a new path. Everything from here on out should become part of this
// one path so that we can clip to the whole thing.
this.drawingContext.beginPath();
if (this._clipInvert) {
// Slight hack: draw a big rectangle over everything with reverse winding
// order. This is hopefully large enough to cover most things.
this.drawingContext.moveTo(
-2 * this.width,
-2 * this.height
);
this.drawingContext.lineTo(
-2 * this.width,
2 * this.height
);
this.drawingContext.lineTo(
2 * this.width,
2 * this.height
);
this.drawingContext.lineTo(
2 * this.width,
-2 * this.height
);
this.drawingContext.closePath();
}
}
endClip() {
this._doFillStrokeClose();
this.drawingContext.clip();
super.endClip();
this.drawingContext.fillStyle = this._cachedFillStyle;
this.drawingContext.strokeStyle = this._cachedStrokeStyle;
this.blendMode(this._cachedBlendMode);
}
//////////////////////////////////////////////
// IMAGE | Loading & Displaying
//////////////////////////////////////////////
image(
img,
sx,
sy,
sWidth,
sHeight,
dx,
dy,
dWidth,
dHeight
) {
let cnv;
if (img.gifProperties) {
img._animateGif(this._pInst);
}
try {
if (p5.MediaElement && img instanceof p5.MediaElement) {
img._ensureCanvas();
}
if (this._tint && img.canvas) {
cnv = this._getTintedImageCanvas(img);
}
if (!cnv) {
cnv = img.canvas || img.elt;
}
let s = 1;
if (img.width && img.width > 0) {
s = cnv.width / img.width;
}
if (this._isErasing) {
this.blendMode(this._cachedBlendMode);
}
this.drawingContext.drawImage(
cnv,
s * sx,
s * sy,
s * sWidth,
s * sHeight,
dx,
dy,
dWidth,
dHeight
);
if (this._isErasing) {
this._pInst.erase();
}
} catch (e) {
if (e.name !== 'NS_ERROR_NOT_AVAILABLE') {
throw e;
}
}
}
_getTintedImageCanvas(img) {
if (!img.canvas) {
return img;
}
if (!img.tintCanvas) {
// Once an image has been tinted, keep its tint canvas
// around so we don't need to re-incur the cost of
// creating a new one for each tint
img.tintCanvas = document.createElement('canvas');
}
// Keep the size of the tint canvas up-to-date
if (img.tintCanvas.width !== img.canvas.width) {
img.tintCanvas.width = img.canvas.width;
}
if (img.tintCanvas.height !== img.canvas.height) {
img.tintCanvas.height = img.canvas.height;
}
// Goal: multiply the r,g,b,a values of the source by
// the r,g,b,a values of the tint color
const ctx = img.tintCanvas.getContext('2d');
ctx.save();
ctx.clearRect(0, 0, img.canvas.width, img.canvas.height);
if (this._tint[0] < 255 || this._tint[1] < 255 || this._tint[2] < 255) {
// Color tint: we need to use the multiply blend mode to change the colors.
// However, the canvas implementation of this destroys the alpha channel of
// the image. To accommodate, we first get a version of the image with full
// opacity everywhere, tint using multiply, and then use the destination-in
// blend mode to restore the alpha channel again.
// Start with the original image
ctx.drawImage(img.canvas, 0, 0);
// This blend mode makes everything opaque but forces the luma to match
// the original image again
ctx.globalCompositeOperation = 'luminosity';
ctx.drawImage(img.canvas, 0, 0);
// This blend mode forces the hue and chroma to match the original image.
// After this we should have the original again, but with full opacity.
ctx.globalCompositeOperation = 'color';
ctx.drawImage(img.canvas, 0, 0);
// Apply color tint
ctx.globalCompositeOperation = 'multiply';
ctx.fillStyle = `rgb(${this._tint.slice(0, 3).join(', ')})`;
ctx.fillRect(0, 0, img.canvas.width, img.canvas.height);
// Replace the alpha channel with the original alpha * the alpha tint
ctx.globalCompositeOperation = 'destination-in';
ctx.globalAlpha = this._tint[3] / 255;
ctx.drawImage(img.canvas, 0, 0);
} else {
// If we only need to change the alpha, we can skip all the extra work!
ctx.globalAlpha = this._tint[3] / 255;
ctx.drawImage(img.canvas, 0, 0);
}
ctx.restore();
return img.tintCanvas;
}
//////////////////////////////////////////////
// IMAGE | Pixels
//////////////////////////////////////////////
blendMode(mode) {
if (mode === constants.SUBTRACT) {
console.warn('blendMode(SUBTRACT) only works in WEBGL mode.');
} else if (
mode === constants.BLEND ||
mode === constants.REMOVE ||
mode === constants.DARKEST ||
mode === constants.LIGHTEST ||
mode === constants.DIFFERENCE ||
mode === constants.MULTIPLY ||
mode === constants.EXCLUSION ||
mode === constants.SCREEN ||
mode === constants.REPLACE ||
mode === constants.OVERLAY ||
mode === constants.HARD_LIGHT ||
mode === constants.SOFT_LIGHT ||
mode === constants.DODGE ||
mode === constants.BURN ||
mode === constants.ADD
) {
this._cachedBlendMode = mode;
this.drawingContext.globalCompositeOperation = mode;
} else {
throw new Error(`Mode ${mode} not recognized.`);
}
}
blend(...args) {
const currBlend = this.drawingContext.globalCompositeOperation;
const blendMode = args[args.length - 1];
const copyArgs = Array.prototype.slice.call(args, 0, args.length - 1);
this.drawingContext.globalCompositeOperation = blendMode;
p5.prototype.copy.apply(this, copyArgs);
this.drawingContext.globalCompositeOperation = currBlend;
}
// p5.Renderer2D.prototype.get = p5.Renderer.prototype.get;
// .get() is not overridden
// x,y are canvas-relative (pre-scaled by _pixelDensity)
_getPixel(x, y) {
let imageData, index;
imageData = this.drawingContext.getImageData(x, y, 1, 1).data;
index = 0;
return [
imageData[index + 0],
imageData[index + 1],
imageData[index + 2],
imageData[index + 3]
];
}
loadPixels() {
const pixelsState = this._pixelsState; // if called by p5.Image
const pd = pixelsState._pixelDensity;
const w = this.width * pd;
const h = this.height * pd;
const imageData = this.drawingContext.getImageData(0, 0, w, h);
// @todo this should actually set pixels per object, so diff buffers can
// have diff pixel arrays.
pixelsState._setProperty('imageData', imageData);
pixelsState._setProperty('pixels', imageData.data);
}
set(x, y, imgOrCol) {
// round down to get integer numbers
x = Math.floor(x);
y = Math.floor(y);
const pixelsState = this._pixelsState;
if (imgOrCol instanceof p5.Image) {
this.drawingContext.save();
this.drawingContext.setTransform(1, 0, 0, 1, 0, 0);
this.drawingContext.scale(
pixelsState._pixelDensity,
pixelsState._pixelDensity
);
this.drawingContext.clearRect(x, y, imgOrCol.width, imgOrCol.height);
this.drawingContext.drawImage(imgOrCol.canvas, x, y);
this.drawingContext.restore();
} else {
let r = 0,
g = 0,
b = 0,
a = 0;
let idx =
4 *
(y *
pixelsState._pixelDensity *
(this.width * pixelsState._pixelDensity) +
x * pixelsState._pixelDensity);
if (!pixelsState.imageData) {
pixelsState.loadPixels();
}
if (typeof imgOrCol === 'number') {
if (idx < pixelsState.pixels.length) {
r = imgOrCol;
g = imgOrCol;
b = imgOrCol;
a = 255;
//this.updatePixels.call(this);
}
} else if (Array.isArray(imgOrCol)) {
if (imgOrCol.length < 4) {
throw new Error('pixel array must be of the form [R, G, B, A]');
}
if (idx < pixelsState.pixels.length) {
r = imgOrCol[0];
g = imgOrCol[1];
b = imgOrCol[2];
a = imgOrCol[3];
//this.updatePixels.call(this);
}
} else if (imgOrCol instanceof p5.Color) {
if (idx < pixelsState.pixels.length) {
r = imgOrCol.levels[0];
g = imgOrCol.levels[1];
b = imgOrCol.levels[2];
a = imgOrCol.levels[3];
//this.updatePixels.call(this);
}
}
// loop over pixelDensity * pixelDensity
for (let i = 0; i < pixelsState._pixelDensity; i++) {
for (let j = 0; j < pixelsState._pixelDensity; j++) {
// loop over
idx =
4 *
((y * pixelsState._pixelDensity + j) *
this.width *
pixelsState._pixelDensity +
(x * pixelsState._pixelDensity + i));
pixelsState.pixels[idx] = r;
pixelsState.pixels[idx + 1] = g;
pixelsState.pixels[idx + 2] = b;
pixelsState.pixels[idx + 3] = a;
}
}
}
}
updatePixels(x, y, w, h) {
const pixelsState = this._pixelsState;
const pd = pixelsState._pixelDensity;
if (
x === undefined &&
y === undefined &&
w === undefined &&
h === undefined
) {
x = 0;
y = 0;
w = this.width;
h = this.height;
}
x *= pd;
y *= pd;
w *= pd;
h *= pd;
if (this.gifProperties) {
this.gifProperties.frames[this.gifProperties.displayIndex].image =
pixelsState.imageData;
}
this.drawingContext.putImageData(pixelsState.imageData, 0, 0, x, y, w, h);
}
//////////////////////////////////////////////
// SHAPE | 2D Primitives
//////////////////////////////////////////////
/*
* This function requires that:
*
* 0 <= start < TWO_PI
*
* start <= stop < start + TWO_PI
*/
arc(x, y, w, h, start, stop, mode) {
const ctx = this.drawingContext;
const centerX = x + w / 2,
centerY = y + h / 2,
radiusX = w / 2,
radiusY = h / 2;
// Determines whether to add a line to the center, which should be done
// when the mode is PIE or default; as well as when the start and end
// angles do not form a full circle.
const createPieSlice = ! (
mode === constants.CHORD ||
mode === constants.OPEN ||
(stop - start) % constants.TWO_PI === 0
);
// Fill
if (this._doFill) {
if (!this._clipping) ctx.beginPath();
ctx.ellipse(centerX, centerY, radiusX, radiusY, 0, start, stop);
if (createPieSlice) ctx.lineTo(centerX, centerY);
ctx.closePath();
if (!this._clipping) ctx.fill();
}
// Stroke
if (this._doStroke) {
if (!this._clipping) ctx.beginPath();
ctx.ellipse(centerX, centerY, radiusX, radiusY, 0, start, stop);
if (mode === constants.PIE && createPieSlice) {
// In PIE mode, stroke is added to the center and back to path,
// unless the pie forms a complete ellipse (see: createPieSlice)
ctx.lineTo(centerX, centerY);
}
if (mode === constants.PIE || mode === constants.CHORD) {
// Stroke connects back to path begin for both PIE and CHORD
ctx.closePath();
}
if (!this._clipping) ctx.stroke();
}
return this;
}
ellipse(args) {
const ctx = this.drawingContext;
const doFill = this._doFill,
doStroke = this._doStroke;
const x = parseFloat(args[0]),
y = parseFloat(args[1]),
w = parseFloat(args[2]),
h = parseFloat(args[3]);
if (doFill && !doStroke) {
if (this._getFill() === styleEmpty) {
return this;
}
} else if (!doFill && doStroke) {
if (this._getStroke() === styleEmpty) {
return this;
}
}
const centerX = x + w / 2,
centerY = y + h / 2,
radiusX = w / 2,
radiusY = h / 2;
if (!this._clipping) ctx.beginPath();
ctx.ellipse(centerX, centerY, radiusX, radiusY, 0, 0, 2 * Math.PI);
ctx.closePath();
if (!this._clipping && doFill) {
ctx.fill();
}
if (!this._clipping && doStroke) {
ctx.stroke();
}
}
line(x1, y1, x2, y2) {
const ctx = this.drawingContext;
if (!this._doStroke) {
return this;
} else if (this._getStroke() === styleEmpty) {
return this;
}
if (!this._clipping) ctx.beginPath();
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.stroke();
return this;
}
point(x, y) {
const ctx = this.drawingContext;
if (!this._doStroke) {
return this;
} else if (this._getStroke() === styleEmpty) {
return this;
}
const s = this._getStroke();
const f = this._getFill();
if (!this._clipping) {
// swapping fill color to stroke and back after for correct point rendering
this._setFill(s);
}
if (!this._clipping) ctx.beginPath();
ctx.arc(x, y, ctx.lineWidth / 2, 0, constants.TWO_PI, false);
if (!this._clipping) {
ctx.fill();
this._setFill(f);
}
}
quad(x1, y1, x2, y2, x3, y3, x4, y4) {
const ctx = this.drawingContext;
const doFill = this._doFill,
doStroke = this._doStroke;
if (doFill && !doStroke) {
if (this._getFill() === styleEmpty) {
return this;
}
} else if (!doFill && doStroke) {
if (this._getStroke() === styleEmpty) {
return this;
}
}
if (!this._clipping) ctx.beginPath();
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.lineTo(x3, y3);
ctx.lineTo(x4, y4);
ctx.closePath();
if (!this._clipping && doFill) {
ctx.fill();
}
if (!this._clipping && doStroke) {
ctx.stroke();
}
return this;
}
rect(args) {
const x = args[0];
const y = args[1];
const w = args[2];
const h = args[3];
let tl = args[4];
let tr = args[5];
let br = args[6];
let bl = args[7];
const ctx = this.drawingContext;
const doFill = this._doFill,
doStroke = this._doStroke;
if (doFill && !doStroke) {
if (this._getFill() === styleEmpty) {
return this;
}
} else if (!doFill && doStroke) {
if (this._getStroke() === styleEmpty) {
return this;
}
}
if (!this._clipping) ctx.beginPath();
if (typeof tl === 'undefined') {
// No rounded corners
ctx.rect(x, y, w, h);
} else {
// At least one rounded corner
// Set defaults when not specified
if (typeof tr === 'undefined') {
tr = tl;
}
if (typeof br === 'undefined') {
br = tr;
}
if (typeof bl === 'undefined') {
bl = br;
}
// corner rounding must always be positive
const absW = Math.abs(w);
const absH = Math.abs(h);
const hw = absW / 2;
const hh = absH / 2;
// Clip radii
if (absW < 2 * tl) {
tl = hw;
}
if (absH < 2 * tl) {
tl = hh;
}
if (absW < 2 * tr) {
tr = hw;
}
if (absH < 2 * tr) {
tr = hh;
}
if (absW < 2 * br) {
br = hw;
}
if (absH < 2 * br) {
br = hh;
}
if (absW < 2 * bl) {
bl = hw;
}
if (absH < 2 * bl) {
bl = hh;
}
ctx.roundRect(x, y, w, h, [tl, tr, br, bl]);
}
if (!this._clipping && this._doFill) {
ctx.fill();
}
if (!this._clipping && this._doStroke) {
ctx.stroke();
}
return this;
}
triangle(args) {
const ctx = this.drawingContext;
const doFill = this._doFill,
doStroke = this._doStroke;
const x1 = args[0],
y1 = args[1];
const x2 = args[2],
y2 = args[3];
const x3 = args[4],
y3 = args[5];
if (doFill && !doStroke) {
if (this._getFill() === styleEmpty) {
return this;
}
} else if (!doFill && doStroke) {
if (this._getStroke() === styleEmpty) {
return this;
}
}
if (!this._clipping) ctx.beginPath();
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.lineTo(x3, y3);
ctx.closePath();
if (!this._clipping && doFill) {
ctx.fill();
}
if (!this._clipping && doStroke) {
ctx.stroke();
}
}
endShape(
mode,
vertices,
isCurve,
isBezier,
isQuadratic,
isContour,
shapeKind
) {
if (vertices.length === 0) {
return this;
}
if (!this._doStroke && !this._doFill) {
return this;
}
const closeShape = mode === constants.CLOSE;
let v;
if (closeShape && !isContour) {
vertices.push(vertices[0]);
}
let i, j;
const numVerts = vertices.length;
if (isCurve && shapeKind === null) {
if (numVerts > 3) {
const b = [],
s = 1 - this._curveTightness;
if (!this._clipping) this.drawingContext.beginPath();
this.drawingContext.moveTo(vertices[1][0], vertices[1][1]);
for (i = 1; i + 2 < numVerts; i++) {
v = vertices[i];
b[0] = [v[0], v[1]];
b[1] = [
v[0] + (s * vertices[i + 1][0] - s * vertices[i - 1][0]) / 6,
v[1] + (s * vertices[i + 1][1] - s * vertices[i - 1][1]) / 6
];
b[2] = [
vertices[i + 1][0] +
(s * vertices[i][0] - s * vertices[i + 2][0]) / 6,
vertices[i + 1][1] +
(s * vertices[i][1] - s * vertices[i + 2][1]) / 6
];
b[3] = [vertices[i + 1][0], vertices[i + 1][1]];
this.drawingContext.bezierCurveTo(
b[1][0],
b[1][1],
b[2][0],
b[2][1],
b[3][0],
b[3][1]
);
}
if (closeShape) {
this.drawingContext.lineTo(vertices[i + 1][0], vertices[i + 1][1]);
}
this._doFillStrokeClose(closeShape);
}
} else if (
isBezier &&
shapeKind === null
) {
if (!this._clipping) this.drawingContext.beginPath();
for (i = 0; i < numVerts; i++) {
if (vertices[i].isVert) {
if (vertices[i].moveTo) {
this.drawingContext.moveTo(vertices[i][0], vertices[i][1]);
} else {
this.drawingContext.lineTo(vertices[i][0], vertices[i][1]);
}
} else {
this.drawingContext.bezierCurveTo(
vertices[i][0],
vertices[i][1],
vertices[i][2],
vertices[i][3],
vertices[i][4],
vertices[i][5]
);
}
}
this._doFillStrokeClose(closeShape);
} else if (
isQuadratic &&
shapeKind === null
) {
if (!this._clipping) this.drawingContext.beginPath();
for (i = 0; i < numVerts; i++) {
if (vertices[i].isVert) {
if (vertices[i].moveTo) {
this.drawingContext.moveTo(vertices[i][0], vertices[i][1]);
} else {
this.drawingContext.lineTo(vertices[i][0], vertices[i][1]);
}
} else {
this.drawingContext.quadraticCurveTo(
vertices[i][0],
vertices[i][1],
vertices[i][2],
vertices[i][3]
);
}
}
this._doFillStrokeClose(closeShape);
} else {
if (shapeKind === constants.POINTS) {
for (i = 0; i < numVerts; i++) {
v = vertices[i];
if (this._doStroke) {
this._pInst.stroke(v[6]);
}
this._pInst.point(v[0], v[1]);
}
} else if (shapeKind === constants.LINES) {
for (i = 0; i + 1 < numVerts; i += 2) {
v = vertices[i];
if (this._doStroke) {
this._pInst.stroke(vertices[i + 1][6]);
}
this._pInst.line(v[0], v[1], vertices[i + 1][0], vertices[i + 1][1]);
}
} else if (shapeKind === constants.TRIANGLES) {
for (i = 0; i + 2 < numVerts; i += 3) {
v = vertices[i];
if (!this._clipping) this.drawingContext.beginPath();
this.drawingContext.moveTo(v[0], v[1]);
this.drawingContext.lineTo(vertices[i + 1][0], vertices[i + 1][1]);
this.drawingContext.lineTo(vertices[i + 2][0], vertices[i + 2][1]);
this.drawingContext.closePath();
if (!this._clipping && this._doFill) {
this._pInst.fill(vertices[i + 2][5]);
this.drawingContext.fill();
}
if (!this._clipping && this._doStroke) {
this._pInst.stroke(vertices[i + 2][6]);
this.drawingContext.stroke();
}
}
} else if (shapeKind === constants.TRIANGLE_STRIP) {
for (i = 0; i + 1 < numVerts; i++) {
v = vertices[i];
if (!this._clipping) this.drawingContext.beginPath();
this.drawingContext.moveTo(vertices[i + 1][0], vertices[i + 1][1]);
this.drawingContext.lineTo(v[0], v[1]);
if (!this._clipping && this._doStroke) {
this._pInst.stroke(vertices[i + 1][6]);
}
if (!this._clipping && this._doFill) {
this._pInst.fill(vertices[i + 1][5]);
}
if (i + 2 < numVerts) {
this.drawingContext.lineTo(vertices[i + 2][0], vertices[i + 2][1]);
if (!this._clipping && this._doStroke) {
this._pInst.stroke(vertices[i + 2][6]);
}
if (!this._clipping && this._doFill) {
this._pInst.fill(vertices[i + 2][5]);
}
}
this._doFillStrokeClose(closeShape);
}
} else if (shapeKind === constants.TRIANGLE_FAN) {
if (numVerts > 2) {
// For performance reasons, try to batch as many of the
// fill and stroke calls as possible.
if (!this._clipping) this.drawingContext.beginPath();
for (i = 2; i < numVerts; i++) {
v = vertices[i];
this.drawingContext.moveTo(vertices[0][0], vertices[0][1]);
this.drawingContext.lineTo(vertices[i - 1][0], vertices[i - 1][1]);
this.drawingContext.lineTo(v[0], v[1]);
this.drawingContext.lineTo(vertices[0][0], vertices[0][1]);
// If the next colour is going to be different, stroke / fill now
if (i < numVerts - 1) {
if (
(this._doFill && v[5] !== vertices[i + 1][5]) ||
(this._doStroke && v[6] !== vertices[i + 1][6])
) {
if (!this._clipping && this._doFill) {
this._pInst.fill(v[5]);
this.drawingContext.fill();
this._pInst.fill(vertices[i + 1][5]);
}
if (!this._clipping && this._doStroke) {
this._pInst.stroke(v[6]);
this.drawingContext.stroke();
this._pInst.stroke(vertices[i + 1][6]);
}
this.drawingContext.closePath();
if (!this._clipping) this.drawingContext.beginPath(); // Begin the next one
}
}
}
this._doFillStrokeClose(closeShape);
}
} else if (shapeKind === constants.QUADS) {
for (i = 0; i + 3 < numVerts; i += 4) {
v = vertices[i];
if (!this._clipping) this.drawingContext.beginPath();
this.drawingContext.moveTo(v[0], v[1]);