-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhippo.js
75 lines (60 loc) · 3.05 KB
/
hippo.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
const mouthSpeed = 0.3;
const easeType = Power2.easeOut;
const mouthOpen = gsap.timeline({ paused: true });
mouthOpen.to('.mouth-back', {duration: mouthSpeed, ease: easeType, y: -70}, 0);
mouthOpen.to('.tongue', {duration: mouthSpeed * 1.5, ease: easeType, y: -70}, 0);
mouthOpen.to('.teeth', {duration: mouthSpeed, ease: easeType, y: -70, scaleY: 1.2}, 0);
mouthOpen.to('.body', {duration: mouthSpeed, ease: easeType, scaleY: 1.06, transformOrigin: 'center bottom'}, 0);
mouthOpen.to('.freckles', {duration: mouthSpeed, ease: easeType, y: -10}, 0);
mouthOpen.to('.ears', {duration: mouthSpeed, ease: easeType, y: 6}, 0);
mouthOpen.to('.eye-right', {duration: mouthSpeed, ease: easeType, x: -2}, 0);
mouthOpen.to('.eye-left', {duration: mouthSpeed, ease: easeType, x: 2}, 0);
mouthOpen.to('.eyes', {duration: mouthSpeed, ease: easeType, y: 2}, 0);
mouthOpen.to('.nostrils', {duration: mouthSpeed, ease: easeType, y: -6}, 0);
// ------------
// Mouse events
// ------------
const button = document.querySelector('button');
button.addEventListener('mouseenter', enterButton);
button.addEventListener('mouseleave', leaveButton);
function enterButton() { mouthOpen.play(); }
function leaveButton() { mouthOpen.reverse(); }
// ----------
// Ear wiggle
// ----------
const earWiggle = gsap.timeline({ paused: true, repeat: 2 });
earWiggle.set('.ear-right', { transformOrigin:"center center" });
earWiggle.to('.ear-right', {duration: 0.1, rotation: 45});
earWiggle.to('.ear-right', {duration: 0.1, rotation: 0});
window.setInterval(() => earWiggle.play(0), 2500);
// ------------
// Eye tracking
// ------------
const eyeRightPupil = document.querySelector('.eye-right-pupil');
const eyeLeftPupil = document.querySelector('.eye-left-pupil');
const eyeLeftInner = document.querySelector('.eye-left-inner');
const innerEyeWidth = eyeLeftInner.getBoundingClientRect().width;
const innerEyeHeight = eyeLeftInner.getBoundingClientRect().height;
const pupilWidth = eyeLeftPupil.getBoundingClientRect().width;
const pupilHeight = eyeLeftPupil.getBoundingClientRect().height;
const xMovement = (innerEyeWidth - pupilWidth)/2;
const yMovement = (innerEyeHeight - pupilHeight)/2;
window.addEventListener('mousemove', updateEyePosition);
function updateEyePosition(e) {
const mousePercentX = e.clientX / document.body.clientWidth;
const mousePercentY = e.clientY / document.body.clientHeight;
const posX = (mousePercentX * 2 - 1) * xMovement;
const posY = (mousePercentY * 2 - 1) * yMovement;
eyeLeftPupil.style.transform = `translate(${posX}px, ${posY}px)`;
eyeRightPupil.style.transform = `translate(${posX}px, ${posY}px)`;
}
const hippoButton = document.querySelector('.hippo-button');
const hippoAnimation = document.querySelector('.hippo-animation');
hippoButton.addEventListener('mouseenter', showHippoAnimation);
hippoButton.addEventListener('mouseleave', hideHippoAnimation);
function showHippoAnimation() {
hippoAnimation.style.display = 'block';
}
function hideHippoAnimation() {
hippoAnimation.style.display = 'none';
}