Last active
August 29, 2015 14:04
-
-
Save rlemon/960e0f0e996b16e7d201 to your computer and use it in GitHub Desktop.
random image from imagur every 60 seconds
This file contains hidden or 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
(function () { | |
"use strict"; | |
var parts = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', | |
canvas = document.createElement('canvas'), | |
context = canvas.getContext('2d'); | |
imgur(); | |
function makeImage(url, cb) { | |
var img = new Image(); | |
img.crossOrigin = 'Anonymous'; | |
img.onload = cb; | |
img.src = url; | |
} | |
function makeId() { | |
var id = ''; | |
for (var i = 0; i < 5; i++) { | |
id += parts.charAt(Math.floor(Math.random() * parts.length)); | |
} | |
return id; | |
} | |
function checkImage(img) { | |
return img.width !== 161 && img.height !== 81; | |
} | |
function encodeImage(img) { | |
canvas.height = img.height; | |
canvas.width = img.width; | |
context.drawImage(img, 0, 0); | |
return canvas.toDataURL('image/png'); | |
} | |
function drawImage(dataUrl) { | |
document.body.style.background = 'url(' + dataUrl + ')'; | |
} | |
function imgur() { | |
var url = 'http://i.imgur.com/' + makeId() + '.png', | |
timeout = 10; | |
makeImage(url, function () { | |
if (checkImage(this)) { | |
drawImage(encodeImage(this)); | |
timeout *= 6000; | |
} | |
setTimeout(imgur, timeout); | |
}); | |
} | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment