Saturday, 16 July 2011

Snippet - Draw Image with Rotation (HTML5 Games)

This is a function that draws an image with specified rotation.
(I first posted this function in the Rotate Image on Canvas tutorial.

function drawImg(img, pX, pY, oX, oY, w, h, rot) {;
  context2D.translate(pX+oX, pY+oY);
  context2D.rotate(rot * Math.PI / 180);
  context2D.drawImage(img, 0, 0, w, h, -(oX), -(oY), w, h);

img: the image object
pX: the x position of the image
pY: the y position of the image
oX: how far across the image that the origin is
oY: how far down the image that the origin is
w: width of the image
h: height of the image
rot: angle of rotation