-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
Copy pathtransform.js
1409 lines (1395 loc) · 35.7 KB
/
transform.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 Transform
* @submodule Transform
* @for p5
* @requires core
* @requires constants
*/
import p5 from './main';
/**
* Applies a transformation matrix to the coordinate system.
*
* Transformations such as
* <a href="#/p5/translate">translate()</a>,
* <a href="#/p5/rotate">rotate()</a>, and
* <a href="#/p5/scale">scale()</a>
* use matrix-vector multiplication behind the scenes. A table of numbers,
* called a matrix, encodes each transformation. The values in the matrix
* then multiply each point on the canvas, which is represented by a vector.
*
* `applyMatrix()` allows for many transformations to be applied at once. See
* <a href="https://en.wikipedia.org/wiki/Transformation_matrix" target="_blank">Wikipedia</a>
* and <a href="https://developer.mozilla.org/en-US/docs/Web/API/WebGL_API/Matrix_math_for_the_web" target="_blank">MDN</a>
* for more details about transformations.
*
* There are two ways to call `applyMatrix()` in two and three dimensions.
*
* In 2D mode, the parameters `a`, `b`, `c`, `d`, `e`, and `f`, correspond to
* elements in the following transformation matrix:
*
* > <img style="max-width: 150px" src="assets/transformation-matrix.png"
* alt="The transformation matrix used when applyMatrix is called in 2D mode."/>
*
* The numbers can be passed individually, as in
* `applyMatrix(2, 0, 0, 0, 2, 0)`. They can also be passed in an array, as in
* `applyMatrix([2, 0, 0, 0, 2, 0])`.
*
* In 3D mode, the parameters `a`, `b`, `c`, `d`, `e`, `f`, `g`, `h`, `i`,
* `j`, `k`, `l`, `m`, `n`, `o`, and `p` correspond to elements in the
* following transformation matrix:
*
* <img style="max-width: 300px" src="assets/transformation-matrix-4-4.png"
* alt="The transformation matrix used when applyMatrix is called in 3D mode."/>
*
* The numbers can be passed individually, as in
* `applyMatrix(2, 0, 0, 0, 0, 2, 0, 0, 0, 0, 2, 0, 0, 0, 0, 1)`. They can
* also be passed in an array, as in
* `applyMatrix([2, 0, 0, 0, 0, 2, 0, 0, 0, 0, 2, 0, 0, 0, 0, 1])`.
*
* By default, transformations accumulate. The
* <a href="#/p5/push">push()</a> and <a href="#/p5/pop">pop()</a> functions
* can be used to isolate transformations within distinct drawing groups.
*
* Note: Transformations are reset at the beginning of the draw loop. Calling
* `applyMatrix()` inside the <a href="#/p5/draw">draw()</a> function won't
* cause shapes to transform continuously.
*
* @method applyMatrix
* @param {Array} arr an array containing the elements of the transformation matrix. Its length should be either 6 (2D) or 16 (3D).
* @chainable
*
* @example
* <div>
* <code>
* function setup() {
* createCanvas(100, 100);
*
* describe('A white circle on a gray background.');
* }
*
* function draw() {
* background(200);
*
* // Translate the origin to the center.
* applyMatrix(1, 0, 0, 1, 50, 50);
*
* // Draw the circle at coordinates (0, 0).
* circle(0, 0, 40);
* }
* </code>
* </div>
*
* <div>
* <code>
* function setup() {
* createCanvas(100, 100);
*
* describe('A white circle on a gray background.');
* }
*
* function draw() {
* background(200);
*
* // Translate the origin to the center.
* let m = [1, 0, 0, 1, 50, 50];
* applyMatrix(m);
*
* // Draw the circle at coordinates (0, 0).
* circle(0, 0, 40);
* }
* </code>
* </div>
*
* <div>
* <code>
* function setup() {
* createCanvas(100, 100);
*
* describe("A white rectangle on a gray background. The rectangle's long axis runs from top-left to bottom-right.");
* }
*
* function draw() {
* background(200);
*
* // Rotate the coordinate system 1/8 turn.
* let angle = QUARTER_PI;
* let ca = cos(angle);
* let sa = sin(angle);
* applyMatrix(ca, sa, -sa, ca, 0, 0);
*
* // Draw a rectangle at coordinates (50, 0).
* rect(50, 0, 40, 20);
* }
* </code>
* </div>
*
* <div>
* <code>
* function setup() {
* createCanvas(100, 100);
*
* describe(
* 'Two white squares on a gray background. The larger square appears at the top-center. The smaller square appears at the top-left.'
* );
* }
*
* function draw() {
* background(200);
*
* // Draw a square at (30, 20).
* square(30, 20, 40);
*
* // Scale the coordinate system by a factor of 0.5.
* applyMatrix(0.5, 0, 0, 0.5, 0, 0);
*
* // Draw a square at (30, 20).
* // It appears at (15, 10) after scaling.
* square(30, 20, 40);
* }
* </code>
* </div>
*
* <div>
* <code>
* function setup() {
* createCanvas(100, 100);
*
* describe('A white quadrilateral on a gray background.');
* }
*
* function draw() {
* background(200);
*
* // Calculate the shear factor.
* let angle = QUARTER_PI;
* let shearFactor = 1 / tan(HALF_PI - angle);
*
* // Shear the coordinate system along the x-axis.
* applyMatrix(1, 0, shearFactor, 1, 0, 0);
*
* // Draw the square.
* square(0, 0, 50);
* }
* </code>
* </div>
*
* <div>
* <code>
* // Click and drag the mouse to view the scene from different angles.
*
* function setup() {
* createCanvas(100, 100, WEBGL);
*
* describe('A white cube rotates slowly against a gray background.');
* }
*
* function draw() {
* background(200);
*
* // Enable orbiting with the mouse.
* orbitControl();
*
* // Rotate the coordinate system a little more each frame.
* let angle = frameCount * 0.01;
* let ca = cos(angle);
* let sa = sin(angle);
* applyMatrix(ca, 0, sa, 0, 0, 1, 0, 0, -sa, 0, ca, 0, 0, 0, 0, 1);
*
* // Draw a box.
* box();
* }
* </code>
* </div>
*/
/**
* @method applyMatrix
* @param {Number} a an element of the transformation matrix.
* @param {Number} b an element of the transformation matrix.
* @param {Number} c an element of the transformation matrix.
* @param {Number} d an element of the transformation matrix.
* @param {Number} e an element of the transformation matrix.
* @param {Number} f an element of the transformation matrix.
* @chainable
*/
/**
* @method applyMatrix
* @param {Number} a
* @param {Number} b
* @param {Number} c
* @param {Number} d
* @param {Number} e
* @param {Number} f
* @param {Number} g an element of the transformation matrix.
* @param {Number} h an element of the transformation matrix.
* @param {Number} i an element of the transformation matrix.
* @param {Number} j an element of the transformation matrix.
* @param {Number} k an element of the transformation matrix.
* @param {Number} l an element of the transformation matrix.
* @param {Number} m an element of the transformation matrix.
* @param {Number} n an element of the transformation matrix.
* @param {Number} o an element of the transformation matrix.
* @param {Number} p an element of the transformation matrix.
* @chainable
*/
p5.prototype.applyMatrix = function(...args) {
let isTypedArray = args[0] instanceof Object.getPrototypeOf(Uint8Array);
if (Array.isArray(args[0]) || isTypedArray) {
this._renderer.applyMatrix(...args[0]);
} else {
this._renderer.applyMatrix(...args);
}
return this;
};
/**
* Clears all transformations applied to the coordinate system.
*
* @method resetMatrix
* @chainable
*
* @example
* <div>
* <code>
* function setup() {
* createCanvas(100, 100);
*
* describe(
* 'Two circles drawn on a gray background. A blue circle is at the top-left and a red circle is at the bottom-right.'
* );
* }
*
* function draw() {
* background(200);
*
* // Translate the origin to the center.
* translate(50, 50);
*
* // Draw a blue circle at the coordinates (25, 25).
* fill('blue');
* circle(25, 25, 20);
*
* // Clear all transformations.
* // The origin is now at the top-left corner.
* resetMatrix();
*
* // Draw a red circle at the coordinates (25, 25).
* fill('red');
* circle(25, 25, 20);
* }
* </code>
* </div>
*/
p5.prototype.resetMatrix = function() {
this._renderer.resetMatrix();
return this;
};
/**
* Rotates the coordinate system.
*
* By default, the positive x-axis points to the right and the positive y-axis
* points downward. The `rotate()` function changes this orientation by
* rotating the coordinate system about the origin. Everything drawn after
* `rotate()` is called will appear to be rotated.
*
* The first parameter, `angle`, is the amount to rotate. For example, calling
* `rotate(1)` rotates the coordinate system clockwise 1 radian which is
* nearly 57˚. `rotate()` interprets angle values using the current
* <a href="#/p5/angleMode">angleMode()</a>.
*
* The second parameter, `axis`, is optional. It's used to orient 3D rotations
* in WebGL mode. If a <a href="#/p5.Vector">p5.Vector</a> is passed, as in
* `rotate(QUARTER_PI, myVector)`, then the coordinate system will rotate
* `QUARTER_PI` radians about `myVector`. If an array of vector components is
* passed, as in `rotate(QUARTER_PI, [1, 0, 0])`, then the coordinate system
* will rotate `QUARTER_PI` radians about a vector with the components
* `[1, 0, 0]`.
*
* By default, transformations accumulate. For example, calling `rotate(1)`
* twice has the same effect as calling `rotate(2)` once. The
* <a href="#/p5/push">push()</a> and <a href="#/p5/pop">pop()</a> functions
* can be used to isolate transformations within distinct drawing groups.
*
* Note: Transformations are reset at the beginning of the draw loop. Calling
* `rotate(1)` inside the <a href="#/p5/draw">draw()</a> function won't cause
* shapes to spin.
*
* @method rotate
* @param {Number} angle angle of rotation in the current <a href="#/p5/angleMode">angleMode()</a>.
* @param {p5.Vector|Number[]} [axis] axis to rotate about in 3D.
* @chainable
*
* @example
* <div>
* <code>
* function setup() {
* createCanvas(100, 100);
*
* describe(
* "A white rectangle on a gray background. The rectangle's long axis runs from top-left to bottom-right."
* );
* }
*
* function draw() {
* background(200);
*
* // Rotate the coordinate system 1/8 turn.
* rotate(QUARTER_PI);
*
* // Draw a rectangle at coordinates (50, 0).
* rect(50, 0, 40, 20);
* }
* </code>
* </div>
*
* <div>
* <code>
* function setup() {
* createCanvas(100, 100);
*
* describe(
* "A white rectangle on a gray background. The rectangle's long axis runs from top-left to bottom-right."
* );
* }
*
* function draw() {
* background(200);
*
* // Rotate the coordinate system 1/16 turn.
* rotate(QUARTER_PI / 2);
*
* // Rotate the coordinate system another 1/16 turn.
* rotate(QUARTER_PI / 2);
*
* // Draw a rectangle at coordinates (50, 0).
* rect(50, 0, 40, 20);
* }
* </code>
* </div>
*
* <div>
* <code>
* function setup() {
* createCanvas(100, 100);
*
* // Use degrees.
* angleMode(DEGREES);
*
* describe(
* "A white rectangle on a gray background. The rectangle's long axis runs from top-left to bottom-right."
* );
* }
*
* function draw() {
* background(200);
*
* // Rotate the coordinate system 1/8 turn.
* rotate(45);
*
* // Draw a rectangle at coordinates (50, 0).
* rect(50, 0, 40, 20);
* }
* </code>
* </div>
*
* <div>
* <code>
* function setup() {
* createCanvas(100, 100);
*
* describe(
* 'A white rectangle on a gray background. The rectangle rotates slowly about the top-left corner. It disappears and reappears periodically.'
* );
* }
*
* function draw() {
* background(200);
*
* // Rotate the coordinate system a little more each frame.
* let angle = frameCount * 0.01;
* rotate(angle);
*
* // Draw a rectangle at coordinates (50, 0).
* rect(50, 0, 40, 20);
* }
* </code>
* </div>
*
* <div>
* <code>
* function setup() {
* createCanvas(100, 100, WEBGL);
*
* describe("A cube on a gray background. The cube's front face points to the top-right.");
* }
*
* function draw() {
* background(200);
*
* // Rotate the coordinate system 1/8 turn about
* // the axis [1, 1, 0].
* let axis = createVector(1, 1, 0);
* rotate(QUARTER_PI, axis);
*
* // Draw a box.
* box();
* }
* </code>
* </div>
*
* <div>
* <code>
* function setup() {
* createCanvas(100, 100, WEBGL);
*
* describe("A cube on a gray background. The cube's front face points to the top-right.");
* }
*
* function draw() {
* background(200);
*
* // Rotate the coordinate system 1/8 turn about
* // the axis [1, 1, 0].
* let axis = [1, 1, 0];
* rotate(QUARTER_PI, axis);
*
* // Draw a box.
* box();
* }
* </code>
* </div>
*/
p5.prototype.rotate = function(angle, axis) {
p5._validateParameters('rotate', arguments);
this._renderer.rotate(this._toRadians(angle), axis);
return this;
};
/**
* Rotates the coordinate system about the x-axis in WebGL mode.
*
* The parameter, `angle`, is the amount to rotate. For example, calling
* `rotateX(1)` rotates the coordinate system about the x-axis by 1 radian.
* `rotateX()` interprets angle values using the current
* <a href="#/p5/angleMode">angleMode()</a>.
*
* By default, transformations accumulate. For example, calling `rotateX(1)`
* twice has the same effect as calling `rotateX(2)` once. The
* <a href="#/p5/push">push()</a> and <a href="#/p5/pop">pop()</a> functions
* can be used to isolate transformations within distinct drawing groups.
*
* Note: Transformations are reset at the beginning of the draw loop. Calling
* `rotateX(1)` inside the <a href="#/p5/draw">draw()</a> function won't cause
* shapes to spin.
*
* @method rotateX
* @param {Number} angle angle of rotation in the current <a href="#/p5/angleMode">angleMode()</a>.
* @chainable
*
* @example
* <div>
* <code>
* // Click and drag the mouse to view the scene from different angles.
*
* function setup() {
* createCanvas(100, 100, WEBGL);
*
* describe('A white cube on a gray background.');
* }
*
* function draw() {
* background(200);
*
* // Enable orbiting with the mouse.
* orbitControl();
*
* // Rotate the coordinate system 1/8 turn.
* rotateX(QUARTER_PI);
*
* // Draw a box.
* box();
* }
* </code>
* </div>
*
* <div>
* <code>
* // Click and drag the mouse to view the scene from different angles.
*
* function setup() {
* createCanvas(100, 100, WEBGL);
*
* describe('A white cube on a gray background.');
* }
*
* function draw() {
* background(200);
*
* // Enable orbiting with the mouse.
* orbitControl();
*
* // Rotate the coordinate system 1/16 turn.
* rotateX(QUARTER_PI / 2);
*
* // Rotate the coordinate system 1/16 turn.
* rotateX(QUARTER_PI / 2);
*
* // Draw a box.
* box();
* }
* </code>
* </div>
*
* <div>
* <code>
* // Click and drag the mouse to view the scene from different angles.
*
* function setup() {
* createCanvas(100, 100, WEBGL);
*
* // Use degrees.
* angleMode(DEGREES);
*
* describe('A white cube on a gray background.');
* }
*
* function draw() {
* background(200);
*
* // Enable orbiting with the mouse.
* orbitControl();
*
* // Rotate the coordinate system 1/8 turn.
* rotateX(45);
*
* // Draw a box.
* box();
* }
* </code>
* </div>
*
* <div>
* <code>
* // Click and drag the mouse to view the scene from different angles.
*
* function setup() {
* createCanvas(100, 100, WEBGL);
*
* describe('A white cube rotates slowly against a gray background.');
* }
*
* function draw() {
* background(200);
*
* // Enable orbiting with the mouse.
* orbitControl();
*
* // Rotate the coordinate system a little more each frame.
* let angle = frameCount * 0.01;
* rotateX(angle);
*
* // Draw a box.
* box();
* }
* </code>
* </div>
*/
p5.prototype.rotateX = function(angle) {
this._assert3d('rotateX');
p5._validateParameters('rotateX', arguments);
this._renderer.rotateX(this._toRadians(angle));
return this;
};
/**
* Rotates the coordinate system about the y-axis in WebGL mode.
*
* The parameter, `angle`, is the amount to rotate. For example, calling
* `rotateY(1)` rotates the coordinate system about the y-axis by 1 radian.
* `rotateY()` interprets angle values using the current
* <a href="#/p5/angleMode">angleMode()</a>.
*
* By default, transformations accumulate. For example, calling `rotateY(1)`
* twice has the same effect as calling `rotateY(2)` once. The
* <a href="#/p5/push">push()</a> and <a href="#/p5/pop">pop()</a> functions
* can be used to isolate transformations within distinct drawing groups.
*
* Note: Transformations are reset at the beginning of the draw loop. Calling
* `rotateY(1)` inside the <a href="#/p5/draw">draw()</a> function won't cause
* shapes to spin.
*
* @method rotateY
* @param {Number} angle angle of rotation in the current <a href="#/p5/angleMode">angleMode()</a>.
* @chainable
*
* @example
* <div>
* <code>
* // Click and drag the mouse to view the scene from different angles.
*
* function setup() {
* createCanvas(100, 100, WEBGL);
*
* describe('A white cube on a gray background.');
* }
*
* function draw() {
* background(200);
*
* // Enable orbiting with the mouse.
* orbitControl();
*
* // Rotate the coordinate system 1/8 turn.
* rotateY(QUARTER_PI);
*
* // Draw a box.
* box();
* }
* </code>
* </div>
*
* <div>
* <code>
* // Click and drag the mouse to view the scene from different angles.
*
* function setup() {
* createCanvas(100, 100, WEBGL);
*
* describe('A white cube on a gray background.');
* }
*
* function draw() {
* background(200);
*
* // Enable orbiting with the mouse.
* orbitControl();
*
* // Rotate the coordinate system 1/16 turn.
* rotateY(QUARTER_PI / 2);
*
* // Rotate the coordinate system 1/16 turn.
* rotateY(QUARTER_PI / 2);
*
* // Draw a box.
* box();
* }
* </code>
* </div>
*
* <div>
* <code>
* // Click and drag the mouse to view the scene from different angles.
*
* function setup() {
* createCanvas(100, 100, WEBGL);
*
* // Use degrees.
* angleMode(DEGREES);
*
* describe('A white cube on a gray background.');
* }
*
* function draw() {
* background(200);
*
* // Enable orbiting with the mouse.
* orbitControl();
*
* // Rotate the coordinate system 1/8 turn.
* rotateY(45);
*
* // Draw a box.
* box();
* }
* </code>
* </div>
*
* <div>
* <code>
* // Click and drag the mouse to view the scene from different angles.
*
* function setup() {
* createCanvas(100, 100, WEBGL);
*
* describe('A white cube rotates slowly against a gray background.');
* }
*
* function draw() {
* background(200);
*
* // Enable orbiting with the mouse.
* orbitControl();
*
* // Rotate the coordinate system a little more each frame.
* let angle = frameCount * 0.01;
* rotateY(angle);
*
* // Draw a box.
* box();
* }
* </code>
* </div>
*/
p5.prototype.rotateY = function(angle) {
this._assert3d('rotateY');
p5._validateParameters('rotateY', arguments);
this._renderer.rotateY(this._toRadians(angle));
return this;
};
/**
* Rotates the coordinate system about the z-axis in WebGL mode.
*
* The parameter, `angle`, is the amount to rotate. For example, calling
* `rotateZ(1)` rotates the coordinate system about the z-axis by 1 radian.
* `rotateZ()` interprets angle values using the current
* <a href="#/p5/angleMode">angleMode()</a>.
*
* By default, transformations accumulate. For example, calling `rotateZ(1)`
* twice has the same effect as calling `rotateZ(2)` once. The
* <a href="#/p5/push">push()</a> and <a href="#/p5/pop">pop()</a> functions
* can be used to isolate transformations within distinct drawing groups.
*
* Note: Transformations are reset at the beginning of the draw loop. Calling
* `rotateZ(1)` inside the <a href="#/p5/draw">draw()</a> function won't cause
* shapes to spin.
*
* @method rotateZ
* @param {Number} angle angle of rotation in the current <a href="#/p5/angleMode">angleMode()</a>.
* @chainable
*
* @example
* <div>
* <code>
* // Click and drag the mouse to view the scene from different angles.
*
* function setup() {
* createCanvas(100, 100, WEBGL);
*
* describe('A white cube on a gray background.');
* }
*
* function draw() {
* background(200);
*
* // Enable orbiting with the mouse.
* orbitControl();
*
* // Rotate the coordinate system 1/8 turn.
* rotateZ(QUARTER_PI);
*
* // Draw a box.
* box();
* }
* </code>
* </div>
*
* <div>
* <code>
* // Click and drag the mouse to view the scene from different angles.
*
* function setup() {
* createCanvas(100, 100, WEBGL);
*
* describe('A white cube on a gray background.');
* }
*
* function draw() {
* background(200);
*
* // Enable orbiting with the mouse.
* orbitControl();
*
* // Rotate the coordinate system 1/16 turn.
* rotateZ(QUARTER_PI / 2);
*
* // Rotate the coordinate system 1/16 turn.
* rotateZ(QUARTER_PI / 2);
*
* // Draw a box.
* box();
* }
* </code>
* </div>
*
* <div>
* <code>
* // Click and drag the mouse to view the scene from different angles.
*
* function setup() {
* createCanvas(100, 100, WEBGL);
*
* // Use degrees.
* angleMode(DEGREES);
*
* describe('A white cube on a gray background.');
* }
*
* function draw() {
* background(200);
*
* // Enable orbiting with the mouse.
* orbitControl();
*
* // Rotate the coordinate system 1/8 turn.
* rotateZ(45);
*
* // Draw a box.
* box();
* }
* </code>
* </div>
*
* <div>
* <code>
* // Click and drag the mouse to view the scene from different angles.
*
* function setup() {
* createCanvas(100, 100, WEBGL);
*
* describe('A white cube rotates slowly against a gray background.');
* }
*
* function draw() {
* background(200);
*
* // Enable orbiting with the mouse.
* orbitControl();
*
* // Rotate the coordinate system a little more each frame.
* let angle = frameCount * 0.01;
* rotateZ(angle);
*
* // Draw a box.
* box();
* }
* </code>
* </div>
*/
p5.prototype.rotateZ = function(angle) {
this._assert3d('rotateZ');
p5._validateParameters('rotateZ', arguments);
this._renderer.rotateZ(this._toRadians(angle));
return this;
};
/**
* Scales the coordinate system.
*
* By default, shapes are drawn at their original scale. A rectangle that's 50
* pixels wide appears to take up half the width of a 100 pixel-wide canvas.
* The `scale()` function can shrink or stretch the coordinate system so that
* shapes appear at different sizes. There are two ways to call `scale()` with
* parameters that set the scale factor(s).
*
* The first way to call `scale()` uses numbers to set the amount of scaling.
* The first parameter, `s`, sets the amount to scale each axis. For example,
* calling `scale(2)` stretches the x-, y-, and z-axes by a factor of 2. The
* next two parameters, `y` and `z`, are optional. They set the amount to
* scale the y- and z-axes. For example, calling `scale(2, 0.5, 1)` stretches
* the x-axis by a factor of 2, shrinks the y-axis by a factor of 0.5, and
* leaves the z-axis unchanged.
*
* The second way to call `scale()` uses a <a href="#/p5.Vector">p5.Vector</a>
* object to set the scale factors. For example, calling `scale(myVector)`
* uses the x-, y-, and z-components of `myVector` to set the amount of
* scaling along the x-, y-, and z-axes. Doing so is the same as calling
* `scale(myVector.x, myVector.y, myVector.z)`.
*
* By default, transformations accumulate. For example, calling `scale(1)`
* twice has the same effect as calling `scale(2)` once. The
* <a href="#/p5/push">push()</a> and <a href="#/p5/pop">pop()</a> functions
* can be used to isolate transformations within distinct drawing groups.
*
* Note: Transformations are reset at the beginning of the draw loop. Calling
* `scale(2)` inside the <a href="#/p5/draw">draw()</a> function won't cause
* shapes to grow continuously.
*
* @method scale
* @param {Number|p5.Vector|Number[]} s amount to scale along the positive x-axis.
* @param {Number} [y] amount to scale along the positive y-axis. Defaults to `s`.
* @param {Number} [z] amount to scale along the positive z-axis. Defaults to `y`.
* @chainable
*
* @example
* <div>
* <code>
* function setup() {
* createCanvas(100, 100);
*
* describe(
* 'Two white squares on a gray background. The larger square appears at the top-center. The smaller square appears at the top-left.'
* );
* }
*
* function draw() {
* background(200);
*
* // Draw a square at (30, 20).
* square(30, 20, 40);
*
* // Scale the coordinate system by a factor of 0.5.
* scale(0.5);
*
* // Draw a square at (30, 20).
* // It appears at (15, 10) after scaling.
* square(30, 20, 40);
* }
* </code>
* </div>
*
* <div>
* <code>
* function setup() {
* createCanvas(100, 100);
*
* describe('A rectangle and a square drawn in white on a gray background.');
* }
*
* function draw() {
* background(200);
*
* // Draw a square at (30, 20).
* square(30, 20, 40);
*
* // Scale the coordinate system by factors of
* // 0.5 along the x-axis and
* // 1.3 along the y-axis.
* scale(0.5, 1.3);
*
* // Draw a square at (30, 20).
* // It appears as a rectangle at (15, 26) after scaling.
* square(30, 20, 40);
* }
* </code>
* </div>
*
* <div>
* <code>
* function setup() {
* createCanvas(100, 100);
*
* describe('A rectangle and a square drawn in white on a gray background.');
* }
*
* function draw() {
* background(200);
*
* // Draw a square at (30, 20).
* square(30, 20, 40);
*
* // Create a p5.Vector object.
* let v = createVector(0.5, 1.3);
*
* // Scale the coordinate system by factors of
* // 0.5 along the x-axis and
* // 1.3 along the y-axis.
* scale(v);
*
* // Draw a square at (30, 20).
* // It appears as a rectangle at (15, 26) after scaling.
* square(30, 20, 40);
* }
* </code>
* </div>
*
* <div>
* <code>
* // Click and drag the mouse to view the scene from different angles.