Skip to content

Instantly share code, notes, and snippets.

@abronte
Created December 16, 2010 22:32
Show Gist options
  • Select an option

  • Save abronte/744131 to your computer and use it in GitHub Desktop.

Select an option

Save abronte/744131 to your computer and use it in GitHub Desktop.
draw image from a sprite
//get canvas element
var canvas = document.getElementById("main");
//get context
var ctx = canvas.getContext('2d');
//create new image object
var image = new Image();
//set callback for when the image actually loads
image.onload = function () {
//actual draw image
ctx.drawImage(image, pStill.x, pStill.y, pStill.w, pStill.h, 0, 0, pStill.w, pStill.h);
};
//load the image
image.src = "mario.png";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment