-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
198 lines (189 loc) · 4.79 KB
/
index.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
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>MediaStream Test</title>
</head>
<body>
<canvas style="border:1px solid;"></canvas>
<p>Enable your webcam and shine a flashlight (like a phone flashlight) in front of the camera</p>
<p>Move the light around to control your attack and slash the objects that come on screen</p>
<script src="game.js"></script>
<script>
let c = document.querySelector("canvas");
let f = c.getContext('2d');
let v = document.createElement("video");
let constraints =
{
audio: false,
video: {
facingMode: {
ideal: "environment"
}
}
};
navigator.mediaDevices.getUserMedia(constraints)
.then( (stream)=>{
window.stream = stream;
try {
v.srcObject = stream;
} catch(error){
v.src = URL.createObjectURL(stream);
}
v.setAttribute('autoplay', '1');
c.width = stream.getVideoTracks()[0].getSettings().width;
c.height = stream.getVideoTracks()[0].getSettings().height;
f.translate(c.width, 0);
f.scale(-1, 1);
gameloop = setInterval(()=>{update(c, f, v)}, 1000/30);
})
.catch( (err)=>{
console.log(err);
});
function update(canvas, ctx, video){
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(video, 0, 0);
if(coi !== null){
nodes.push({x: t(coi.x-20), y: coi.y, age: 0});
}
coi = centerOfIntensity(c, f, 254);
cursor = {x: t(coi.x-10), y: coi.y-10, w:20, h:20};
for(let i=0;i<nodes.length;i++){
nodes[i].age++;
if(nodes[i].age > maxNodeAge){
nodes.splice(i, 1);
}
}
//////////////Game Logic///////////////
switch(state){
case 0:
if(coi.y > canvas.height*0.75){
state = 1;
}
break;
case 1:
entityList = cleanList(entityList);
for(let i=0;i<entityList.length;i++){
entityList[i].index = i;
}
if(spawnTimer >= spawnDelay){
spawnBox(canvas);
spawnTimer = 0;
if(spawnDelay > 30){
spawnDelay -= 0.5;
}
}
for(let entity of entityList){
if(entity !== null){
entity.kinematics(canvas);
entity.collision(cursor);
}
}
spawnTimer++;
break;
case 2:
entityList = [];
if(loseScreenTimer < 1 && coi.y > canvas.height*0.75){
state = 1;
let entityList = [];
score = 0;
spawnDelay = 90;
spawnTimer = 90;
nodes = [];
loseScreenTimer = 30;
}
loseScreenTimer--;
break;
default:
break;
}
/////////////Game Logic End////////////
ctx.globalAlpha = 0.7;
ctx.fillStyle = "black";
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.globalAlpha = 1;
drawNodeList(f);
ctx.fillStyle="hsl(220, 50%, 50%)";
ctx.fillRect(t(coi.x-10), coi.y-10, 20, 20);
switch(state){
case 0:
ctx.save();
ctx.translate(c.width, 0);
ctx.scale(-1, 1);
ctx.fillStyle = "hsl(200, 70%, 80%)";
ctx.font = "50px Courier";
ctx.fillText("LIGHT SLICER", 145, 100);
ctx.fillStyle = "hsl(180, 50%, 50%)";
ctx.font = "20px Courier";
ctx.fillText("Shine a light in front of the", 150, 150);
ctx.fillText("camera and slice the things", 160, 180);
ctx.globalAlpha = 0.7;
ctx.fillStyle = "gold";
ctx.fillRect(0, canvas.height*0.75, canvas.width, canvas.height*0.25);
ctx.globalAlpha = 1;
ctx.fillStyle="black"
ctx.font = "20px Courier";
ctx.fillText("Slice here to begin", 200, 430);
ctx.restore();
break;
case 1:
ctx.fillStyle = "white";
ctx.save();
ctx.translate(c.width, 0);
ctx.scale(-1, 1);
ctx.font = "20px Courier";
ctx.fillText(`Score: ${score}`, 15, 30);
ctx.restore();
for(let entity of entityList){
//console.log(entity.color);
if(entity !== null){
entity.draw(ctx);
}
}
break;
case 2:
ctx.save();
ctx.translate(c.width, 0);
ctx.scale(-1, 1);
ctx.fillStyle = "hsl(10, 70%, 80%)";
ctx.font = "50px Courier";
ctx.fillText("YOU FAILED", 170, 100);
ctx.fillStyle = "hsl(20, 50%, 50%)";
ctx.font = "30px Courier";
ctx.fillText(`Score: ${score}`, 170, 150);
ctx.globalAlpha = 0.7;
ctx.fillStyle = "salmon";
ctx.fillRect(0, canvas.height*0.75, canvas.width, canvas.height*0.25);
ctx.globalAlpha = 1;
ctx.fillStyle="black"
ctx.font = "20px Courier";
ctx.fillText("Slice here to restart", 190, 430);
ctx.restore();
break;
default:
break;
}
}
function t(x){ return -1*x+c.width};
function centerOfIntensity(c, ctx, threshold){
frame = ctx.getImageData(0, 0, c.width, c.height).data;
let weightedSumX = 0, weightedSumY = 0, intensitySum = 0;
for(let i=0;i<frame.length;i+=4){
let intensity = (frame[i]+frame[i+1]+frame[i+2])/3;
if( intensity >= threshold ){
weightedSumX += intensity*((i/4)%c.width);
weightedSumY += intensity*Math.floor((i/4)/c.width);
intensitySum += intensity;
}
}
return {x: weightedSumX/intensitySum , y: weightedSumY/intensitySum};
}
window.onkeydown = (event)=>{
let key = event.which || event.charCode;
if(state === 1 && key === 32){
spawnBox(c);
}
}
</script>
</body>
</html>