Skip to content

Instantly share code, notes, and snippets.

@defektive
Created November 15, 2012 18:24
Show Gist options
  • Save defektive/4080282 to your computer and use it in GitHub Desktop.
Save defektive/4080282 to your computer and use it in GitHub Desktop.
Sprite Animator
var AnimateSprite = new Class({
Implements: [Options],
options: {
size: {
x: 10,
y: 10
},
src: "",
element: null,
updateInterval: 300,
scenes: {
}
},
initialize: function (options){
this.setOptions(options);
if(this.options.element === null){
this.options.element = new Element("div");
document.body.appendChild(this.options.element);
}
this.options.element.setStyles({
width: this.options.size.x +"px",
height: this.options.size.y + "px",
backgroundImage: "url("+this.options.src+")"
});
},
toggle: function (){
if(this.i){
clearInterval(this.i);
this.i = false;
} else {
this.i = this._update.periodical(this.options.updateInterval, this);
}
},
animate: function(pos){
this.scene = this.options.scenes[pos];
this.frame = 0;
this.mode = 0;
this._update();
},
_update: function (){
if(this.scene){
var pos = this.scene[this.frame].x + "px "+ this.scene[this.frame].y + "px";
this.options.element.style.backgroundPosition = pos;
if(this.mode === 0){
this.frame++;
} else {
this.frame--;
}
if(this.frame >= this.scene.length){
this.mode = 1;
this.frame--;
} else if (this.frame <= 0) {
this.mode = 0;
// this.frame++;
}
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment