-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
162 lines (107 loc) · 5.11 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
let randomValue = () =>{
return Math.floor(Math.random() * 255);
}
let theme = document.getElementById('theme')
theme.onclick = ()=>{
value = `rgba(${randomValue()},${randomValue()},${randomValue()})`
document.body.style.backgroundColor = value
theme.style.backgroundColor = value
}
const eye = document.querySelector('.eye');
const pupil = eye.querySelector('.pupil');
document.addEventListener('mousemove', e => {
const mouseX = e.clientX;
const mouseY = e.clientY;
const eyeX = eye.getBoundingClientRect().left + eye.offsetWidth / 2;
const eyeY = eye.getBoundingClientRect().top + eye.offsetHeight / 2;
const deltaX = mouseX - eyeX;
const deltaY = mouseY - eyeY;
// Calculate the distance between the eye element's center and the mouse pointer's position
const distance = Math.sqrt(deltaX ** 2 + deltaY ** 2);
const maxDistance = eye.offsetWidth / 2 - pupil.offsetWidth / 2;
// Calculate the angle between the eye element's center and the mouse pointer's position
const angle = Math.atan2(deltaY, deltaX);
// Calculate the pupil's new position
const pupilX = Math.cos(angle) * Math.min(distance, maxDistance);
const pupilY = Math.sin(angle) * Math.min(distance, maxDistance);
// Update the pupil element's position
pupil.style.transform = `translate(${pupilX}px, ${pupilY}px)`;
});
//2
// Get the eye and pupil elements
const inside = document.querySelector('.inside');
// Listen for the mousemove event
document.addEventListener('mousemove', e => {
// Get the mouse pointer's position
const mouseX = e.clientX;
const mouseY = e.clientY;
// Get the eye element's position
const eyeX = pupil.getBoundingClientRect().left + pupil.offsetWidth / 2;
const eyeY = pupil.getBoundingClientRect().top + pupil.offsetHeight / 2;
// Calculate the difference between the mouse position and the eye position
const deltaX = mouseX - eyeX;
const deltaY = mouseY - eyeY;
// Calculate the distance between the eye element's center and the mouse pointer's position
const distance = Math.sqrt(deltaX ** 2 + deltaY ** 2);
// Calculate the maximum distance the pupil can move
const maxDistance = pupil.offsetWidth / 2 - inside.offsetWidth / 2;
// Calculate the angle between the eye element's center and the mouse pointer's position
const angle = Math.atan2(deltaY, deltaX);
// Calculate the pupil's new position
const pupilX = Math.cos(angle) * Math.min(distance, maxDistance);
const pupilY = Math.sin(angle) * Math.min(distance, maxDistance);
// Update the pupil element's position
inside.style.transform = `translate(${pupilX}px, ${pupilY}px)`;
});
//1
// Get the eye and pupil elements
const eye1 = document.querySelector('.eye1');
const pupil1 = document.querySelector('.pupil1');
// Listen for the mousemove event
document.addEventListener('mousemove', e => {
// Get the mouse pointer's position
const mouseX = e.clientX;
const mouseY = e.clientY;
// Get the eye element's position
const eyeX = eye1.getBoundingClientRect().left + eye.offsetWidth / 2;
const eyeY = eye1.getBoundingClientRect().top + eye.offsetHeight / 2;
// Calculate the difference between the mouse position and the eye position
const deltaX = mouseX - eyeX;
const deltaY = mouseY - eyeY;
// Calculate the distance between the eye element's center and the mouse pointer's position
const distance = Math.sqrt(deltaX ** 2 + deltaY ** 2);
const maxDistance = eye1.offsetWidth / 2 - pupil1.offsetWidth / 2;
// Calculate the angle between the eye element's center and the mouse pointer's position
const angle = Math.atan2(deltaY, deltaX);
// Calculate the pupil's new position
const pupilX = Math.cos(angle) * Math.min(distance, maxDistance);
const pupilY = Math.sin(angle) * Math.min(distance, maxDistance);
// Update the pupil element's position
pupil1.style.transform = `translate(${pupilX}px, ${pupilY}px)`;
});
//2
// Get the eye and pupil elements
const inside1 = document.querySelector('.inside1');
// Listen for the mousemove event
document.addEventListener('mousemove', e => {
// Get the mouse pointer's position
const mouseX = e.clientX;
const mouseY = e.clientY;
// Get the eye element's position
const eyeX = pupil1.getBoundingClientRect().left + pupil.offsetWidth / 2;
const eyeY = pupil1.getBoundingClientRect().top + pupil.offsetHeight / 2;
// Calculate the difference between the mouse position and the eye position
const deltaX = mouseX - eyeX;
const deltaY = mouseY - eyeY;
// Calculate the distance between the eye element's center and the mouse pointer's position
const distance = Math.sqrt(deltaX ** 2 + deltaY ** 2);
// Calculate the maximum distance the pupil can move
const maxDistance = pupil1.offsetWidth / 2 - inside1.offsetWidth / 2;
// Calculate the angle between the eye element's center and the mouse pointer's position
const angle = Math.atan2(deltaY, deltaX);
// Calculate the pupil's new position
const pupilX = Math.cos(angle) * Math.min(distance, maxDistance);
const pupilY = Math.sin(angle) * Math.min(distance, maxDistance);
// Update the pupil element's position
inside1.style.transform = `translate(${pupilX}px, ${pupilY}px)`;
});