Skip to content

Instantly share code, notes, and snippets.

@ngryman
Created February 11, 2016 17:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ngryman/dbe020b5135722bbce8a to your computer and use it in GitHub Desktop.
Save ngryman/dbe020b5135722bbce8a to your computer and use it in GitHub Desktop.
Heartbeat relative to mouse position
/**
* Heartbeat rhythm
*/
var heart = document.querySelector('footer span')
var heartBounds = heart.getBoundingClientRect()
var rhythm
document.body.addEventListener('mousemove', function(e) {
var mx = e.clientX
var my = e.clientY
var hx = heartBounds.left + heartBounds.width / 2
var hy = heartBounds.top + heartBounds.height
var dist = Math.sqrt((mx - hx) * (mx - hx) + (my - hy) * (my - hy))
var vmax = Math.max(hx, hy)
var ratio = dist / vmax
rhythm = Math.min(2, Math.round(ratio * 100 * 2) / 100 + 0.5)
})
// TODO: it seems to sometimes restart the animation while it has already begun
heart.addEventListener('animationiteration', function() {
heart.style.animationDuration = rhythm + 's'
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment