-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
rendering.js
1256 lines (1240 loc) · 34.9 KB
/
rendering.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
/**
* @module Rendering
* @submodule Rendering
* @for p5
*/
import p5 from './main';
import * as constants from './constants';
import './p5.Graphics';
import './p5.Renderer2D';
import '../webgl/p5.RendererGL';
let defaultId = 'defaultCanvas0'; // this gets set again in createCanvas
const defaultClass = 'p5Canvas';
/**
* Creates a canvas element on the web page.
*
* `createCanvas()` creates the main drawing canvas for a sketch. It should
* only be called once at the beginning of <a href="#/p5/setup">setup()</a>.
* Calling `createCanvas()` more than once causes unpredictable behavior.
*
* The first two parameters, `width` and `height`, are optional. They set the
* dimensions of the canvas and the values of the
* <a href="#/p5/width">width</a> and <a href="#/p5/height">height</a> system
* variables. For example, calling `createCanvas(900, 500)` creates a canvas
* that's 900×500 pixels. By default, `width` and `height` are both 100.
*
* The third parameter is also optional. If either of the constants `P2D` or
* `WEBGL` is passed, as in `createCanvas(900, 500, WEBGL)`, then it will set
* the sketch's rendering mode. If an existing
* <a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement" target="_blank">HTMLCanvasElement</a>
* is passed, as in `createCanvas(900, 500, myCanvas)`, then it will be used
* by the sketch.
*
* The fourth parameter is also optional. If an existing
* <a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement" target="_blank">HTMLCanvasElement</a>
* is passed, as in `createCanvas(900, 500, WEBGL, myCanvas)`, then it will be
* used by the sketch.
*
* Note: In WebGL mode, the canvas will use a WebGL2 context if it's supported
* by the browser. Check the <a href="#/p5/webglVersion">webglVersion</a>
* system variable to check what version is being used, or call
* `setAttributes({ version: 1 })` to create a WebGL1 context.
*
* @method createCanvas
* @param {Number} [width] width of the canvas. Defaults to 100.
* @param {Number} [height] height of the canvas. Defaults to 100.
* @param {Constant} [renderer] either P2D or WEBGL. Defaults to `P2D`.
* @param {HTMLCanvasElement} [canvas] existing canvas element that should be used for the sketch.
* @return {p5.Renderer} new `p5.Renderer` that holds the canvas.
*
* @example
* <div>
* <code>
* function setup() {
* createCanvas(100, 100);
*
* background(180);
*
* // Draw a diagonal line.
* line(0, 0, width, height);
*
* describe('A diagonal line drawn from top-left to bottom-right on a gray background.');
* }
* </code>
* </div>
*
* <div>
* <code>
* function setup() {
* createCanvas(100, 50);
*
* background(180);
*
* // Draw a diagonal line.
* line(0, 0, width, height);
*
* describe('A diagonal line drawn from top-left to bottom-right on a gray background.');
* }
* </code>
* </div>
*
* <div>
* <code>
* // Use WebGL mode.
*
* function setup() {
* createCanvas(100, 100, WEBGL);
*
* background(180);
*
* // Draw a diagonal line.
* line(-width / 2, -height / 2, width / 2, height / 2);
*
* describe('A diagonal line drawn from top-left to bottom-right on a gray background.');
* }
* </code>
* </div>
*
* <div>
* <code>
* function setup() {
* // Create a p5.Render object.
* let cnv = createCanvas(50, 50);
*
* // Position the canvas.
* cnv.position(10, 20);
*
* background(180);
*
* // Draw a diagonal line.
* line(0, 0, width, height);
*
* describe('A diagonal line drawn from top-left to bottom-right on a gray background.');
* }
* </code>
* </div>
*/
/**
* @method createCanvas
* @param {Number} [width]
* @param {Number} [height]
* @param {HTMLCanvasElement} [canvas]
* @return {p5.Renderer}
*/
p5.prototype.createCanvas = function(w, h, renderer, canvas) {
p5._validateParameters('createCanvas', arguments);
//optional: renderer, otherwise defaults to p2d
let r;
if (arguments[2] instanceof HTMLCanvasElement) {
renderer = constants.P2D;
canvas = arguments[2];
} else {
r = renderer || constants.P2D;
}
let c;
if (canvas) {
c = document.getElementById(defaultId);
if (c) {
c.parentNode.removeChild(c); //replace the existing defaultCanvas
}
c = canvas;
this._defaultGraphicsCreated = false;
} else {
if (r === constants.WEBGL) {
c = document.getElementById(defaultId);
if (c) {
//if defaultCanvas already exists
c.parentNode.removeChild(c); //replace the existing defaultCanvas
const thisRenderer = this._renderer;
this._elements = this._elements.filter(e => e !== thisRenderer);
}
c = document.createElement('canvas');
c.id = defaultId;
c.classList.add(defaultClass);
} else {
if (!this._defaultGraphicsCreated) {
if (canvas) {
c = canvas;
} else {
c = document.createElement('canvas');
}
let i = 0;
while (document.getElementById(`defaultCanvas${i}`)) {
i++;
}
defaultId = `defaultCanvas${i}`;
c.id = defaultId;
c.classList.add(defaultClass);
} else {
// resize the default canvas if new one is created
c = this.canvas;
}
}
// set to invisible if still in setup (to prevent flashing with manipulate)
if (!this._setupDone) {
c.dataset.hidden = true; // tag to show later
c.style.visibility = 'hidden';
}
if (this._userNode) {
// user input node case
this._userNode.appendChild(c);
} else {
//create main element
if (document.getElementsByTagName('main').length === 0) {
let m = document.createElement('main');
document.body.appendChild(m);
}
//append canvas to main
document.getElementsByTagName('main')[0].appendChild(c);
}
}
// Init our graphics renderer
//webgl mode
if (r === constants.WEBGL) {
this._setProperty('_renderer', new p5.RendererGL(c, this, true));
this._elements.push(this._renderer);
const dimensions =
this._renderer._adjustDimensions(w, h);
w = dimensions.adjustedWidth;
h = dimensions.adjustedHeight;
} else {
//P2D mode
if (!this._defaultGraphicsCreated) {
this._setProperty('_renderer', new p5.Renderer2D(c, this, true));
this._defaultGraphicsCreated = true;
this._elements.push(this._renderer);
}
}
this._renderer.resize(w, h);
this._renderer._applyDefaults();
return this._renderer;
};
/**
* Resizes the canvas to a given width and height.
*
* `resizeCanvas()` immediately clears the canvas and calls
* <a href="#/p5/redraw">redraw()</a>. It's common to call `resizeCanvas()`
* within the body of <a href="#/p5/windowResized">windowResized()</a> like
* so:
*
* ```js
* function windowResized() {
* resizeCanvas(windowWidth, windowHeight);
* }
* ```
*
* The first two parameters, `width` and `height`, set the dimensions of the
* canvas. They also the values of the <a href="#/p5/width">width</a> and
* <a href="#/p5/height">height</a> system variables. For example, calling
* `resizeCanvas(300, 500)` resizes the canvas to 300×500 pixels, then sets
* <a href="#/p5/width">width</a> to 300 and
* <a href="#/p5/height">height</a> 500.
*
* The third parameter, `noRedraw`, is optional. If `true` is passed, as in
* `resizeCanvas(300, 500, true)`, then the canvas will be canvas to 300×500
* pixels but the <a href="#/p5/redraw">redraw()</a> function won't be called
* immediately. By default, <a href="#/p5/redraw">redraw()</a> is called
* immediately when `resizeCanvas()` finishes executing.
*
* @method resizeCanvas
* @param {Number} width width of the canvas.
* @param {Number} height height of the canvas.
* @param {Boolean} [noRedraw] whether to delay calling
* <a href="#/p5/redraw">redraw()</a>. Defaults
* to `false`.
*
* @example
* <div>
* <code>
* // Double-click to resize the canvas.
*
* function setup() {
* createCanvas(100, 100);
*
* describe(
* 'A white circle drawn on a gray background. The canvas shrinks by half the first time the user double-clicks.'
* );
* }
*
* function draw() {
* background(180);
*
* // Draw a circle at the center of the canvas.
* circle(width / 2, height / 2, 20);
* }
*
* // Resize the canvas when the user double-clicks.
* function doubleClicked() {
* resizeCanvas(50, 50);
* }
* </code>
* </div>
*
* <div>
* <code>
* // Resize the web browser to change the canvas size.
*
* function setup() {
* createCanvas(windowWidth, windowHeight);
*
* describe('A white circle drawn on a gray background.');
* }
*
* function draw() {
* background(180);
*
* // Draw a circle at the center of the canvas.
* circle(width / 2, height / 2, 20);
* }
*
* // Always resize the canvas to fill the browser window.
* function windowResized() {
* resizeCanvas(windowWidth, windowHeight);
* }
* </code>
* </div>
*/
p5.prototype.resizeCanvas = function(w, h, noRedraw) {
p5._validateParameters('resizeCanvas', arguments);
if (this._renderer) {
// save canvas properties
const props = {};
for (const key in this.drawingContext) {
const val = this.drawingContext[key];
if (typeof val !== 'object' && typeof val !== 'function') {
props[key] = val;
}
}
if (this._renderer instanceof p5.RendererGL) {
const dimensions =
this._renderer._adjustDimensions(w, h);
w = dimensions.adjustedWidth;
h = dimensions.adjustedHeight;
}
this.width = w;
this.height = h;
// Make sure width and height are updated before the renderer resizes so
// that framebuffers updated from the resize read the correct size
this._renderer.resize(w, h);
// reset canvas properties
for (const savedKey in props) {
try {
this.drawingContext[savedKey] = props[savedKey];
} catch (err) {
// ignore read-only property errors
}
}
if (!noRedraw) {
this.redraw();
}
}
//accessible Outputs
if (this._addAccsOutput()) {
this._updateAccsOutput();
}
};
/**
* Removes the default canvas.
*
* By default, a 100×100 pixels canvas is created without needing to call
* <a href="#/p5/createCanvas">createCanvas()</a>. `noCanvas()` removes the
* default canvas for sketches that don't need it.
*
* @method noCanvas
*
* @example
* <div>
* <code>
* function setup() {
* noCanvas();
* }
* </code>
* </div>
*/
p5.prototype.noCanvas = function() {
if (this.canvas) {
this.canvas.parentNode.removeChild(this.canvas);
}
};
/**
* Creates a <a href="#/p5.Graphics">p5.Graphics</a> object.
*
* `createGraphics()` creates an offscreen drawing canvas (graphics buffer)
* and returns it as a <a href="#/p5.Graphics">p5.Graphics</a> object. Drawing
* to a separate graphics buffer can be helpful for performance and for
* organizing code.
*
* The first two parameters, `width` and `height`, are optional. They set the
* dimensions of the <a href="#/p5.Graphics">p5.Graphics</a> object. For
* example, calling `createGraphics(900, 500)` creates a graphics buffer
* that's 900×500 pixels.
*
* The third parameter is also optional. If either of the constants `P2D` or
* `WEBGL` is passed, as in `createGraphics(900, 500, WEBGL)`, then it will set
* the <a href="#/p5.Graphics">p5.Graphics</a> object's rendering mode. If an
* existing
* <a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement" target="_blank">HTMLCanvasElement</a>
* is passed, as in `createGraphics(900, 500, myCanvas)`, then it will be used
* by the graphics buffer.
*
* The fourth parameter is also optional. If an existing
* <a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement" target="_blank">HTMLCanvasElement</a>
* is passed, as in `createGraphics(900, 500, WEBGL, myCanvas)`, then it will be
* used by the graphics buffer.
*
* Note: In WebGL mode, the <a href="#/p5.Graphics">p5.Graphics</a> object
* will use a WebGL2 context if it's supported by the browser. Check the
* <a href="#/p5/webglVersion">webglVersion</a> system variable to check what
* version is being used, or call `setAttributes({ version: 1 })` to create a
* WebGL1 context.
*
* @method createGraphics
* @param {Number} width width of the graphics buffer.
* @param {Number} height height of the graphics buffer.
* @param {Constant} [renderer] either P2D or WEBGL. Defaults to P2D.
* @param {HTMLCanvasElement} [canvas] existing canvas element that should be
* used for the graphics buffer..
* @return {p5.Graphics} new graphics buffer.
*
* @example
* <div>
* <code>
* // Double-click to draw the contents of the graphics buffer.
*
* let pg;
*
* function setup() {
* createCanvas(100, 100);
*
* background(180);
*
* // Create the p5.Graphics object.
* pg = createGraphics(50, 50);
*
* // Draw to the graphics buffer.
* pg.background(100);
* pg.circle(pg.width / 2, pg.height / 2, 20);
*
* describe('A gray square. A smaller, darker square with a white circle at its center appears when the user double-clicks.');
* }
*
* // Display the graphics buffer when the user double-clicks.
* function doubleClicked() {
* if (mouseX > 0 && mouseX < 100 && mouseY > 0 && mouseY < 100) {
* image(pg, 25, 25);
* }
* }
* </code>
* </div>
*
* <div>
* <code>
* // Double-click to draw the contents of the graphics buffer.
*
* let pg;
*
* function setup() {
* createCanvas(100, 100);
*
* background(180);
*
* // Create the p5.Graphics object in WebGL mode.
* pg = createGraphics(50, 50, WEBGL);
*
* // Draw to the graphics buffer.
* pg.background(100);
* pg.lights();
* pg.noStroke();
* pg.rotateX(QUARTER_PI);
* pg.rotateY(QUARTER_PI);
* pg.torus(15, 5);
*
* describe('A gray square. A smaller, darker square with a white torus at its center appears when the user double-clicks.');
* }
*
* // Display the graphics buffer when the user double-clicks.
* function doubleClicked() {
* if (mouseX > 0 && mouseX < 100 && mouseY > 0 && mouseY < 100) {
* image(pg, 25, 25);
* }
* }
* </code>
* </div>
*/
/**
* @method createGraphics
* @param {Number} width
* @param {Number} height
* @param {HTMLCanvasElement} [canvas]
* @return {p5.Graphics}
*/
p5.prototype.createGraphics = function(w, h, ...args) {
/**
* args[0] is expected to be renderer
* args[1] is expected to be canvas
*/
if (args[0] instanceof HTMLCanvasElement) {
args[1] = args[0];
args[0] = constants.P2D;
}
p5._validateParameters('createGraphics', arguments);
return new p5.Graphics(w, h, args[0], this, args[1]);
};
/**
* Creates and a new <a href="#/p5.Framebuffer">p5.Framebuffer</a> object.
*
* <a href="#/p5.Framebuffer">p5.Framebuffer</a> objects are separate drawing
* surfaces that can be used as textures in WebGL mode. They're similar to
* <a href="#/p5.Graphics">p5.Graphics</a> objects and generally run much
* faster when used as textures.
*
* The parameter, `options`, is optional. An object can be passed to configure
* the <a href="#/p5.Framebuffer">p5.Framebuffer</a> object. The available
* properties are:
*
* - `format`: data format of the texture, either `UNSIGNED_BYTE`, `FLOAT`, or `HALF_FLOAT`. Default is `UNSIGNED_BYTE`.
* - `channels`: whether to store `RGB` or `RGBA` color channels. Default is to match the main canvas which is `RGBA`.
* - `depth`: whether to include a depth buffer. Default is `true`.
* - `depthFormat`: data format of depth information, either `UNSIGNED_INT` or `FLOAT`. Default is `FLOAT`.
* - `stencil`: whether to include a stencil buffer for masking. `depth` must be `true` for this feature to work. Defaults to the value of `depth` which is `true`.
* - `antialias`: whether to perform anti-aliasing. If set to `true`, as in `{ antialias: true }`, 2 samples will be used by default. The number of samples can also be set, as in `{ antialias: 4 }`. Default is to match <a href="#/p5/setAttributes">setAttributes()</a> which is `false` (`true` in Safari).
* - `width`: width of the <a href="#/p5.Framebuffer">p5.Framebuffer</a> object. Default is to always match the main canvas width.
* - `height`: height of the <a href="#/p5.Framebuffer">p5.Framebuffer</a> object. Default is to always match the main canvas height.
* - `density`: pixel density of the <a href="#/p5.Framebuffer">p5.Framebuffer</a> object. Default is to always match the main canvas pixel density.
* - `textureFiltering`: how to read values from the <a href="#/p5.Framebuffer">p5.Framebuffer</a> object. Either `LINEAR` (nearby pixels will be interpolated) or `NEAREST` (no interpolation). Generally, use `LINEAR` when using the texture as an image and `NEAREST` if reading the texture as data. Default is `LINEAR`.
*
* If the `width`, `height`, or `density` attributes are set, they won't automatically match the main canvas and must be changed manually.
*
* Note: `createFramebuffer()` can only be used in WebGL mode.
*
* @method createFramebuffer
* @param {Object} [options] configuration options.
* @return {p5.Framebuffer} new framebuffer.
*
* @example
* <div>
* <code>
* let myBuffer;
*
* function setup() {
* createCanvas(100, 100, WEBGL);
*
* // Create a p5.Framebuffer object.
* myBuffer = createFramebuffer();
*
* describe('A grid of white toruses rotating against a dark gray background.');
* }
*
* function draw() {
* background(50);
*
* // Start drawing to the p5.Framebuffer object.
* myBuffer.begin();
*
* // Clear the drawing surface.
* clear();
*
* // Turn on the lights.
* lights();
*
* // Rotate the coordinate system.
* rotateX(frameCount * 0.01);
* rotateY(frameCount * 0.01);
*
* // Style the torus.
* noStroke();
*
* // Draw the torus.
* torus(20);
*
* // Stop drawing to the p5.Framebuffer object.
* myBuffer.end();
*
* // Iterate from left to right.
* for (let x = -50; x < 50; x += 25) {
* // Iterate from top to bottom.
* for (let y = -50; y < 50; y += 25) {
* // Draw the p5.Framebuffer object to the canvas.
* image(myBuffer, x, y, 25, 25);
* }
* }
* }
* </code>
* </div>
*
* <div>
* <code>
* let myBuffer;
*
* function setup() {
* createCanvas(100, 100, WEBGL);
*
* // Create an options object.
* let options = { width: 25, height: 25 };
*
* // Create a p5.Framebuffer object.
* // Use options for configuration.
* myBuffer = createFramebuffer(options);
*
* describe('A grid of white toruses rotating against a dark gray background.');
* }
*
* function draw() {
* background(50);
*
* // Start drawing to the p5.Framebuffer object.
* myBuffer.begin();
*
* // Clear the drawing surface.
* clear();
*
* // Turn on the lights.
* lights();
*
* // Rotate the coordinate system.
* rotateX(frameCount * 0.01);
* rotateY(frameCount * 0.01);
*
* // Style the torus.
* noStroke();
*
* // Draw the torus.
* torus(5, 2.5);
*
* // Stop drawing to the p5.Framebuffer object.
* myBuffer.end();
*
* // Iterate from left to right.
* for (let x = -50; x < 50; x += 25) {
* // Iterate from top to bottom.
* for (let y = -50; y < 50; y += 25) {
* // Draw the p5.Framebuffer object to the canvas.
* image(myBuffer, x, y);
* }
* }
* }
* </code>
* </div>
*/
p5.prototype.createFramebuffer = function(options) {
return new p5.Framebuffer(this, options);
};
/**
* Clears the depth buffer in WebGL mode.
*
* `clearDepth()` clears information about how far objects are from the camera
* in 3D space. This information is stored in an object called the
* *depth buffer*. Clearing the depth buffer ensures new objects aren't drawn
* behind old ones. Doing so can be useful for feedback effects in which the
* previous frame serves as the background for the current frame.
*
* The parameter, `depth`, is optional. If a number is passed, as in
* `clearDepth(0.5)`, it determines the range of objects to clear from the
* depth buffer. 0 doesn't clear any depth information, 0.5 clears depth
* information halfway between the near and far clipping planes, and 1 clears
* depth information all the way to the far clipping plane. By default,
* `depth` is 1.
*
* Note: `clearDepth()` can only be used in WebGL mode.
*
* @method clearDepth
* @param {Number} [depth] amount of the depth buffer to clear between 0
* (none) and 1 (far clipping plane). Defaults to 1.
*
* @example
* <div>
* <code>
* let previous;
* let current;
*
* function setup() {
* createCanvas(100, 100, WEBGL);
*
* // Create the p5.Framebuffer objects.
* previous = createFramebuffer({ format: FLOAT });
* current = createFramebuffer({ format: FLOAT });
*
* describe(
* 'A multicolor box drifts from side to side on a white background. It leaves a trail that fades over time.'
* );
* }
*
* function draw() {
* // Swap the previous p5.Framebuffer and the
* // current one so it can be used as a texture.
* [previous, current] = [current, previous];
*
* // Start drawing to the current p5.Framebuffer.
* current.begin();
*
* // Paint the background.
* background(255);
*
* // Draw the previous p5.Framebuffer.
* // Clear the depth buffer so the previous
* // frame doesn't block the current one.
* push();
* tint(255, 250);
* image(previous, -50, -50);
* clearDepth();
* pop();
*
* // Draw the box on top of the previous frame.
* push();
* let x = 25 * sin(frameCount * 0.01);
* let y = 25 * sin(frameCount * 0.02);
* translate(x, y, 0);
* rotateX(frameCount * 0.01);
* rotateY(frameCount * 0.01);
* normalMaterial();
* box(12);
* pop();
*
* // Stop drawing to the current p5.Framebuffer.
* current.end();
*
* // Display the current p5.Framebuffer.
* image(current, -50, -50);
* }
* </code>
* </div>
*/
p5.prototype.clearDepth = function(depth) {
this._assert3d('clearDepth');
this._renderer.clearDepth(depth);
};
/**
* Sets the way colors blend when added to the canvas.
*
* By default, drawing with a solid color paints over the current pixel values
* on the canvas. `blendMode()` offers many options for blending colors.
*
* Shapes, images, and text can be used as sources for drawing to the canvas.
* A source pixel changes the color of the canvas pixel where it's drawn. The
* final color results from blending the source pixel's color with the canvas
* pixel's color. RGB color values from the source and canvas pixels are
* compared, added, subtracted, multiplied, and divided to create different
* effects. Red values with red values, greens with greens, and blues with
* blues.
*
* The parameter, `mode`, sets the blend mode. For example, calling
* `blendMode(ADD)` sets the blend mode to `ADD`. The following blend modes
* are available in both 2D and WebGL mode:
*
* - `BLEND`: color values from the source overwrite the canvas. This is the default mode.
* - `ADD`: color values from the source are added to values from the canvas.
* - `DARKEST`: keeps the darkest color value.
* - `LIGHTEST`: keeps the lightest color value.
* - `EXCLUSION`: similar to `DIFFERENCE` but with less contrast.
* - `MULTIPLY`: color values from the source are multiplied with values from the canvas. The result is always darker.
* - `SCREEN`: all color values are inverted, then multiplied, then inverted again. The result is always lighter. (Opposite of `MULTIPLY`)
* - `REPLACE`: the last source drawn completely replaces the rest of the canvas.
* - `REMOVE`: overlapping pixels are removed by making them completely transparent.
*
* The following blend modes are only available in 2D mode:
*
* - `DIFFERENCE`: color values from the source are subtracted from the values from the canvas. If the difference is a negative number, it's made positive.
* - `OVERLAY`: combines `MULTIPLY` and `SCREEN`. Dark values in the canvas get darker and light values get lighter.
* - `HARD_LIGHT`: combines `MULTIPLY` and `SCREEN`. Dark values in the source get darker and light values get lighter.
* - `SOFT_LIGHT`: a softer version of `HARD_LIGHT`.
* - `DODGE`: lightens light tones and increases contrast. Divides the canvas color values by the inverted color values from the source.
* - `BURN`: darkens dark tones and increases contrast. Divides the source color values by the inverted color values from the canvas, then inverts the result.
*
* The following blend modes are only available in WebGL mode:
*
* - `SUBTRACT`: RGB values from the source are subtracted from the values from the canvas. If the difference is a negative number, it's made positive. Alpha (transparency) values from the source and canvas are added.
*
* @method blendMode
* @param {Constant} mode blend mode to set.
* either BLEND, DARKEST, LIGHTEST, DIFFERENCE, MULTIPLY,
* EXCLUSION, SCREEN, REPLACE, OVERLAY, HARD_LIGHT,
* SOFT_LIGHT, DODGE, BURN, ADD, REMOVE or SUBTRACT
*
* @example
* <div>
* <code>
* function setup() {
* createCanvas(100, 100);
*
* background(180);
*
* // Use the default blend mode.
* blendMode(BLEND);
*
* // Style the lines.
* strokeWeight(30);
*
* // Draw the first line.
* stroke('#1a85ff');
* line(25, 25, 75, 75);
*
* // Draw the second line.
* stroke('#d41159');
* line(75, 25, 25, 75);
*
* describe('A Sky Blue line and a Deep Rose line form an X on a gray background.');
* }
* </code>
* </div>
*
* <div>
* <code>
* function setup() {
* createCanvas(100, 100);
*
* background(180);
*
* // Set the blend mode.
* blendMode(HARD_LIGHT);
*
* // Style the lines.
* strokeWeight(30);
*
* // Draw the first line.
* stroke('#1a85ff');
* line(25, 25, 75, 75);
*
* // Draw the second line.
* stroke('#d41159');
* line(75, 25, 25, 75);
*
* describe('An ocean blue line and a hot pink line form an X on a gray background. The area where they overlap is Magenta purple.');
* }
* </code>
* </div>
*
* <div>
* <code>
* function setup() {
* createCanvas(100, 100);
*
* background(180);
*
* // Set the blend mode.
* blendMode(ADD);
*
* // Style the lines.
* strokeWeight(30);
*
* // Draw the first line.
* stroke('#1a85ff');
* line(25, 25, 75, 75);
*
* // Draw the second line.
* stroke('#d41159');
* line(75, 25, 25, 75);
*
* describe('An icy blue line and a light lavender line form an X on a gray background. The area where they overlap is white.');
* }
* </code>
* </div>
*
* <div>
* <code>
* function setup() {
* createCanvas(100, 100);
*
* background(180);
*
* // Set the blend mode.
* blendMode(DARKEST);
*
* // Style the lines.
* strokeWeight(30);
*
* // Draw the first line.
* stroke('#1a85ff');
* line(25, 25, 75, 75);
*
* // Draw the second line.
* stroke('#d41159');
* line(75, 25, 25, 75);
*
* describe('A steel blue line and a cranberry line form an X on a gray background. The area where they overlap is deep purple.');
* }
* </code>
* </div>
*
* <div>
* <code>
* function setup() {
* createCanvas(100, 100);
*
* background(180);
*
* // Set the blend mode.
* blendMode(BURN);
*
* // Style the lines.
* strokeWeight(30);
*
* // Draw the first line.
* stroke('#1a85ff');
* line(25, 25, 75, 75);
*
* // Draw the second line.
* stroke('#d41159');
* line(75, 25, 25, 75);
*
* describe('A cobalt blue line and a burgundy line form an X on a gray background. The area where they overlap is black.');
* }
* </code>
* </div>
*
* <div>
* <code>
* function setup() {
* createCanvas(100, 100);
*
* background(180);
*
* // Set the blend mode.
* blendMode(LIGHTEST);
*
* // Style the lines.
* strokeWeight(30);
*
* // Draw the first line.
* stroke('#1a85ff');
* line(25, 25, 75, 75);
*
* // Draw the second line.
* stroke('#d41159');
* line(75, 25, 25, 75);
*
* describe('A pale lavender line and a soft beige line form an X on a gray background. The area where they overlap is pale lilac.');
* }
* </code>
* </div>
*
* <div>
* <code>
* function setup() {
* createCanvas(100, 100);
*
* background(180);
*
* // Set the blend mode.
* blendMode(EXCLUSION);
*
* // Style the lines.
* strokeWeight(30);
*
* // Draw the first line.
* stroke('#1a85ff');
* line(25, 25, 75, 75);
*
* // Draw the second line.
* stroke('#d41159');
* line(75, 25, 25, 75);
*
* describe('An earthy brown line and a muted sage line form an X on a gray background. The area where they overlap is sage green.');
* }
* </code>
* </div>
*
* <div>
* <code>
* function setup() {
* createCanvas(100, 100);
*
* background(180);
*
* // Set the blend mode.
* blendMode(MULTIPLY);
*
* // Style the lines.
* strokeWeight(30);
*
* // Draw the first line.
* stroke('#1a85ff');
* line(25, 25, 75, 75);
*
* // Draw the second line.
* stroke('#d41159');
* line(75, 25, 25, 75);
*
* describe('A slate blue line and a plum line form an X on a gray background. The area where they overlap is dark Indigo.');
* }
* </code>
* </div>
*
* <div>
* <code>
* function setup() {
* createCanvas(100, 100);
*
* background(180);
*
* // Set the blend mode.
* blendMode(SCREEN);
*
* // Style the lines.
* strokeWeight(30);
*
* // Draw the first line.
* stroke('#1a85ff');
* line(25, 25, 75, 75);
*
* // Draw the second line.
* stroke('#d41159');
* line(75, 25, 25, 75);
*
* describe('A baby blue line and a peach pink line form an X on a gray background. The area where they overlap is misty lilac.');
* }
* </code>
* </div>