-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
334 lines (271 loc) · 8.94 KB
/
index.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
// let smiling = {};
// let neutral = {};
// let smiled = false;
let start = new Date();
function blink() {
_blinked = true;
const now = new Date();
const timeDiff = (now - start) / 1000; //in s
// // get seconds
const seconds = Math.round(timeDiff);
if(!alert(`YOU LASTED ${seconds} SECONDS!`)){window.location.reload();}
// start = new Date();
if (_timeOut > -1) {
clearTimeout(_timeOut);
}
_timeOut = setTimeout(resetBlink, 150);
}
function resetBlink() {
_blinked = false;
}
function storeFaceShapeVertices(vertices) {
for (var i = 0, l = vertices.length; i < l; i++) {
_oldFaceShapeVertices[i] = vertices[i];
}
}
var _oldFaceShapeVertices = [];
var _blinked = false;
var _timeOut = -1;
// BRFv4DemoMinimal.js defines: var handleTrackingResults = function(brfv4, faces, imageDataCtx)
// Here we overwrite it. The initialization code for BRFv4 should always be similar,
// that's why we put it into its own file.
handleTrackingResults = function(
brfv4, // namespace
faces, // tracked faces
imageDataCtx // canvas context to draw into
) {
// var p0 = new brfv4.Point();
// var p1 = new brfv4.Point();
// var setPoint = brfv4.BRFv4PointUtils.setPoint;
// var calcDistance = brfv4.BRFv4PointUtils.calcDistance;
for (var i = 0; i < faces.length; i++) {
var face = faces[i];
if (
face.state === brfv4.BRFState.FACE_TRACKING_START ||
face.state === brfv4.BRFState.FACE_TRACKING
) {
// simple blink detection
// A simple approach with quite a lot false positives. Fast movement can't be
// handled properly. This code is quite good when it comes to
// staring contest apps though.
// It basically compares the old positions of the eye points to the current ones.
// If rapid movement of the current points was detected it's considered a blink.
var v = face.vertices;
if (_oldFaceShapeVertices.length === 0) storeFaceShapeVertices(v);
var k, l, yLE, yRE;
// Left eye movement (y)
for (k = 36, l = 41, yLE = 0; k <= l; k++) {
yLE += v[k * 2 + 1] - _oldFaceShapeVertices[k * 2 + 1];
}
yLE /= 6;
// Right eye movement (y)
for (k = 42, l = 47, yRE = 0; k <= l; k++) {
yRE += v[k * 2 + 1] - _oldFaceShapeVertices[k * 2 + 1];
}
yRE /= 6;
var yN = 0;
// Compare to overall movement (nose y)
yN += v[27 * 2 + 1] - _oldFaceShapeVertices[27 * 2 + 1];
yN += v[28 * 2 + 1] - _oldFaceShapeVertices[28 * 2 + 1];
yN += v[29 * 2 + 1] - _oldFaceShapeVertices[29 * 2 + 1];
yN += v[30 * 2 + 1] - _oldFaceShapeVertices[30 * 2 + 1];
yN /= 4;
var blinkRatio = Math.abs((yLE + yRE) / yN);
if (blinkRatio > 12 && (yLE > 0.4 || yRE > 0.4)) {
console.log(
'blink ' +
blinkRatio.toFixed(2) +
' ' +
yLE.toFixed(2) +
' ' +
yRE.toFixed(2) +
' ' +
yN.toFixed(2)
);
blink();
}
// Let the color of the shape show whether you blinked.
//var color = 0x00a0ff;
var color = '#00a0ff';
if (_blinked) {
color = '#ffd200';
}
// Face Tracking results: 68 facial feature points.
// draw.drawTriangles(face.vertices, face.triangles, false, 1.0, color, 0.4);
// draw.drawVertices(face.vertices, 2.0, false, color, 0.4);
// brfv4Example.dom.updateHeadline(
// 'BRFv4 - advanced - face tracking - simple blink' +
// 'detection.\nDetects an eye blink: ' +
// (_blinked ? 'Yes' : 'No')
// );
imageDataCtx.strokeStyle = color;
for (var k = 0; k < face.vertices.length; k += 2) {
imageDataCtx.beginPath();
imageDataCtx.arc(
face.vertices[k],
face.vertices[k + 1],
2,
0,
2 * Math.PI
);
imageDataCtx.stroke();
}
storeFaceShapeVertices(v);
// Smile Detection
// setPoint(face.vertices, 48, p0); // mouth corner left
// setPoint(face.vertices, 54, p1); // mouth corner right
// var mouthWidth = calcDistance(p0, p1);
// setPoint(face.vertices, 39, p1); // left eye inner corner
// setPoint(face.vertices, 42, p0); // right eye outer corner
// var eyeDist = calcDistance(p0, p1);
// var smileFactor = mouthWidth / eyeDist;
// smileFactor -= 1.4; // 1.40 - neutral, 1.70 smiling
// if (smileFactor > 0.25) smileFactor = 0.25;
// if (smileFactor < 0.0) smileFactor = 0.0;
// smileFactor *= 4.0;
// if (smileFactor > 0.5) {
// smiling['face' + 1] += 1;
// neutral['face' + 1] = 0;
// if (smiling['face' + 1] > 2) {
// smiled = true;
// nyanTip.style.display = 'none';
// nyanGif.style.display = 'block';
// if (nyanAudio.paused) {
// nyanAudio.fastSeek(5.4);
// nyanAudio.play();
// }
// }
// } else {
// smiling['face' + 1] = 0;
// neutral['face' + 1] += 1;
// if (neutral['face' + 1] > 5) {
// nyanGif.style.display = 'none';
// nyanAudio.pause();
// }
// }
}
}
};
// resize the canvas to match the window
function setsize() {
the_canvas.width = the_canvas_container.clientWidth;
the_canvas_width = the_canvas_container.clientWidth;
the_canvas.height = the_canvas_container.clientHeight;
the_canvas_height = the_canvas_container.clientHeight;
}
// grab a 100-pixel tall horizontal strip and move it left or right by 50px
function effect_tearing() {
var pos_y = parseInt(Math.random() * (the_canvas_height - 100));
the_canvas_ctx.drawImage(
the_canvas,
0,
pos_y,
the_canvas_width,
100,
parseInt(Math.random() * 2) * 100 - 50, // either 50 or -50
pos_y,
the_canvas_width,
100
);
}
// add an image to somewhere inside the canvas
function add_image() {
var rnd_pic = document.getElementById('pic' + parseInt(Math.random() * 18));
if (rnd_pic) {
var img = new Image();
img.onload = function() {
the_canvas_ctx.drawImage(
rnd_pic,
parseInt(Math.random() * (the_canvas_width - rnd_pic.width)),
parseInt(Math.random() * (the_canvas_height - rnd_pic.height))
);
};
img.src = rnd_pic.src;
}
}
// rewind and play one of the audio elements
function do_sound() {
return;
var rnd_snd = document.getElementById('snd' + parseInt(Math.random() * 7));
rnd_snd.currentTime = 0;
rnd_snd.play();
}
// main loop (on interval)
function slowloop() {
if (document.hidden === true) {
return;
} // We musn't be rude
if (Math.random() < 0.1) {
effect_tearing();
}
if (Math.random() < 0.05) {
screen_shake();
}
add_image();
if (devmode) if (Math.random() < 0.1) vertline();
// Chrome is mean and usually doesn't let us autoplay sound anymore :(
// Fun fact, trying to use HTML5 audio causes this function to die when
// run in IE from an "N" edition of windows, which is why this is last
do_sound();
}
// bump the screen side-to-side
function screen_shake() {
the_canvas_container.style.left = (Math.random() < 0.5 ? '-' : '') + '50px';
setTimeout(function() {
the_canvas_container.style.left = '0px';
}, 50);
}
// draw vertical lines of "dead pixels"
function vertline() {
var base_x = parseInt(Math.random() * (the_canvas_width - 10));
for (let i = 0; i < 10; i++) {
the_canvas_ctx.strokeStyle =
'rgb(' +
parseInt(Math.random() * 255) +
',' +
parseInt(Math.random() * 255) +
',' +
parseInt(Math.random() * 255) +
')';
let line_x = base_x + i + Math.random() * 50 - 25;
the_canvas_ctx.beginPath();
the_canvas_ctx.moveTo(line_x + 0.5, 0.5);
the_canvas_ctx.lineTo(line_x + 0.5, 0.5 + the_canvas_height);
the_canvas_ctx.stroke();
}
}
function sw_init() {
the_canvas_container = document.getElementById('canvas_container');
the_canvas = document.getElementById('the_canvas');
the_canvas_ctx = the_canvas.getContext('2d');
setsize();
window.setInterval(slowloop, 100);
}
var the_canvas;
var the_canvas_container;
var the_canvas_ctx;
var the_canvas_height;
var the_canvas_width;
var devmode = false;
if (document.cookie.indexOf('devmode=enable') !== -1) {
devmode = true;
}
window.addEventListener('resize', setsize);
window.addEventListener('load', sw_init);
// onResize = function() {
// // fill whole browser
// var imageData = document.getElementById('_imageData');
// var ww = window.innerWidth;
// var wh = window.innerHeight;
// var s = wh / imageData.height;\
// if (imageData.width * s < ww) {
// s = ww / imageData.width;
// }
// var iw = imageData.width * s;
// var ih = imageData.height * s;
// var ix = (ww - iw) * 0.5;
// var iy = (wh - ih) * 0.5;
// imageData.style.transformOrigin = '0% 0%';
// imageData.style.transform =
// 'matrix(' + s + ', 0, 0, ' + s + ', ' + ix + ', ' + iy + ')';
// };