forked from SuperIlu/DOjS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatp5.js
118 lines (104 loc) · 2.94 KB
/
matp5.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
/*
This file was derived from the p5.js source code at
https://github.com/processing/p5.js
Copyright (c) the p5.js contributors and Andre Seidelt <[email protected]>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
Include('p5');
function setup() {
frameRate(10);
rectMode(CENTER);
test = 0;
}
function draw() {
// matrix tx
if (test == 0) {
var step = frameCount % 20;
background(200);
// Equivalent to translate(x, y);
applyMatrix(1, 0, 0, 1, 40 + step, 50);
rect(0, 0, 50, 50);
} else if (test == 1) {
// matrix scale
var step = frameCount % 20;
background(200);
translate(50, 50);
// Equivalent to scale(x, y);
applyMatrix(1 / step, 0, 0, 1 / step, 0, 0);
rect(0, 0, 50, 50);
} else if (test == 2) {
// matrix rot
var step = frameCount % 20;
var angle = map(step, 0, 20, 0, TWO_PI);
var cos_a = cos(angle);
var sin_a = sin(angle);
background(200);
translate(50, 50);
// Equivalent to rotate(angle);
applyMatrix(cos_a, sin_a, -sin_a, cos_a, 0, 0);
rect(0, 0, 50, 50);
} else if (test == 3) {
// matrix shear
var step = frameCount % 20;
var angle = map(step, 0, 20, -PI / 4, PI / 4);
background(200);
translate(50, 50);
// equivalent to shearX(angle);
var shear_factor = 1 / tan(PI / 2 - angle);
applyMatrix(1, 0, shear_factor, 1, 0, 0);
rect(0, 0, 50, 50);
} else if (test == 4) {
// rotate()
resetMatrix();
background(200);
translate(width / 2, height / 2);
rotate(PI / 3.0);
rect(-26, -26, 52, 52);
} else if (test == 5) {
// shearX()
resetMatrix();
background(200);
translate(width / 2, height / 2);
shearX(PI / 4.0);
rect(0, 0, 30, 30);
} else if (test == 6) {
// shearY()
resetMatrix();
background(200);
translate(width / 2, height / 2);
shearY(PI / 4.0);
rect(0, 0, 30, 30);
} else if (test == 7) {
// scale
resetMatrix();
background(200);
translate(width / 2, height / 2);
rect(30, 20, 50, 50);
scale(0.5, 1.3);
rect(30, 20, 50, 50);
} else if (test == 8) {
// resetMatrix
resetMatrix();
background(200);
translate(50, 50);
applyMatrix(0.5, 0.5, -0.5, 0.5, 0, 0);
rect(0, 0, 20, 20);
// Note that the translate is also reset.
resetMatrix();
rect(0, 0, 20, 20);
}
}
function keyPressed() {
test = int(key) - CharCode('0');
resetMatrix();
}