-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
53 lines (50 loc) · 1.71 KB
/
Copy pathmain.js
File metadata and controls
53 lines (50 loc) · 1.71 KB
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
alert(
"- Desktop users try moving your mouse across the screen \n- Mobile users try to swipe your finger across the screen"
);
// Selecting the eye div
let eye_ref = document.querySelectorAll(".eye");
//mousemove for devices with mouse and touchmove for touchscreen devices
let events = ["mousemove", "touchmove"];
// Check for touch screen
function is_touch_device() {
try {
document.createEvent("TouchEvent");
return true;
} catch (e) {
return false;
}
}
//Same function for both events
events.forEach((eventType) => {
document.body.addEventListener(eventType, (event) => {
eye_ref.forEach((eye) => {
/*
getBoundingClientRect() method returns the position relative to the viewport.
*/
let eyeX = eye.getBoundingClientRect().left + eye.clientWidth / 2;
let eyeY = eye.getBoundingClientRect().top + eye.clientHeight / 2;
/*
ClientX and Client Y return the position of client's cursor from top left of screen
*/
try{
var x = !is_touch_device()
? event.clientX
: event.touches[0].clientX;
var y = !is_touch_device()
? event.clientY
: event.touches[0].clientY;
}catch(e){
}
/*
Subtract x position of mouse from x position of eye
and y position of mouse from y position of eye
then we use atan2 (returns angle in radians)
*/
let radian = Math.atan2(x - eyeX, y - eyeY);
// Convert Radians to Degrees
let rotationDegrees = radian * (180 / Math.PI) * -1 + 180;
// Rotate the eye
eye.style.transform = "rotate(" + rotationDegrees + "deg)";
});
});
});