-
Notifications
You must be signed in to change notification settings - Fork 0
/
projector.js
239 lines (204 loc) · 8.86 KB
/
projector.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
/*var projectormode=1;
var ploopcount=2
var projectorcount2=0
var projectorbackground="black"
function makebeep(f) {
var playnote=true
if (playnote==true) {
context1 = new AudioContext()
o = context1.createOscillator()
frequency = f
o.frequency.value = frequency
o.type = "sine"
g = context1.createGain()
o.connect(g)
g.connect(context1.destination)
o.start(0)
}
return o
}
function stopbeep(o) {
o.frequency.value=0.1
o.volume="0"
setTimeout(function () {o.stop()}, 50)
}
var shadows=[]
var wall1color="red"
var bordersize;
var wall2color="blue"
function setborder(pixels) {
bordersize=pixels
var wcolor1=getRGB(wall1color)
var wcolor2=getRGB(wall2color)
var pdiv=document.getElementById("projectordiv")
pdiv.style.borderLeft=pixels + "px solid rgb(" + Math.floor(wcolor1[0] / 2) + "," + Math.floor(wcolor1[1] / 2) + "," + Math.floor(wcolor1[2] / 2) + ")"
pdiv.style.borderTop=pixels + "px solid rgb(" + Math.floor(wcolor2[0] / 2) + "," + Math.floor(wcolor2[1] / 2) + "," + Math.floor(wcolor2[2] / 2) + ")"
pdiv.style.width=(window.innerWidth - pixels) + "px"
pdiv.style.height=(window.innerHeight - pixels) + "px"
document.getElementById("projectorborder").style.borderLeft=pixels + "px solid rgb(" + Math.floor(wcolor1[0] / 2) + "," + Math.floor(wcolor1[1] / 2) + "," + Math.floor(wcolor1[2] / 2) + ")"
document.getElementById("projectorborder").style.borderTop=pixels + "px solid rgb(" + Math.floor(wcolor2[0] / 2) + "," + Math.floor(wcolor2[1] / 2) + "," + Math.floor(wcolor2[2] / 2) + ")"
document.getElementById("projectorborder").style.width=(window.innerWidth - pixels) + "px"
document.getElementById("projectorborder").style.height=(window.innerHeight - pixels) + "px"
document.getElementById("projectorborder").style.zIndex="2"
}
function projecttext(text, only=false) {
document.getElementById("textonly").innerHTML=text
if (only==true) {
document.getElementById("maindiv").style.display="none"
document.body.style.backgroundColor="black"
}
setTimeout(blink, 250)
document.getElementById("textonly").parentElement.style.display="block"
}
function blink() {
document.getElementById("textonly").style.color="blue"
setTimeout(blink2, 250)
}
function blink2() {
document.getElementById("textonly").style.color="red"
setTimeout(blink3, 250)
}
function blink3() {
document.getElementById("textonly").style.color="green"
setTimeout(blink, 250)
}
function projector() {
var pdiv=document.getElementById("projectordiv")
pdiv.style.display="block"
if (projectormode==0) {
//Show the elements quickly one at a time --> colors don't blend to each other
if (Math.floor(projectorcount2 / 2) * 2==projectorcount2) {
//Show background
pdiv.style.background=projectorbackground
projectorcount2++
pdiv.innerHTML=""
}else{
//Hide background, show foreground element
pdiv.style.background="black"
if (projectorcount < projectorElements.length) {
pdiv.innerHTML=""
//Add the elements
for (var i=0; i < ploopcount; i++) {
var num=projectorcount+i
if (num < projectorElements.length) {
var element=projectorElements[num]
pdiv.appendChild(element)
}
var num=projectorcount-i
if (num > -1) {
var element=projectorElements[num]
pdiv.appendChild(element)
}
}
projectorcount2++
projectorcount++
}else {
projectorcount=0
projectorcount2=0
}}
}else if (projectormode==1) {
pdiv.style.background=projectorbackground
//A SPECIAL MODE: show all the elements to at the same time --> CAUSES THE COLORS TO BLEND TO EACH OTHER
//Delete shadows
var allelements=pdiv.getElementsByClassName("shadow")
for (var i=0; i <allelements.length; i++) {
pdiv.removeChild(allelements[i])
}
var allelements=pdiv.getElementsByTagName("div")
for (var i=0; i < projectorElements.length; i++) {
var createnew=false
if (allelements.length==0) {
createnew=true
}else if (pdiv.contains(projectorElements[i])==false) {
createnew=true
}
if (createnew==true) {
//If element doesn't exist yet
projectorElements[i].style.zIndex="5"
//Make the element visible
pdiv.appendChild(projectorElements[i])
//Calculate the background brightness
}
var bg=projectorElements[i].style.backgroundColor
var brightness=getBrightness(bg)
//Calculate the size depending on the brigthness
var ogwidth=projectorElements[i].offsetWidth
var ogheight=projectorElements[i].offsetHeight
var brightness=765-brightness
projectorElements[i].style.border="none"
projectorElements[i].style.width=ogwidth + "px"
projectorElements[i].style.height=ogheight + "px"
var section=brightness / 765
projectorElements[i].style.width=(projectorElements[i].offsetWidth /2 + projectorElements[i].offsetWidth /4* section) +"px"
projectorElements[i].style.height=(projectorElements[i].offsetHeight /2+projectorElements[i].offsetHeight /4* section) +"px"
//Set the actual borders
projectorElements[i].style.borderLeft=((ogwidth - projectorElements[i].offsetWidth)/ 2) +"px solid black"
projectorElements[i].style.borderRight=((ogwidth - projectorElements[i].offsetWidth)) +"px solid black"
projectorElements[i].style.borderTop=((ogheight - projectorElements[i].offsetHeight)/ 2) +"px solid black"
projectorElements[i].style.borderBottom=((ogheight - projectorElements[i].offsetHeight)) +"px solid black"
//alert("Original height: " + ogheight + "\nOriginal width: " +ogwidth + "\nNew height: " + projectorElements[i].offsetHeight + "\nNew width: " + projectorElements[i].offsetWidth)
var rect = projectorElements[i].getBoundingClientRect()
//Calculate shadow widths
bordersizex=rect.width / 4
bordersizey=rect.height / 4
//Set the shadows
var newelement=document.createElement("div")
newelement.style=projectorElements[i].style
newelement.style.position="absolute"
newelement.style.width=(bordersizex * 2 + rect.width) + "px"
newelement.style.height=(bordersizey * 2 + rect.height) + "px"
newelement.style.backgroundColor="black"
newelement.style.top=(rect.top - bordersizey-bordersize) + "px"
newelement.style.left=(rect.left - bordersizex-bordersize) + "px"
newelement.className="shadow"
newelement.style.zIndex="3"
pdiv.appendChild(newelement)
}
//Delete "deleted" elements
var allelements=pdiv.getElementsByTagName("div")
var i2=0
for (var i=0; i < allelements.length; i++) {
//If the element is not a shadow
if (allelements[i].className != "shadow") {
//If projector elements does not contain an element in projectordiv
if (projectorElements.includes(allelements[i])==false) {
//Delete element
pdiv.removeChild(allelements[i])
}
}
}
}
}
function getBrightness(bg) {
if (bg.substring(0, "rgb(".length)=="rgb(") {
var bg=bg.substring("rgb(".length, bg.length - 1).split(",")
var brightness=Number(bg[0]) + Number(bg[1]) +Number(bg[2])
}else if (bg != undefined) {
//Get the RGB value of the color
var rgb=getRGB(bg)
if (rgb==-1) {
return -1
}
var brightness=rgb[0] + rgb[1] + rgb[2]
}else{
brightness=-1
}
return brightness
}
function getRGB(bg) {
if (bg.substring(0, "rgb(".length)=="rgb(") {
var bg=bg.substring("rgb(".length, bg.length - 1).split(",")
return [Number(bg[0]), Number(bg[1]),Number(bg[2])]
}else if (bg != undefined) {
//Put the color in canvas
var canvas=document.getElementById("colorcanvas")
var ctx=canvas.getContext("2d")
ctx.fillStyle = bg;
ctx.fillRect(0, 0, 1, 1);
//Get the color back when canvas has the color rgb
var imgData = ctx.getImageData(0, 0, 1, 1);
return [Number(imgData.data[0]), Number(imgData.data[1]), Number(imgData.data[2]) ]
}else{
return -1
}
}*/