Created
February 11, 2016 17:02
-
-
Save ngryman/dbe020b5135722bbce8a to your computer and use it in GitHub Desktop.
Heartbeat relative to mouse position
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
/** | |
* 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