eye.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. window.onload = function () {
  2. const body = document.getElementById('body');
  3. const lang = document.getElementById('lang-chooser');
  4. const eye = document.getElementById('logo');
  5. const logo = document.getElementById('alucho-logo');
  6. const iris = document.getElementById('iris');
  7. const pupil = document.getElementById('pupil');
  8. // #iris cx attribute animation settings (relative to #alucho-logo i.e. absolute inside the svg)
  9. const istart = 66;
  10. const iend = 126;
  11. const ilength = iend - istart;
  12. // #pupil cx attribute animation settings (relative to #iris cx)
  13. const pstart = -17;
  14. const pend = 17;
  15. const plength = pend - pstart;
  16. const oY = logo.getAttribute('height') / 2.;
  17. var firstTime = true;
  18. var x0 = -1;
  19. var y0 = -1;
  20. // preinitialize eye position (it could be never updated)
  21. const r = .9;//(ev.clientX) / (window.innerWidth) / 2 + .5;
  22. const iX = istart + ilength * r;
  23. iris.setAttribute('cx', iX);
  24. const pX = iX + pstart + plength * r;
  25. pupil.setAttribute('cx', pX);
  26. pupil.setAttribute('cy', oY);
  27. window.onmousemove = function (ev) {
  28. if (firstTime) {
  29. x0 = ev.clientX;
  30. y0 = ev.clientY;
  31. firstTime = false;
  32. return;
  33. }
  34. if (ev.clientX == x0 && ev.clientY == y0) return; // avoid fake mouse data (Safari et all)
  35. const eyeOffset = body.getBoundingClientRect().x + lang.clientWidth + eye.clientWidth / 2;
  36. const r = (ev.clientX - eyeOffset) / (window.innerWidth - eyeOffset) / 2 + .5;
  37. const ry = ev.clientY / window.innerHeight;
  38. const iX = istart + ilength * r;
  39. iris.setAttribute('cx', iX);
  40. const pX = iX + pstart + plength * r;
  41. const pY = oY + 20 * r * (1 - r) * ry;
  42. // p(r) = y0 + a * r * (1 - r) is a parabola
  43. // p(0) = p(1) = y0
  44. // p(.5) = y0 + a / 4
  45. pupil.setAttribute('cx', pX);
  46. pupil.setAttribute('cy', pY);
  47. };
  48. };