Sometimes, we want to rotate a single object on an HTML5 canvas.
In this article, we’ll look at how to rotate a single object on an HTML5 canvas.
Rotate a Single Object on an HTML5 Canvas
To rotate a single object on an HTML5 canvas, we can use the canvas context’s translate
and rotate
methods.
For instance, we write:
<canvas id='canvas'></canvas>
to create the canvas element.
Then we write:
const drawImageRot = (ctx, img, x, y, width, height, deg) => {
ctx.save()
const rad = deg * Math.PI / 180;
ctx.translate(x + width / 2, y + height / 2);
ctx.rotate(rad);
ctx.drawImage(img, width / 2 * (-1), height / 2 * (-1), width, height);
ctx.restore();
}
const img = new Image();
img.src = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAApgAAAKYB3X3/OAAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAAANCSURBVEiJtZZPbBtFFMZ/M7ubXdtdb1xSFyeilBapySVU8h8OoFaooFSqiihIVIpQBKci6KEg9Q6H9kovIHoCIVQJJCKE1ENFjnAgcaSGC6rEnxBwA04Tx43t2FnvDAfjkNibxgHxnWb2e/u992bee7tCa00YFsffekFY+nUzFtjW0LrvjRXrCDIAaPLlW0nHL0SsZtVoaF98mLrx3pdhOqLtYPHChahZcYYO7KvPFxvRl5XPp1sN3adWiD1ZAqD6XYK1b/dvE5IWryTt2udLFedwc1+9kLp+vbbpoDh+6TklxBeAi9TL0taeWpdmZzQDry0AcO+jQ12RyohqqoYoo8RDwJrU+qXkjWtfi8Xxt58BdQuwQs9qC/afLwCw8tnQbqYAPsgxE1S6F3EAIXux2oQFKm0ihMsOF71dHYx+f3NND68ghCu1YIoePPQN1pGRABkJ6Bus96CutRZMydTl+TvuiRW1m3n0eDl0vRPcEysqdXn+jsQPsrHMquGeXEaY4Yk4wxWcY5V/9scqOMOVUFthatyTy8QyqwZ+kDURKoMWxNKr2EeqVKcTNOajqKoBgOE28U4tdQl5p5bwCw7BWquaZSzAPlwjlithJtp3pTImSqQRrb2Z8PHGigD4RZuNX6JYj6wj7O4TFLbCO/Mn/m8R+h6rYSUb3ekokRY6f/YukArN979jcW+V/S8g0eT/N3VN3kTqWbQ428m9/8k0P/1aIhF36PccEl6EhOcAUCrXKZXXWS3XKd2vc/TRBG9O5ELC17MmWubD2nKhUKZa26Ba2+D3P+4/MNCFwg59oWVeYhkzgN/JDR8deKBoD7Y+ljEjGZ0sosXVTvbc6RHirr2reNy1OXd6pJsQ+gqjk8VWFYmHrwBzW/n+uMPFiRwHB2I7ih8ciHFxIkd/3Omk5tCDV1t+2nNu5sxxpDFNx+huNhVT3/zMDz8usXC3ddaHBj1GHj/As08fwTS7Kt1HBTmyN29vdwAw+/wbwLVOJ3uAD1wi/dUH7Qei66PfyuRj4Ik9is+hglfbkbfR3cnZm7chlUWLdwmprtCohX4HUtlOcQjLYCu+fzGJH2QRKvP3UNz8bWk1qMxjGTOMThZ3kvgLI5AzFfo379UAAAAASUVORK5CYII=";
img.onload = () => {
const scale = 8;
const srcCanvas = document.querySelector('canvas');
const srcCtx = srcCanvas.getContext('2d');
drawImageRot(srcCtx, img, 0, 0, 100, 100, 90)
};
to create the drawImageRot
function to rotate the image.
The function takes the ctx
canvas context, img
img element object, x
and y
coordinates of the top left corner of the image, width
and height
of the image, and deg
degrees of rotation.
In the function, we call save
to save the contents.
Then we convert deg
to rad
radians.
Next, we call ctx.translate
to translate the content by width / 2
and height / 2
.
Then we call rotate
to rotate the image.
After that we call drawImage
to draw a rotated image.
Finally, we call restore
to render the image.
Next we use the Image
constructor to create the image.
Finally, we set img.onload
to a function that gets the canvas with document.querySelector
.
Then we call getContext
to get the context.
And then we call drawImageRot
to draw the image rotated 90 degrees.
Conclusion
To rotate a single object on an HTML5 canvas, we can use the canvas context’s translate
and rotate
methods.