-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
149 lines (125 loc) · 3.98 KB
/
sketch.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
//Simple star drawing code
//Written by Vittoria Ventura, 2020 / [email protected]
function setup() {
createCanvas(displayWidth, displayHeight);
frameRate(60);
background(0);
smooth();
}
function draw() {
//Set mouse position as centre of drawing
translate(mouseX, mouseY);
//Determine different values to r,g,b
//Map the values that will be used for fill
//r,g,b set for 1st gradient:
let r = map(mouseY,0,windowHeight,1,255);
let g = map(mouseX,0,windowHeight,1,255);
let b = map(mouseY,0,windowWidth,255,1);
//Determine default fill for stars
//Combine the mapped r,g,b values to create a gradient
fill(r, g, b, 50);
noStroke();
//Create different sets of r,g,b mappings
//Each set is a new gradient
//r,g,b set for 2nd gradient:
let r2 = map(mouseY, 0, displayHeight, 255,1);
let g2 = map(mouseX, 0, displayHeight, 1, 255);
let b2 = map(mouseY, 0, displayHeight, 1, 255);
//r,g,b set for 3rd gradient:
let r3 = map(mouseY, 0, displayHeight, 255,1);
let g3 = map(mouseY, 0, displayHeight, 1, 255);
let b3 = map(mouseX, 0, displayHeight, 1, 255);
//r,g,b set for 4th gradient:
let r4 = map(mouseY, 0, displayHeight, 255, 1);
let g4 = map(mouseX, 0, displayWidth, 1, 255);
let b4 = map(mouseX, 0, displayHeight, 255, 1);
//r,g,b set for 5th gradient:
let r5 = map(mouseY, 0, displayHeight, 1, 255);
let g5 = map(mouseX, 0, displayWidth, 1, 255);
let b5 = map(mouseY, 0, displayHeight, 255, 1);
//r,g,b set for 6th gradient:
let r6 = map(mouseY, 0, displayHeight, 255,1);
let g6 = map(mouseY, 0, displayHeight, 1, 255);
let b6 = map(mouseY, 0, displayHeight, 1, 255);
//r,g,b set for 7th gradient:
let r7 = map(mouseY, 0, displayHeight, 1, 255);
let g7 = map(mouseY, 0, displayHeight, 255, 1);
let b7 = map(mouseY, 0, displayHeight, 1, 255);
//r,g,b set for 8th gradient:
let r8 = map(mouseY, 0, displayHeight, 1, 255);
let g8 = map(mouseX, 0, displayWidth, 255, 1);
let b8 = map(mouseY, 0, displayWidth, 255, 1);
//r,g,b set for 9th gradient:
let r9 = map(mouseY, 0, displayHeight, 1, 255);
let g9 = map(mouseY, 0, displayHeight, 1, 255);
let b9 = map(mouseY, 0, displayHeight, 255, 1);
//If certain key is pressed, change fill
//Each number key corresponds to a different fill
if (key === "1") {
fill(r, g, b, 50);
noStroke();
} else if (key === "2") {
fill(r2, g2, b2, 50);
noStroke();
} else if (key === "3") {
fill(r3, g3, b3, 50);
noStroke();
} else if (key === "4") {
fill(r4, g4, b4, 50);
noStroke();
} else if (key === "5") {
fill(r5, g5, b5, 50);
noStroke();
} else if (key === "6") {
fill(r6, g6, b6, 50);
noStroke();
} else if (key === "7") {
fill(r7, g7, b7, 50);
noStroke();
} else if (key === "8") {
fill(r8, g8, b8, 50);
noStroke();
} else if (key === "9") {
fill(r9, g9, b9, 50);
noStroke();
//The "0" key corresponds to a special option
//No fill, just white stroke
} else if (key === "0") {
stroke(255, 50)
noFill()
strokeWeight(2)
}
//If spacebar is pressed, clear canvas
//
if (keyCode === 32) {
createCanvas(displayWidth, displayHeight);
background(0);
}
//If mouse is pressed draw star
if (mouseIsPressed) {
//Map size of star
//Size is small at top, large at bottom
let size = map(mouseY, 0, windowWidth, 10, 300)
//Set the number of points in a star to 5
p = 5;
//Determine measuraments for the star
star(0, 0, size / 2, size, p);
}
}
//Star Shape Function
function star(x, y, radius1, radius2, npoints) {
let angle = TWO_PI / npoints;
let halfAngle = angle / 2.0;
beginShape();
for (let a = 0; a < TWO_PI; a += angle) {
let sx = x + cos(a) * radius2;
let sy = y + sin(a) * radius2;
vertex(sx, sy);
sx = x + cos(a + halfAngle) * radius1;
sy = y + sin(a + halfAngle) * radius1;
vertex(sx, sy);
}
//Make star rotate
rotate(radians(frameCount + mouseX));
endShape(CLOSE);
}