Skip to content

Instantly share code, notes, and snippets.

@rlemon
Last active August 29, 2015 14:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rlemon/960e0f0e996b16e7d201 to your computer and use it in GitHub Desktop.
Save rlemon/960e0f0e996b16e7d201 to your computer and use it in GitHub Desktop.
random image from imagur every 60 seconds
(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