Created
October 11, 2011 08:25
-
-
Save villiger/1277564 to your computer and use it in GitHub Desktop.
Animate letters on screen with JavaScript along a sinus wave
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> | |
<title>Sine Wave</title> | |
<style type="text/css"> | |
.letter { | |
font-family: sans-serif; | |
font-size: 40px; | |
font-weight: bold; | |
position: absolute; | |
top: -100px; | |
left: 0px; | |
} | |
</style> | |
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" type="text/javascript" charset="utf-8"></script> | |
<script type="text/javascript"> | |
$(document).ready(function() { | |
var width = $(window).width() - 40; | |
var starttime = new Date().getTime(); | |
var letters = document.location.hash ? document.location.hash.substr(1) : 'SINUSWAVE'; | |
var elements = $('#elements'); | |
for (i = 0; i < letters.length; i++) { | |
$('<div>', { | |
html: letters[i] | |
}) | |
.addClass('letter') | |
.appendTo(elements); | |
} | |
function run() { | |
var elapsed = new Date().getTime() - starttime; | |
var pos = elapsed * 0.1; | |
$('div.letter').each(function(index, letter) { | |
var posx = (pos + 50 * index) % width; | |
var posy = 200 + Math.sin(posx / 50) * 50; | |
$(letter).css('left', posx + 'px'); | |
$(letter).css('top', posy + 'px'); | |
}); | |
} | |
setInterval(run, 30); | |
}); | |
</script> | |
</head> | |
<body> | |
<div id="elements"></div> | |
</body> | |
</html> |
pwfisher
commented
Aug 2, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment