Skip to content

Instantly share code, notes, and snippets.

@UbaldoRosas
Last active July 24, 2019 17:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save UbaldoRosas/770b4afdac1539eb449dd2184371bfc0 to your computer and use it in GitHub Desktop.
Save UbaldoRosas/770b4afdac1539eb449dd2184371bfc0 to your computer and use it in GitHub Desktop.
Javascript | Sprites animator
animated_sprite = function () {
this.vars = {
currentIndex : 0,
path : null,
class : null,
interval : null,
lenght : null,
time : null
}
this.init = function(sprite, lenght = null, time)
{
this.vars.paths = document.querySelector("." + sprite);
this.vars.class = sprite;
this.vars.lenght = lenght;
this.vars.time = time;
window.clearInterval(this.vars.interval);
this.vars.interval = window.setInterval(this.draw_image.bind(this), this.vars.time);
}
this.draw_image = function ()
{
this.vars.paths.setAttribute('class', this.vars.class + ' ' + this.vars.class + '_' + this.vars.currentIndex);
if (this.vars.currentIndex < this.vars.lenght - 1) {
this.vars.currentIndex += 1;
} else {
this.vars.currentIndex = 0;
}
}
this.stop = function ()
{
window.clearInterval(this.vars.interval);
},
this.play = function ()
{
this.vars.interval = window.setInterval(this.draw_image.bind(this), this.vars.time);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment