-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
356 lines (324 loc) · 10.4 KB
/
script.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/* Face recognition from : https://nenadmarkus.com/p/picojs-intro/demo/ */
console.clear();
let distance = 0;
let isWatching = true;
let pos = {
x: -1,
y: -1
};
let prev_pos = {
x: 0,
y: 0
};
let distanceSinceWatching = 0;
const elDistance = document.querySelector('.distance .total');
const elStart = document.querySelector('.start');
const elHowTo = document.querySelector('.howto');
const elGame = document.querySelector('.game');
const elContainer = document.querySelector('.container');
const elTime = document.querySelector('.timer .time');
const elMovement = document.querySelector('.movement');
const elReplay1 = document.querySelector('.replay1');
const elReplay2 = document.querySelector('.replay2');
const elDead = document.querySelector('.dead');
const elWin = document.querySelector('.win');
const audioDoll = new Audio('https://assets.codepen.io/127738/squid-game-sound.mp3');
const audioDollDuration = 5.433469;
const shotGun = new Audio('https://assets.codepen.io/127738/shotgun.mp3');
shotGun.volume = 0.2;
const sigh = new Audio('https://assets.codepen.io/127738/sigh.mp3');
const MAX_TIME = 60;
const FINISH_DISTANCE = 100;
const IN_GAME_MAX_DISTANCE = 4000;
const MAX_MOVEMENT = 180;
let playing = false;
var mycamvas;
function init () {
var initialized = false;
if (initialized) return;
/*
(1) prepare the pico.js face detector
*/
var update_memory = pico.instantiate_detection_memory(5); // we will use the detecions of the last 5 frames
var facefinder_classify_region = function (r, c, s, pixels, ldim) {
return -1.0;
};
var cascadeurl = "https://raw.githubusercontent.com/nenadmarkus/pico/c2e81f9d23cc11d1a612fd21e4f9de0921a5d0d9/rnt/cascades/facefinder";
fetch(cascadeurl).then(function (response) {
response.arrayBuffer().then(function (buffer) {
var bytes = new Int8Array(buffer);
facefinder_classify_region = pico.unpack_cascade(bytes);
console.log("* cascade loaded");
});
});
/*
(2) get the drawing context on the canvas and define a function to transform an RGBA image to grayscale
*/
var ctx = document.querySelector(".webcam").getContext("2d");
ctx.lineWidth = 4;
ctx.strokeStyle = "#d7327a";
function rgba_to_grayscale(rgba, nrows, ncols) {
var gray = new Uint8Array(nrows * ncols);
for (var r = 0; r < nrows; ++r)
for (var c = 0; c < ncols; ++c)
// gray = 0.2*red + 0.7*green + 0.1*blue
gray[r * ncols + c] =
(2 * rgba[r * 4 * ncols + 4 * c + 0] +
7 * rgba[r * 4 * ncols + 4 * c + 1] +
1 * rgba[r * 4 * ncols + 4 * c + 2]) /
10;
return gray;
}
/*
(3) this function is called each time a video frame becomes available
*/
var processfn = function (video, dt) {
// render the video frame to the canvas element and extract RGBA pixel data
ctx.drawImage(video, 0, 0);
var rgba = ctx.getImageData(0, 0, 640, 480).data;
// prepare input to `run_cascade`
image = {
pixels: rgba_to_grayscale(rgba, 480, 640),
nrows: 480,
ncols: 640,
ldim: 640
};
params = {
shiftfactor: 0.1, // move the detection window by 10% of its size
minsize: 100, // minimum size of a face
maxsize: 1000, // maximum size of a face
scalefactor: 1.1 // for multiscale processing: resize the detection window by 10% when moving to the higher scale
};
// run the cascade over the frame and cluster the obtained detections
// dets is an array that contains (r, c, s, q) quadruplets
// (representing row, column, scale and detection score)
dets = pico.run_cascade(image, facefinder_classify_region, params);
dets = update_memory(dets);
dets = pico.cluster_detections(dets, 0.2); // set IoU threshold to 0.2
// draw detections
if (dets.length) {
const det = dets[0];
// check the detection score
// if it's above the threshold, draw it
// (the constant 50.0 is empirical: other cascades might require a different one)
if (det[3] > 50.0) {
prev_pos.x = pos.x;
prev_pos.y = pos.y;
pos.x = det[1];
pos.y = det[0];
let _dist = Math.hypot(pos.x - prev_pos.x, pos.y - prev_pos.y);
// If not watching, increase the distance from the face position
if (prev_pos.x === -1) {
_dist = 0;
}
if (!isWatching) {
distance += _dist;
/* If user reached the end */
if (distance > IN_GAME_MAX_DISTANCE) {
reachedEnd();
}
} else {
distanceSinceWatching += _dist;
if (distanceSinceWatching > MAX_MOVEMENT) {
dead();
}
}
let formattedDistance = '' + Math.round((distance / IN_GAME_MAX_DISTANCE) * FINISH_DISTANCE);
if (formattedDistance.length === 1) {
formattedDistance = '00' + formattedDistance;
} else if (formattedDistance.length == 2) {
formattedDistance = '0' + formattedDistance;
}
elDistance.innerHTML = formattedDistance;
let movingDistance = Math.floor(distanceSinceWatching / MAX_MOVEMENT * 100);
movingDistance = '' + Math.min(100, movingDistance);
if (movingDistance.length === 1) {
movingDistance = '0' + movingDistance;
}
elMovement.innerHTML = `${movingDistance}%`;
ctx.beginPath();
ctx.arc(det[1], det[0], det[2] / 2, 0, 2 * Math.PI, false);
ctx.stroke();
}
}
updateTimer(MAX_TIME - (Date.now() - mycamvas.startTime) / 1000);
/* Check if reached timeout */
if ((Date.now() - mycamvas.startTime) / 1000 > MAX_TIME) {
timeOut();
}
};
/*
(4) instantiate camera handling (see https://github.com/cbrandolino/camvas)
*/
mycamvas = new camvas(ctx, processfn, () => {
playing = true;
updateWatching();
});
/*
(5) it seems that everything went well
*/
initialized = true;
}
elStart.addEventListener('click', () => {
init();
elContainer.classList.add('is-playing');
elHowTo.classList.remove('is-visible');
});
function reachedEnd () {
watchingTween.kill();
sigh.currentTime = 0;
sigh.play();
audioDoll.pause();
mycamvas.stop();
playing = false;
elWin.classList.add('is-visible');
elContainer.classList.remove('is-playing');
}
function timeOut () {
watchingTween.kill();
audioDoll.pause();
shotGun.currentTime = 0;
shotGun.play();
mycamvas.stop();
playing = false;
elDead.classList.add('is-visible');
elContainer.classList.remove('is-playing');
}
function dead () {
watchingTween.kill();
audioDoll.pause();
shotGun.currentTime = 0;
shotGun.play();
mycamvas.stop();
playing = false;
elDead.classList.add('is-visible');
elContainer.classList.remove('is-playing');
}
let watchingTween = null;
function updateWatching () {
if (!playing) return;
isWatching = !isWatching;
let duration = Math.random() * 3.5 + 2.5;
if (isWatching) {
duration = Math.random() * 2 + 2;
}
if (!isWatching) {
audioDoll.currentTime = 0;
audioDoll.playbackRate = (audioDollDuration - 0.5) / duration;
audioDoll.play();
}
gsap.to(head.rotation, {
y: isWatching ? 0 : -Math.PI,
duration: 0.4
});
watchingTween = gsap.to({}, {
duration: duration,
onComplete: updateWatching
});
if (!isWatching) {
distanceSinceWatching = 0;
}
}
function updateTimer (timeLeft) {
let min = '' + Math.floor(timeLeft / 60);
if (min.length === 1) {
min = '0' + min;
}
let sec = '' + Math.floor(timeLeft % 60);
if (sec.length === 1) {
sec = '0' + sec;
}
if (timeLeft < 0) {
min = '00';
sec = '00';
}
elTime.innerHTML = `${min}:${sec}`;
}
/* THREEJS PART */
const scene = new THREE.Scene();
let sceneWidth = 0;
if (window.innerWidth / window.innerHeight > 1.9) {
sceneWidth = window.innerWidth * 0.6;
} else {
sceneWidth = window.innerWidth * 0.95;
}
let sceneHeight = sceneWidth / 2;
const camera = new THREE.PerspectiveCamera(75, sceneWidth / sceneHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer({
alpha: true,
antialias: true
});
renderer.setSize(sceneWidth, sceneHeight);
elGame.appendChild(renderer.domElement);
camera.position.y = 2.8;
camera.position.z = 11;
// camera.lookAt(new THREE.Vector3(0, -2, 0));
const light = new THREE.HemisphereLight(0xffffbb, 0x080820, 1);
scene.add(light);
const light2 = new THREE.AmbientLight(0x404040, 1.2);
scene.add(light2);
const spotLight = new THREE.SpotLight(0xffffff);
spotLight.position.set(100, 1000, 100);
scene.add( spotLight );
let head = new THREE.Group();
scene.add(head);
const loader = new THREE.GLTFLoader();
loader.load(
'https://assets.codepen.io/127738/Squid_game_doll.gltf',
function (gltf) {
scene.add(gltf.scene);
head.add(gltf.scene.children[0].children[0].children[0].children[1]);
head.add(gltf.scene.children[0].children[0].children[0].children[1]);
head.add(gltf.scene.children[0].children[0].children[0].children[2]);
head.children[0].position.y = -8;
head.children[1].position.y = -8;
head.children[2].position.y = -8;
head.children[0].position.z = 1;
head.children[1].position.z = 1;
head.children[2].position.z = 1;
head.children[0].scale.setScalar(1);
head.children[1].scale.setScalar(1);
head.children[2].scale.setScalar(1);
head.position.y = 8;
head.position.z = -1;
elStart.classList.add('is-ready');
},
function (xhr) {console.log((xhr.loaded / xhr.total * 100) + '% loaded');},
function (error) {console.log( 'An error happened', error);}
);
renderer.setAnimationLoop(renderWebGL);
function renderWebGL () {
renderer.render(scene, camera);
}
/* On Resize */
function onWindowResize(){
if (window.innerWidth / window.innerHeight > 1.9) {
sceneWidth = window.innerWidth * 0.6;
} else {
sceneWidth = window.innerWidth * 0.95;
}
sceneHeight = sceneWidth / 2;
camera.aspect = sceneWidth / sceneHeight;
camera.updateProjectionMatrix();
renderer.setSize(sceneWidth, sceneHeight);
}
window.addEventListener('resize', onWindowResize, false);
onWindowResize();
function replay () {
elContainer.classList.add('is-playing');
elDead.classList.remove('is-visible');
elWin.classList.remove('is-visible');
distance = 0;
isWatching = true;
distanceSinceWatching = 0;
playing = true;
updateWatching();
mycamvas.startTime = Date.now();
mycamvas.play();
}
elReplay1.addEventListener('click', () => {
replay();
});
elReplay2.addEventListener('click', () => {
replay();
});