Skip to content

Instantly share code, notes, and snippets.

@goliatone
Forked from mmurray/gist:702799
Created July 20, 2017 23:01
Show Gist options
  • Select an option

  • Save goliatone/f2caf95f82f3543b294d665e3314f940 to your computer and use it in GitHub Desktop.

Select an option

Save goliatone/f2caf95f82f3543b294d665e3314f940 to your computer and use it in GitHub Desktop.
function CroppedImage(image,sx,sy,sw,sh){
this.image = image;
this.sx = sx;
this.sy = sy;
this.sw = sw;
this.sh = sh;
}
CroppedImage.prototype.draw = function(context,x,y){
context.drawImage(this.image, this.sx, this.sy, this.sw, this.sh, x, y, this.sw, this.sh)
}
//Usage:
var sprite1 = new CroppedImage(img1, 0, 0, 25, 25);
var sprite2 = new CroppedImage(img1, 0, 25, 25, 25);
sprite1.draw(context, 0, 0);
sprite2.draw(context, 34, 62);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment