-
Notifications
You must be signed in to change notification settings - Fork 0
/
firefight.html
218 lines (169 loc) · 6.45 KB
/
firefight.html
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
<!doctype html>
<html lang="en">
<head>
<title>Firefight</title>
<meta charset="UTF-8">
<style>
body {
font-family: sans-serif;
font-size: 22px;
font-weight: bold;
color: #00B399;
}
</style>
<script>
window.addEventListener('load', eventWindowLoaded, false);
function eventWindowLoaded() {
var audioElement = document.getElementById('theAudio');
audioElement.addEventListener('canplaythrough',audioLoaded,false);
// audioElement.addEventListener('progress', updateLoadingStatus,false);
audioElement.load();
}
function audioLoaded() {
canvasApp();
}
function canvasApp() { // ************************************************* canvasApp()
var theCanvas = document.getElementById('canvasOne');
var context = theCanvas.getContext('2d');
var audioElement = document.getElementById('theAudio');
var infoCanvas = document.getElementById('canvasTwo');
var infoContext = infoCanvas.getContext('2d');
var message;
var sky_y = 0;
var earth_y = 180;
// * * * * Asteroids setup
var asteroids = new Array();
for (i=0; i < (Math.floor(Math.random()*10))+6; i++) { // how many asteroids?
tempx = Math.floor(Math.random()*600)+600;
tempy = Math.floor(Math.random()*200)+100; // this will need to change to like minus 400 or something to start off screen
speed = Math.floor(Math.random()*4);
trajectory = Math.floor(Math.random()*2);
asteroids.push({x:tempx, y:tempy, speed:speed, trajectory:trajectory});
} // * * * End Asteroids setup
// console.log(asteroids);
var liveScenes = new Array(); // as each scene fires it will be added to the liveScenes array so we know to update it
var scenes = new Array(); // remember to push scenes in reverse order!
var start,twelve,taylor,end; // initialize vars for calling each scene
scenes.push({start:120, text:"End of the line.", timeFor:end});
scenes.push({start:22, text:"Taylor Swift time.", timeFor:taylor});
scenes.push({start:12, text:"Twelve seconds in.", timeFor:twelve});
scenes.push({start:0, text:"Beginning of the line.", timeFor:start});
function updateScene(scene){
switch(scene){
case 0:
sky_y-=2; // move sky gradient up
break;
case 1:
if (earth_y > 0) {
earth_y--; // move earth planet up
}
break;
case 2:
for (i=0; i<asteroids.length;i++) { // do some shit with asteroids
asteroids[i].x -= 1 + asteroids[i].speed;
asteroids[i].y += 2 + asteroids[i].trajectory;
}
default:
console.log('default');
}
}
function drawScreen(){ // ********************************************* drawScreen()
// stage setup
// backgrounds
context.fillStyle="#000";
context.fillRect(0,0,theCanvas.width, theCanvas.height);
infoContext.fillStyle="#ffa";
infoContext.fillRect(0,0,infoCanvas.width,infoCanvas.height);
//boxes
context.strokeStyle = "#ccc";
context.strokeRect(0,0,theCanvas.width, theCanvas.height);
infoContext.strokeStyle = "#000";
infoContext.strokeRect(5,5,infoCanvas.width-10,infoCanvas.height-10);
// text
infoContext.fillStyle="#000";
infoContext.fillText("Current Time: " + audioElement.currentTime, 20, 40);
infoContext.fillText("Can play OGG: " + audioElement.canPlayType("audio/ogg") + "... Can play WebM: " + audioElement.canPlayType("audio/webm") + "... Can play WAV: " + audioElement.canPlayType("audio/wav"), 20, 80);
infoContext.fillText("Playing: " + audioElement.currentSrc, 20, 100);
infoContext.fillText(message, 20, 140);
// end setup
// gradient for sky
var gr = context.createLinearGradient(0,sky_y,0,theCanvas.height*5);
gr.addColorStop(0,'rgb(0,0,0)');
gr.addColorStop(0.25, 'rgb(0,0,0)');
gr.addColorStop(0.4, 'rgb(100,10,10)');
gr.addColorStop(0.6, 'rgb(100,30,150)');
gr.addColorStop(0.8, 'rgb(50,20,255)');
gr.addColorStop(1,'rgb(50,10,255)');
context.fillStyle = gr;
context.fillRect(0,0,theCanvas.width,theCanvas.height);
// earth shape
context.fillStyle = "#586";
context.beginPath();
context.moveTo(0,theCanvas.height + earth_y);
context.lineTo(theCanvas.width,theCanvas.height + earth_y);
context.lineTo(theCanvas.width,theCanvas.height-100 + earth_y);
context.quadraticCurveTo(theCanvas.width/2,theCanvas.height-180 + earth_y,0,theCanvas.height-100 + earth_y);
context.lineTo(0,theCanvas.height - earth_y);
context.closePath();
context.fill();
// asteroids
function generateAsteroids() {
for (i=0; i < asteroids.length; i++) {
context.beginPath();
context.shadowOffsetX = 5;
context.shadowOffsetY = -5;
myFillColor = Math.floor(Math.random()*100)+150;
// console.log(myShadowColor);
context.shadowColor = "rgb(100,100,150)";
context.shadowBlur = 10;
context.fillStyle = "rgb("+myFillColor+","+myFillColor+","+myFillColor+")";
context.arc(asteroids[i].x,asteroids[i].y,5,0,360*Math.PI/180, false);
context.fill();
context.closePath();
}
}
generateAsteroids();
var t = Math.floor(audioElement.currentTime);
var scene = scenes.pop();
if (t >= scene.start) { // This is sort of the main control loop for the whole thing. ?
message = scene.text;
liveScenes.push(scene);
} else {
scenes.push(scene); // if its not time for that scene yet we need to put it back on the stack...
}
for (i=0; i < liveScenes.length; i++) {
updateScene(i);
}
} // ************ End drawScreen()
function gameLoop() {
window.setTimeout(gameLoop,20);
drawScreen();
}
gameLoop();
audioElement.play();
} // ************************************************************************************** End canvasApp()
</script>
</head>
<body>
<div id="top">
FIREFIGHT
<a href="http://jennyowenyoungs.bandcamp.com/album/exhibit">Jenny Owen Youngs - Exhibit</a>
</div>
<div style="position:absolute; top:50px; left:50px;">
<canvas id="canvasOne" width="1280" height="720">
Your browser is old as fuck. <a href="https://support.google.com/chrome/answer/95346?hl=en" target="_blank">Download Chrome please.</a>
</canvas>
</div>
<div style="position:absolute; top:820px; left:50px;">
<canvas id="canvasTwo" width="1280" height="200">
</canvas>
</div>
<div id="div_audio">
<audio id="theAudio" style="display:none">
<source src="outfile.webm" type="audio/webm" />
<source src="outfile.ogg" type="audio/ogg" />
<source src="outfile.wav" type="audio/wav" />
</audio>
</div>
</body>
</html>