Skip to content

Instantly share code, notes, and snippets.

@nickworks
Created March 16, 2018 22:35
Show Gist options
  • Save nickworks/9784f85ff42a46e709c38c55e3d8f2c2 to your computer and use it in GitHub Desktop.
Save nickworks/9784f85ff42a46e709c38c55e3d8f2c2 to your computer and use it in GitHub Desktop.
A simple Sprite prototype for vanilla JS.
function Sprite(url){
this.img = new Image();
this.img.src = url;
this.img.addEventListener("load", ()=>{
this.anchor.x = this.img.width/2;
this.anchor.y = this.img.height/2;
});
this.x = 0;
this.y = 0;
this.angle = 0;
this.scale = 1;
this.anchor = {x:0, y:0};
this.draw = function(gfx){
gfx.translate(this.x, this.y);
gfx.rotate(0);
gfx.scale(this.scale, this.scale);
gfx.drawImage(this.img, -this.anchor.x, -this.anchor.y);
gfx.resetTransform();
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment