-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
230 lines (189 loc) · 4.06 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
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
let lightswitch
let lightbulb
let remote
let colorOff, colorOn
let lightLevel
let soundOff, soundOn
function preload() {
soundOff = loadSound('off.mp3')
soundOn = loadSound('on.mp3')
}
function setup() {
createCanvas(windowWidth - 5, windowHeight - 5)
lightswitch = new Lightswitch()
lightbulb = new Lightbulb()
remote = new Remote()
colorOff = color(175)
colorOn = color(255, 255, 225)
lightLevel = 3
}
function draw() {
background(0)
if (lightswitch.on) {
noFill()
strokeWeight(4)
let rMax = map(lightLevel, 1, 3, 400, 1500)
for (let r = lightbulb.r; r < rMax; r+=lightLevel*1.3) {
let amt = map(r, lightbulb.r, rMax, 0.1, 1)
let c = lerpColor(lightbulb.getColor(), color(0), amt)
stroke(c)
circle(lightbulb.x, lightbulb.y, r)
}
}
lightswitch.show()
lightbulb.show()
remote.show()
}
function distance(x1, y1, x2, y2) {
let dx = x1 - x2
let dy = y1 - y2
return sqrt(dx*dx + dy*dy)
}
function mouseClicked() {
if (lightswitch.mouseOver()) {
lightswitch.toggle()
lightLevel = 3
}
if (remote.buttonPlus.mouseOver()) {
if (lightLevel < 3) {
lightLevel++
}
}
if (remote.buttonMinus.mouseOver()) {
if (lightLevel > 1) {
lightLevel--
}
}
}
class Lightswitch {
constructor() {
this.x = width/2 - 200
this.y = height - 150
this.rI = 100
this.rO = 150
this.on = false
}
show() {
strokeWeight(5)
fill(150)
circle(this.x, this.y, this.rO)
strokeWeight(3)
if (this.mouseOver() && mouseIsPressed) {
strokeWeight(5)
}
fill(250)
circle(this.x, this.y, this.rI)
noFill()
strokeWeight(5)
arc(this.x, this.y, this.rI/2, this.rI/2, -PI/4, PI + PI/4)
line(this.x, this.y - this.rI/4, this.x, this.y)
}
toggle() {
lightswitch.on = !lightswitch.on
if (lightswitch.on) {
soundOn.play()
} else {
soundOff.play()
}
}
mouseOver() {
return distance(mouseX, mouseY, this.x, this.y) < this.rI/2
}
}
class Lightbulb {
constructor() {
this.x = width/2
this.y = 175
this.r = 150
}
show() {
if (lightswitch.on) {
fill(this.getColor())
} else {
fill(colorOff)
}
ellipseMode(CENTER)
noStroke()
circle(this.x, this.y, this.r)
noStroke()
fill(150)
rect(this.x, 0, 50, this.y + 50)
arc(this.x, this.y, this.r, this.r, 1.5*PI - 1, 1.5*PI + 1, CHORD)
fill(50)
rect(this.x, 0, 150, 50)
rect(this.x, 0, 75, this.y)
// rect(this.x, this.y -this.r/2, this.r/2, this.r/3, 10)
// strokeWeight(10)
// stroke(125)
// line(this.x, 0, this.x, this.y - 110)
}
getColor() {
return lerpColor(colorOff, colorOn, map(lightLevel, 0, 3, 0, 1))
}
}
class Remote {
constructor() {
this.x = width/2 + 200
this.y = height - 150
this.w = 100
this.h = 200
this.buttonPlus = new ButtonPlus(this.x, this.y - this.h/4, this.w/2, this.w/2)
this.buttonMinus = new ButtonMinus(this.x, this.y + this.h/4, this.w/2, this.w/2)
}
show() {
rectMode(CENTER)
stroke(0)
strokeWeight(5)
fill(250)
rect(this.x, this.y, this.w, this.h, 10)
this.buttonPlus.show()
this.buttonMinus.show()
}
}
class ButtonPlus {
constructor(x, y, w, h) {
this.x = x
this.y = y
this.w = w
this.h = h
}
show() {
fill(150)
stroke(0)
strokeWeight(3)
if (this.mouseOver() && mouseIsPressed) {
strokeWeight(5)
}
rect(this.x, this.y, this.w, this.h, 10)
stroke(0)
strokeWeight(5)
line(this.x - this.w/4, this.y, this.x + this.w/4, this.y)
line(this.x, this.y - this.h/4, this.x, this.y + this.h/4)
}
mouseOver() {
return ((this.x - this.w/2 <= mouseX) && (mouseX <= this.x + this.w/2) && (this.y - this.h/2 <= mouseY) && (mouseY <= this.y + this.h/2))
}
}
class ButtonMinus {
constructor(x, y, w, h) {
this.x = x
this.y = y
this.w = w
this.h = h
}
show() {
fill(150)
stroke(0)
strokeWeight(3)
if (this.mouseOver() && mouseIsPressed) {
strokeWeight(5)
}
rect(this.x, this.y, this.w, this.h, 10)
stroke(0)
strokeWeight(5)
line(this.x - this.w/4, this.y, this.x + this.w/4, this.y)
}
mouseOver() {
return ((this.x - this.w/2 <= mouseX) && (mouseX <= this.x + this.w/2) && (this.y - this.h/2 <= mouseY) && (mouseY <= this.y + this.h/2))
}
}