-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
63 lines (58 loc) · 1.81 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
window.saveDataAcrossSessions = true;
const LOOK_DELAY = 1000; // ms
const LEFT_CUTOFF = window.innerWidth / 4;
const RIGHT_CUTOFF = window.innerWidth - window.innerWidth / 4;
let lookDirection = null;
let startLookTime = Number.POSITIVE_INFINITY; // ms
let imageElement = getNewImage(); // get the image element
let nextImageElement = getNewImage(true); // get the image element
webgazer
.setGazeListener((data, timestamp) => {
//console.log(data, timestamp);
if (data == null || lookDirection === "STOP") return;
if (
data.x < LEFT_CUTOFF &&
lookDirection !== "LEFT" &&
lookDirection !== "RESET"
) {
startLookTime = timestamp;
lookDirection = "LEFT";
} else if (
data.x > RIGHT_CUTOFF &&
lookDirection !== "RIGHT" &&
lookDirection !== "RESET"
) {
startLookTime = timestamp;
lookDirection = "RIGHT";
} else if (data.x >= LEFT_CUTOFF && data.x <= RIGHT_CUTOFF) {
startLookTime = Number.POSITIVE_INFINITY;
lookDirection = null;
}
if (startLookTime + LOOK_DELAY < timestamp) {
//console.log("looked at image");
if (lookDirection === "LEFT") {
imageElement.classList.add("left");
} else {
imageElement.classList.add("right");
}
startLookTime = Number.POSITIVE_INFINITY;
lookDirection = "STOP";
setTimeout(() => {
imageElement.remove();
nextImageElement.classList.remove("next");
imageElement = nextImageElement;
nextImageElement = getNewImage(true);
lookDirection = "RESET";
}, 200);
}
})
.begin();
function getNewImage(next = false) {
const img = document.createElement("img");
img.src = "https://picsum.photos/1000?" + Math.random();
if (next) {
img.classList.add("next");
}
document.body.append(img);
return img;
}