Skip to content

Instantly share code, notes, and snippets.

@sapslaj
Last active January 1, 2016 23:09
Show Gist options
  • Save sapslaj/8215085 to your computer and use it in GitHub Desktop.
Save sapslaj/8215085 to your computer and use it in GitHub Desktop.
Mini sprite viewer made with jQuery and some CSS tricks. Set variables in the URL, ie: spriteviewer.html?image=http://i.imgur.com/GAZG8wV.png&framesize=40&framecount=8&fps=30
<!doctype html>
<html>
<body>
<div id="canvas"></div>
<script src="//code.jquery.com/jquery-1.10.2.min.js"></script>
<script>
var options = {};
window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function (m, key, value) {
options[key] = value;
});
$(function() {
var canvas = $("#canvas");
options['framesize'] = parseInt(options['framesize']);
options['framecount'] = parseInt(options['framecount']);
options['fps'] = parseInt(options['fps']);
canvas.width(options['framesize']);
canvas.height(options['framesize']);
canvas.css("background-image", "url(\"" + options['image'] + "\")");
var i = options['framecount'];
setInterval(function () {
if (typeof options['reverse'] === 'undefined') {
i == 1 ? i = options['framecount'] : i--;
} else {
i == options['framecount'] ? i = 1 : i++;
}
canvas.css("background-position", ((options['framesize'] * i) + "px 0px"));
}, 1000 / options['fps']);
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment