I need multiple images placed into canvas. The images should be rotated, resized and placed at particular coordinates. So in general I need
ctx.drawImage(img,left,top, width, height);
but rotated.
I have come up with this function:
function drawRotated(image, context, angle, left, top, width, height) {
context.save();
context.translate(width / 2, height / 2);
context.rotate(Math.PI / 180 * angle);
context.drawImage(image, left - width / 2, top - height / 2, width, height);
context.restore();
}
But it doesn't seem to be working exactly as needed. It looks like after rotation I need to recalculate the position with the new width and height that rotation has turned the image into. But I can't figure out the math. Do I need to actually do trigonometry stuff to calculate all that, or is there a simple solution? Thanks.