Last active
August 29, 2015 14:18
-
-
Save nickjshearer/7cb7de5fae88e2c08d84 to your computer and use it in GitHub Desktop.
Dial Up Images
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
// Dial-up modem image loading using canvas. Relive the glory days of the internet | |
// | |
// Usage: | |
// <canvas class="image-canvas" width="180" height="180" data-image="http://lorempixel.com/180/180/"> | |
// <p>Image alt textc</p> | |
// </canvas> | |
// | |
window.onload = function() { | |
var elements = document.getElementsByClassName("image-canvas"); | |
Array.prototype.forEach.call(elements, function(canvas) { | |
var context = canvas.getContext("2d"); | |
var img = new Image(); | |
var canvasHeight = canvas.offsetHeight; | |
img.src = canvas.getAttribute('data-image'); | |
img.onload = function() { | |
var height = 0; | |
function animate(timestamp) { | |
height += Math.floor(Math.random()*10); | |
updateDelay = Math.floor(Math.random()*1000); | |
if (height > canvasHeight) { | |
height = canvasHeight; | |
} | |
context.drawImage(img, 0, 0, canvas.offsetWidth, height, 0, 0, canvas.offsetWidth, height); | |
if (height < canvasHeight) { | |
setTimeout(function(){window.requestAnimationFrame(animate)}, updateDelay); | |
} | |
} | |
window.requestAnimationFrame(animate); | |
}; | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice! - In codepen form: http://codepen.io/anon/pen/jERmNm