Skip to content

Instantly share code, notes, and snippets.

@mmurray
Created November 17, 2010 00:35
Show Gist options
  • Select an option

  • Save mmurray/702799 to your computer and use it in GitHub Desktop.

Select an option

Save mmurray/702799 to your computer and use it in GitHub Desktop.

Revisions

  1. mmurray created this gist Nov 17, 2010.
    20 changes: 20 additions & 0 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    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);