Last active
October 21, 2020 02:52
-
-
Save motsu0/51f1cb9da34e6de013ec284c87ac95d5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.face-outer{ | |
width: 60%; | |
margin: 0 auto; | |
} | |
.face-outline{ | |
position: relative; | |
border: 1px solid #333; | |
border-radius: 50%; | |
} | |
.face-outline::before{ | |
content: ""; | |
display: block; | |
padding-top: 100%; | |
} | |
.face-weye{ | |
position: absolute; | |
top: 25%; | |
width: 25%; | |
border: 1px solid #333; | |
border-radius: 50%; | |
} | |
.face-weye::before{ | |
content: ""; | |
display: block; | |
padding-top: 100%; | |
} | |
.face-weye.left{ | |
left: 16%; | |
} | |
.face-weye.right{ | |
right: 16%; | |
} | |
.face-beye{ | |
position: absolute; | |
top: 50%; | |
left: 50%; | |
width: 20%; | |
transform: translate(-50%,-50%); | |
background-color: #333; | |
border-radius: 50%; | |
} | |
.face-beye::before{ | |
content: ""; | |
display: block; | |
padding-top: 100%; | |
} | |
.face-mouth{ | |
position: absolute; | |
left: 50%; | |
bottom: 18%; | |
width: 50%; | |
transform: translateX(-50%); | |
border-width: 0 0 1px 0; | |
border-style: solid; | |
border-color: #333; | |
border-radius: 50%; | |
} | |
.face-mouth::before{ | |
content: ""; | |
display: block; | |
padding-top: 50%; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="face-outer"> | |
<div class="face-outline"> | |
<div class="face-weye left"> | |
<div class="face-beye left"></div> | |
</div> | |
<div class="face-weye right"> | |
<div class="face-beye right"></div> | |
</div> | |
<div class="face-mouth"></div> | |
</div> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const max_d = 200; | |
const beye = document.getElementsByClassName('face-beye'); | |
const beye_r = beye[0].clientWidth/2; | |
let eye_pos; | |
window.addEventListener('load',()=>{ | |
eye_pos = [...beye].map(el=>{ | |
const cr = el.getBoundingClientRect(); | |
return [window.pageXOffset + cr.left + el.clientWidth/2, window.pageYOffset + cr.top + el.clientHeight/2]; | |
}); | |
window.addEventListener('mousemove',chase_mouse); | |
}); | |
function chase_mouse(e){ | |
const pos_mouse = [e.pageX, e.pageY]; | |
[...beye].forEach((eye,i)=>{ | |
let d_eye_mouse = [e.pageX - eye_pos[i][0], e.pageY - eye_pos[i][1]]; | |
let d = Math.sqrt(Math.pow(d_eye_mouse[0],2)+Math.pow(d_eye_mouse[1],2)); | |
const cos = d_eye_mouse[0]/d; | |
const sin = d_eye_mouse[1]/d; | |
if(d>max_d) d=max_d; | |
const black_d = (50-100*beye_r/eye.parentElement.clientWidth)*d/max_d; | |
eye.style.left = `${50 + black_d*cos}%`; | |
eye.style.top = `${50 + black_d*sin}%`; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment