Skip to content

Instantly share code, notes, and snippets.

@Fire-
Last active August 29, 2015 14:13
Show Gist options
  • Save Fire-/66744a8911b793c7b45c to your computer and use it in GitHub Desktop.
Save Fire-/66744a8911b793c7b45c to your computer and use it in GitHub Desktop.
Random emotes to part of the dom
'use strict';
var timeoutID;
function makeEmote(emoteWidth, emoteHeight, emoteImage) {
var $newdiv = $('<div>').css({
'width': emoteWidth + 'px',
'height': emoteHeight + 'px',
'background-image': 'url(' + emoteImage + ')'
});
// make position sensitive to size and document's width
var posx = (((Math.random() * ($(document).width() - emoteWidth)) / $(document).width()) * 100).toFixed(1);
var posy = (((Math.random() * ($(document).height() - emoteHeight)) / $(document).height()) * 100).toFixed(1);
$newdiv.css({
'position': 'absolute',
'left': posx + '%',
'top': posy + '%',
'display': 'none'
}).appendTo('body').fadeIn(100);
}
function pickEmote(emoteList) {
if (emoteList.length <= 0) {
fetch();
return;
}
var selection = emoteList.shift();
makeEmote(selection.images[0].width, selection.images[0].height, selection.images[0].url);
timeoutID = setTimeout(pickEmote, randomIntFromInterval(1000, 10000), emoteList);
}
function fetch() {
var emoteList = [];
//( requires crossDomain permission or a different input source for this to work )
$.ajax('https://api.twitch.tv/kraken/chat/emoticons.json', {
dataType: 'json'
})
.done(function(d) {
emoteList = shuffle(d.emoticons);
})
.fail(function() {
emoteList = [];
})
.always(function() {
timeoutID = setTimeout(pickEmote, randomIntFromInterval(1000, 10000), emoteList);
})
}
function shuffle(array) {
var m = array.length,
t, i;
// While there remain elements to shuffle…
while (m) {
// Pick a remaining element…
i = Math.floor(Math.random() * m--);
// And swap it with the current element.
t = array[m];
array[m] = array[i];
array[i] = t;
}
return array;
}
function randomIntFromInterval(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
$(function() {
fetch();
});
<!DOCTYPE html>
<html>
<head>
<title>Kappa Kappa Kappa</title>
<style type="text/css">
div {
position: absolute;
-webkit-animation:spin 4s linear infinite;
-moz-animation:spin 4s linear infinite;
animation:spin 4s linear infinite;
}
@-moz-keyframes spin { 100% { -moz-transform: rotate(360deg); } }
@-webkit-keyframes spin { 100% { -webkit-transform: rotate(360deg); } }
@keyframes spin { 100% { -webkit-transform: rotate(360deg); transform:rotate(360deg); } }
html,body {
background: #262626;
width: 100%;
padding: 0;
margin: 0;
overflow:hidden;
}
</style>
<script src="jquery.1.11.2.min.js"></script>
<script src="randemote.js"></script>
</head>
<body>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment