-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
Copy pathenvironment.js
1248 lines (1215 loc) · 30.8 KB
/
environment.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 Environment
* @submodule Environment
* @for p5
* @requires core
* @requires constants
*/
import p5 from './main';
import * as C from './constants';
const standardCursors = [C.ARROW, C.CROSS, C.HAND, C.MOVE, C.TEXT, C.WAIT];
p5.prototype._frameRate = 0;
p5.prototype._lastFrameTime = window.performance.now();
p5.prototype._targetFrameRate = 60;
const _windowPrint = window.print;
let windowPrintDisabled = false;
/**
* Displays text in the web browser's console.
*
* `print()` is helpful for printing values while debugging. Each call to
* `print()` creates a new line of text.
*
* Note: Call `print('\n')` to print a blank line. Calling `print()` without
* an argument opens the browser's dialog for printing documents.
*
* @method print
* @param {Any} contents content to print to the console.
* @example
* <div>
* <code class="norender">
* function setup() {
* // Prints "hello, world" to the console.
* print('hello, world');
* }
* </code>
* </div>
*
* <div>
* <code class="norender">
* function setup() {
* let name = 'ada';
* // Prints "hello, ada" to the console.
* print(`hello, ${name}`);
* }
* </code>
* </div>
*/
p5.prototype.print = function(...args) {
if (!args.length) {
if (!windowPrintDisabled) {
_windowPrint();
if (
window.confirm(
'You just tried to print the webpage. Do you want to prevent this from running again?'
)
) {
windowPrintDisabled = true;
}
}
} else {
console.log(...args);
}
};
/**
* A `Number` variable that tracks the number of frames drawn since the sketch
* started.
*
* `frameCount`'s value is 0 inside <a href="#/p5/setup">setup()</a>. It
* increments by 1 each time the code in <a href="#/p5/draw">draw()</a>
* finishes executing.
*
* @property {Integer} frameCount
* @readOnly
* @example
* <div>
* <code>
* function setup() {
* createCanvas(100, 100);
*
* background(200);
*
* // Display the value of
* // frameCount.
* textSize(30);
* textAlign(CENTER, CENTER);
* text(frameCount, 50, 50);
*
* describe('The number 0 written in black in the middle of a gray square.');
* }
* </code>
* </div>
*
* <div>
* <code>
* function setup() {
* createCanvas(100, 100);
*
* // Set the frameRate to 30.
* frameRate(30);
*
* textSize(30);
* textAlign(CENTER, CENTER);
*
* describe('A number written in black in the middle of a gray square. Its value increases rapidly.');
* }
*
* function draw() {
* background(200);
*
* // Display the value of
* // frameCount.
* text(frameCount, 50, 50);
* }
* </code>
* </div>
*/
p5.prototype.frameCount = 0;
/**
* A `Number` variable that tracks the number of milliseconds it took to draw
* the last frame.
*
* `deltaTime` contains the amount of time it took
* <a href="#/p5/draw">draw()</a> to execute during the previous frame. It's
* useful for simulating physics.
*
* @property {Integer} deltaTime
* @readOnly
* @example
* <div>
* <code>
* let x = 0;
* let speed = 0.05;
*
* function setup() {
* createCanvas(100, 100);
*
* // Set the frameRate to 30.
* frameRate(30);
*
* describe('A white circle moves from left to right on a gray background. It reappears on the left side when it reaches the right side.');
* }
*
* function draw() {
* background(200);
*
* // Use deltaTime to calculate
* // a change in position.
* let deltaX = speed * deltaTime;
*
* // Update the x variable.
* x += deltaX;
*
* // Reset x to 0 if it's
* // greater than 100.
* if (x > 100) {
* x = 0;
* }
*
* // Use x to set the circle's
* // position.
* circle(x, 50, 20);
* }
* </code>
* </div>
*/
p5.prototype.deltaTime = 0;
/**
* A `Boolean` variable that's `true` if the browser is focused and `false` if
* not.
*
* Note: The browser window can only receive input if it's focused.
*
* @property {Boolean} focused
* @readOnly
* @example
* <div>
* <code>
* // Open this example in two separate browser
* // windows placed side-by-side to demonstrate.
*
* function setup() {
* createCanvas(100, 100);
*
* describe('A square changes color from green to red when the browser window is out of focus.');
* }
*
* function draw() {
* // Change the background color
* // when the browser window
* // goes in/out of focus.
* if (focused === true) {
* background(0, 255, 0);
* } else {
* background(255, 0, 0);
* }
* }
* </code>
* </div>
*/
p5.prototype.focused = document.hasFocus();
/**
* Changes the cursor's appearance.
*
* The first parameter, `type`, sets the type of cursor to display. The
* built-in options are `ARROW`, `CROSS`, `HAND`, `MOVE`, `TEXT`, and `WAIT`.
* `cursor()` also recognizes standard CSS cursor properties passed as
* strings: `'help'`, `'wait'`, `'crosshair'`, `'not-allowed'`, `'zoom-in'`,
* and `'grab'`. If the path to an image is passed, as in
* `cursor('assets/target.png')`, then the image will be used as the cursor.
* Images must be in .cur, .gif, .jpg, .jpeg, or .png format and should be <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/cursor#icon_size_limits">at most 32 by 32 pixels large.</a>
*
* The parameters `x` and `y` are optional. If an image is used for the
* cursor, `x` and `y` set the location pointed to within the image. They are
* both 0 by default, so the cursor points to the image's top-left corner. `x`
* and `y` must be less than the image's width and height, respectively.
*
* @method cursor
* @param {String|Constant} type Built-in: either ARROW, CROSS, HAND, MOVE, TEXT, or WAIT.
* Native CSS properties: 'grab', 'progress', and so on.
* Path to cursor image.
* @param {Number} [x] horizontal active spot of the cursor.
* @param {Number} [y] vertical active spot of the cursor.
* @example
* <div>
* <code>
* function setup() {
* createCanvas(100, 100);
*
* describe('A gray square. The cursor appears as crosshairs.');
* }
*
* function draw() {
* background(200);
*
* // Set the cursor to crosshairs: +
* cursor(CROSS);
* }
* </code>
* </div>
*
* <div>
* <code>
* function setup() {
* createCanvas(100, 100);
*
* describe('A gray square divided into quadrants. The cursor image changes when the mouse moves to each quadrant.');
* }
*
* function draw() {
* background(200);
*
* // Divide the canvas into quadrants.
* line(50, 0, 50, 100);
* line(0, 50, 100, 50);
*
* // Change cursor based on mouse position.
* if (mouseX < 50 && mouseY < 50) {
* cursor(CROSS);
* } else if (mouseX > 50 && mouseY < 50) {
* cursor('progress');
* } else if (mouseX > 50 && mouseY > 50) {
* cursor('https://avatars0.githubusercontent.com/u/1617169?s=16');
* } else {
* cursor('grab');
* }
* }
* </code>
* </div>
*
* <div>
* <code>
* function setup() {
* createCanvas(100, 100);
*
* describe('An image of three purple curves follows the mouse. The image shifts when the mouse is pressed.');
* }
*
* function draw() {
* background(200);
*
* // Change the cursor's active spot
* // when the mouse is pressed.
* if (mouseIsPressed === true) {
* cursor('https://avatars0.githubusercontent.com/u/1617169?s=16', 8, 8);
* } else {
* cursor('https://avatars0.githubusercontent.com/u/1617169?s=16');
* }
* }
* </code>
* </div>
*/
p5.prototype.cursor = function(type, x, y) {
let cursor = 'auto';
const canvas = this._curElement.elt;
if (standardCursors.includes(type)) {
// Standard css cursor
cursor = type;
} else if (typeof type === 'string') {
let coords = '';
if (x && y && (typeof x === 'number' && typeof y === 'number')) {
// Note that x and y values must be unit-less positive integers < 32
// https://developer.mozilla.org/en-US/docs/Web/CSS/cursor
coords = `${x} ${y}`;
}
if (
type.substring(0, 7) === 'http://' ||
type.substring(0, 8) === 'https://'
) {
// Image (absolute url)
cursor = `url(${type}) ${coords}, auto`;
} else if (/\.(cur|jpg|jpeg|gif|png|CUR|JPG|JPEG|GIF|PNG)$/.test(type)) {
// Image file (relative path) - Separated for performance reasons
cursor = `url(${type}) ${coords}, auto`;
} else {
// Any valid string for the css cursor property
cursor = type;
}
}
canvas.style.cursor = cursor;
};
/**
* Sets the number of frames to draw per second.
*
* Calling `frameRate()` with one numeric argument, as in `frameRate(30)`,
* attempts to draw 30 frames per second (FPS). The target frame rate may not
* be achieved depending on the sketch's processing needs. Most computers
* default to a frame rate of 60 FPS. Frame rates of 24 FPS and above are
* fast enough for smooth animations.
*
* Calling `frameRate()` without an argument returns the current frame rate.
* The value returned is an approximation.
*
* @method frameRate
* @param {Number} fps number of frames to draw per second.
* @chainable
*
* @example
* <div>
* <code>
* function setup() {
* createCanvas(100, 100);
*
* describe('A white circle on a gray background. The circle moves from left to right in a loop. It slows down when the mouse is pressed.');
* }
*
* function draw() {
* background(200);
*
* // Set the x variable based
* // on the current frameCount.
* let x = frameCount % 100;
*
* // If the mouse is pressed,
* // decrease the frame rate.
* if (mouseIsPressed === true) {
* frameRate(10);
* } else {
* frameRate(60);
* }
*
* // Use x to set the circle's
* // position.
* circle(x, 50, 20);
* }
* </code>
* </div>
*
* <div>
* <code>
* function setup() {
* createCanvas(100, 100);
*
* describe('A number written in black on a gray background. The number decreases when the mouse is pressed.');
* }
*
* function draw() {
* background(200);
*
* // If the mouse is pressed, do lots
* // of math to slow down drawing.
* if (mouseIsPressed === true) {
* for (let i = 0; i < 1000000; i += 1) {
* random();
* }
* }
*
* // Get the current frame rate
* // and display it.
* let fps = frameRate();
* text(fps, 50, 50);
* }
* </code>
* </div>
*/
/**
* @method frameRate
* @return {Number} current frame rate.
*/
p5.prototype.frameRate = function(fps) {
p5._validateParameters('frameRate', arguments);
if (typeof fps !== 'number' || fps < 0) {
return this._frameRate;
} else {
this._setProperty('_targetFrameRate', fps);
if (fps === 0) {
this._setProperty('_frameRate', fps);
}
return this;
}
};
/**
* Returns the current framerate.
*
* @private
* @return {Number} current frameRate
*/
p5.prototype.getFrameRate = function() {
return this.frameRate();
};
/**
* Specifies the number of frames to be displayed every second. For example,
* the function call frameRate(30) will attempt to refresh 30 times a second.
* If the processor is not fast enough to maintain the specified rate, the
* frame rate will not be achieved. Setting the frame rate within <a href="#/p5/setup">setup()</a> is
* recommended. The default rate is 60 frames per second.
*
* Calling `frameRate()` with no arguments returns the current frame rate.
*
* @private
* @param {Number} [fps] number of frames to be displayed every second
*/
p5.prototype.setFrameRate = function(fps) {
return this.frameRate(fps);
};
/**
* Returns the target frame rate.
*
* The value is either the system frame rate or the last value passed to
* <a href="#/p5/frameRate">frameRate()</a>.
*
* @method getTargetFrameRate
* @return {Number} _targetFrameRate
* @example
* <div>
* <code>
* function setup() {
* createCanvas(100, 100);
*
* describe('The number 20 written in black on a gray background.');
* }
*
* function draw() {
* background(200);
*
* // Set the frame rate to 20.
* frameRate(20);
*
* // Get the target frame rate and
* // display it.
* let fps = getTargetFrameRate();
* text(fps, 43, 54);
* }
* </code>
* </div>
*/
p5.prototype.getTargetFrameRate = function() {
return this._targetFrameRate;
};
/**
* Hides the cursor from view.
*
* @method noCursor
* @example
* <div>
* <code>
* function setup() {
* // Hide the cursor.
* noCursor();
* }
*
* function draw() {
* background(200);
*
* circle(mouseX, mouseY, 10);
*
* describe('A white circle on a gray background. The circle follows the mouse as it moves. The cursor is hidden.');
* }
* </code>
* </div>
*/
p5.prototype.noCursor = function() {
this._curElement.elt.style.cursor = 'none';
};
/**
* A `String` variable with the WebGL version in use.
*
* `webglVersion`'s value equals one of the following string constants:
*
* - `WEBGL2` whose value is `'webgl2'`,
* - `WEBGL` whose value is `'webgl'`, or
* - `P2D` whose value is `'p2d'`. This is the default for 2D sketches.
*
* See <a href="#/p5/setAttributes">setAttributes()</a> for ways to set the
* WebGL version.
*
* @property {String} webglVersion
* @readOnly
* @example
* <div>
* <code>
* function setup() {
* background(200);
*
* // Display the current WebGL version.
* text(webglVersion, 42, 54);
*
* describe('The text "p2d" written in black on a gray background.');
* }
* </code>
* </div>
*
* <div>
* <code>
* let font;
*
* function preload() {
* // Load a font to use.
* font = loadFont('assets/inconsolata.otf');
* }
*
* function setup() {
* // Create a canvas using WEBGL mode.
* createCanvas(100, 50, WEBGL);
* background(200);
*
* // Display the current WebGL version.
* fill(0);
* textFont(font);
* text(webglVersion, -15, 5);
*
* describe('The text "webgl2" written in black on a gray background.');
* }
* </code>
* </div>
*
* <div>
* <code>
* let font;
*
* function preload() {
* // Load a font to use.
* font = loadFont('assets/inconsolata.otf');
* }
*
* function setup() {
* // Create a canvas using WEBGL mode.
* createCanvas(100, 50, WEBGL);
*
* // Set WebGL to version 1.
* setAttributes({ version: 1 });
*
* background(200);
*
* // Display the current WebGL version.
* fill(0);
* textFont(font);
* text(webglVersion, -14, 5);
*
* describe('The text "webgl" written in black on a gray background.');
* }
* </code>
* </div>
*/
p5.prototype.webglVersion = C.P2D;
/**
* A `Number` variable that stores the width of the screen display.
*
* `displayWidth` is useful for running full-screen programs. Its value
* depends on the current <a href="#/p5/pixelDensity">pixelDensity()</a>.
*
* Note: The actual screen width can be computed as
* `displayWidth * pixelDensity()`.
*
* @property {Number} displayWidth
* @readOnly
* @example
* <div class="norender">
* <code>
* function setup() {
* // Set the canvas' width and height
* // using the display's dimensions.
* createCanvas(displayWidth, displayHeight);
*
* background(200);
*
* describe('A gray canvas that is the same size as the display.');
* }
* </code>
* </div>
*
* @alt
* This example does not render anything.
*/
p5.prototype.displayWidth = screen.width;
/**
* A `Number` variable that stores the height of the screen display.
*
* `displayHeight` is useful for running full-screen programs. Its value
* depends on the current <a href="#/p5/pixelDensity">pixelDensity()</a>.
*
* Note: The actual screen height can be computed as
* `displayHeight * pixelDensity()`.
*
* @property {Number} displayHeight
* @readOnly
* @example
* <div class="norender">
* <code>
* function setup() {
* // Set the canvas' width and height
* // using the display's dimensions.
* createCanvas(displayWidth, displayHeight);
*
* background(200);
*
* describe('A gray canvas that is the same size as the display.');
* }
* </code>
* </div>
*
* @alt
* This example does not render anything.
*/
p5.prototype.displayHeight = screen.height;
/**
* A `Number` variable that stores the width of the browser's viewport.
*
* The <a href="https://developer.mozilla.org/en-US/docs/Glossary/Layout_viewport" target="_blank">layout viewport</a>
* is the area within the browser that's available for drawing.
*
* @property {Number} windowWidth
* @readOnly
* @example
* <div class="norender">
* <code>
* function setup() {
* // Set the canvas' width and height
* // using the browser's dimensions.
* createCanvas(windowWidth, windowHeight);
*
* background(200);
*
* describe('A gray canvas that takes up the entire browser window.');
* }
* </code>
* </div>
*
* @alt
* This example does not render anything.
*/
p5.prototype.windowWidth = getWindowWidth();
/**
* A `Number` variable that stores the height of the browser's viewport.
*
* The <a href="https://developer.mozilla.org/en-US/docs/Glossary/Layout_viewport" target="_blank">layout viewport</a>
* is the area within the browser that's available for drawing.
*
* @property {Number} windowHeight
* @readOnly
* @example
* <div class="norender">
* <code>
* function setup() {
* // Set the canvas' width and height
* // using the browser's dimensions.
* createCanvas(windowWidth, windowHeight);
*
* background(200);
*
* describe('A gray canvas that takes up the entire browser window.');
* }
* </code>
* </div>
*
* @alt
* This example does not render anything.
*/
p5.prototype.windowHeight = getWindowHeight();
/**
* A function that's called when the browser window is resized.
*
* Code placed in the body of `windowResized()` will run when the
* browser window's size changes. It's a good place to call
* <a href="#/p5/resizeCanvas">resizeCanvas()</a> or make other
* adjustments to accommodate the new window size.
*
* The `event` parameter is optional. If added to the function declaration, it
* can be used for debugging or other purposes.
*
* @method windowResized
* @param {UIEvent} [event] optional resize Event.
* @example
* <div class="norender">
* <code>
* function setup() {
* createCanvas(windowWidth, windowHeight);
*
* describe('A gray canvas with a white circle at its center. The canvas takes up the entire browser window. It changes size to match the browser window.');
* }
*
* function draw() {
* background(200);
*
* // Draw a circle at the center.
* circle(width / 2, height / 2, 50);
* }
*
* // Resize the canvas when the
* // browser's size changes.
* function windowResized() {
* resizeCanvas(windowWidth, windowHeight);
* }
* </code>
* </div>
* @alt
* This example does not render anything.
*
* <div class="norender">
* <code>
* function setup() {
* createCanvas(windowWidth, windowHeight);
* }
*
* function draw() {
* background(200);
*
* describe('A gray canvas that takes up the entire browser window. It changes size to match the browser window.');
* }
*
* function windowResized(event) {
* // Resize the canvas when the
* // browser's size changes.
* resizeCanvas(windowWidth, windowHeight);
*
* // Print the resize event to the console for debugging.
* print(event);
* }
* </code>
* </div>
* @alt
* This example does not render anything.
*/
p5.prototype._onresize = function(e) {
this._setProperty('windowWidth', getWindowWidth());
this._setProperty('windowHeight', getWindowHeight());
const context = this._isGlobal ? window : this;
let executeDefault;
if (typeof context.windowResized === 'function') {
executeDefault = context.windowResized(e);
if (executeDefault !== undefined && !executeDefault) {
e.preventDefault();
}
}
};
function getWindowWidth() {
return (
window.innerWidth ||
(document.documentElement && document.documentElement.clientWidth) ||
(document.body && document.body.clientWidth) ||
0
);
}
function getWindowHeight() {
return (
window.innerHeight ||
(document.documentElement && document.documentElement.clientHeight) ||
(document.body && document.body.clientHeight) ||
0
);
}
/**
* A `Number` variable that stores the width of the canvas in pixels.
*
* `width`'s default value is 100. Calling
* <a href="#/p5/createCanvas">createCanvas()</a> or
* <a href="#/p5/resizeCanvas">resizeCanvas()</a> changes the value of
* `width`. Calling <a href="#/p5/noCanvas">noCanvas()</a> sets its value to
* 0.
*
* @example
* <div>
* <code>
* function setup() {
* background(200);
*
* // Display the canvas' width.
* text(width, 42, 54);
*
* describe('The number 100 written in black on a gray square.');
* }
* </code>
* </div>
*
* <div>
* <code>
* function setup() {
* createCanvas(50, 100);
*
* background(200);
*
* // Display the canvas' width.
* text(width, 21, 54);
*
* describe('The number 50 written in black on a gray rectangle.');
* }
* </code>
* </div>
*
* <div>
* <code>
* function setup() {
* createCanvas(100, 100);
*
* background(200);
*
* // Display the canvas' width.
* text(width, 42, 54);
*
* describe('The number 100 written in black on a gray square. When the mouse is pressed, the square becomes a rectangle and the number becomes 50.');
* }
*
* // If the mouse is pressed, reisze
* // the canvas and display its new
* // width.
* function mousePressed() {
* if (mouseX > 0 && mouseX < width && mouseY > 0 && mouseY < height) {
* resizeCanvas(50, 100);
* background(200);
* text(width, 21, 54);
* }
* }
* </code>
* </div>
*
* @property {Number} width
* @readOnly
*/
p5.prototype.width = 0;
/**
* A `Number` variable that stores the height of the canvas in pixels.
*
* `height`'s default value is 100. Calling
* <a href="#/p5/createCanvas">createCanvas()</a> or
* <a href="#/p5/resizeCanvas">resizeCanvas()</a> changes the value of
* `height`. Calling <a href="#/p5/noCanvas">noCanvas()</a> sets its value to
* 0.
*
* @example
* <div>
* <code>
* function setup() {
* background(200);
*
* // Display the canvas' height.
* text(height, 42, 54);
*
* describe('The number 100 written in black on a gray square.');
* }
* </code>
* </div>
*
* <div>
* <code>
* function setup() {
* createCanvas(100, 50);
*
* background(200);
*
* // Display the canvas' height.
* text(height, 42, 27);
*
* describe('The number 50 written in black on a gray rectangle.');
* }
* </code>
* </div>
*
* <div>
* <code>
* function setup() {
* createCanvas(100, 100);
*
* background(200);
*
* // Display the canvas' height.
* text(height, 42, 54);
*
* describe('The number 100 written in black on a gray square. When the mouse is pressed, the square becomes a rectangle and the number becomes 50.');
* }
*
* // If the mouse is pressed, reisze
* // the canvas and display its new
* // height.
* function mousePressed() {
* if (mouseX > 0 && mouseX < width && mouseY > 0 && mouseY < height) {
* resizeCanvas(100, 50);
* background(200);
* text(height, 42, 27);
* }
* }
* </code>
* </div>
*
* @property {Number} height
* @readOnly
*/
p5.prototype.height = 0;
/**
* Toggles full-screen mode or returns the current mode.
*
* Calling `fullscreen(true)` makes the sketch full-screen. Calling
* `fullscreen(false)` makes the sketch its original size.
*
* Calling `fullscreen()` without an argument returns `true` if the sketch
* is in full-screen mode and `false` if not.
*
* Note: Due to browser restrictions, `fullscreen()` can only be called with
* user input such as a mouse press.
*
* @method fullscreen
* @param {Boolean} [val] whether the sketch should be in fullscreen mode.
* @return {Boolean} current fullscreen state.
* @example
* <div>
* <code>
* function setup() {
* background(200);
*
* describe('A gray canvas that switches between default and full-screen display when clicked.');
* }
*
* // If the mouse is pressed,
* // toggle full-screen mode.
* function mousePressed() {
* if (mouseX > 0 && mouseX < width && mouseY > 0 && mouseY < height) {
* let fs = fullscreen();
* fullscreen(!fs);
* }
* }
* </code>
* </div>
*/
p5.prototype.fullscreen = function(val) {
p5._validateParameters('fullscreen', arguments);
// no arguments, return fullscreen or not
if (typeof val === 'undefined') {
return (
document.fullscreenElement ||
document.webkitFullscreenElement ||
document.mozFullScreenElement ||
document.msFullscreenElement
);
} else {
// otherwise set to fullscreen or not
if (val) {
launchFullscreen(document.documentElement);
} else {
exitFullscreen();
}
}
};
/**
* Sets the pixel density or returns the current density.
*
* Computer displays are grids of little lights called <em>pixels</em>. A
* display's <em>pixel density</em> describes how many pixels it packs into an