I have the following code below to be able to rotate an image through canvas
.
function qs(selector) {
return document.querySelector(selector);
}
function rotacionar(canvas, image, angle)
{
canvas.height = image.height;
canvas.width = image.width;
var context = canvas.getContext('2d');
context.translate(image.width / 2, image.height / 2);
context.rotate(angle * Math.PI / 180)
context.drawImage(image, -image.width / 2, -image.height / 2);
}
function onLoad()
{
var canvas = qs('#canvas');
var image = qs('#image');
var btnRotate = qs('#rotate');
var angle = 0;
btnRotate.addEventListener('click', function () {
rotacionar(canvas, image, angle += 90);
});
}
window.addEventListener('load', onLoad)
<img id="image" src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/4QAqRXhpZgAASUkqAAgAAAABADEBAgAHAAAAGgAAAAAAAABHb29nbGUAAP/bAIQAAwICCg0KCg0LCgsKCgsKDQoQCgoLCAoKCgoKCggICAoKCAgNCggKDQgJCggKCggKCgoKCgoKCg0NCggNCAgKCAEDBAQGBQYKBgYKDw0LDg4PDxANDw0NDg0NDQ8NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0N/8AAEQgAZABkAwERAAIRAQMRAf/EAB0AAAIDAQEBAQEAAAAAAAAAAAYHBAUIAwIBCQD/xAA6EAACAQIEAwYEAwgBBQAAAAABAhEDIQAEEjEFBkEHEyJRYXGBkaGxCDLwFEJSYsHR4fGCFRgjM3L/xAAbAQACAwEBAQAAAAAAAAAAAAAEBQIDBgcBAP/EADARAAICAgIBBAECBQMFAAAAAAECAAMEERIhMQUTQVEiMnEGFCNhkVKBoWKxweHw/9oADAMBAAIRAxEAPwBH8H4FTqHXUIAB6/M45wzWVdCMA7HoQoDIW0ZdCLXY29N9sF4+O9w2/gT3ZHRl7yz2DVahJdl0TfxKT57YA9VuTHp2ixr6fiDJcgtCvO9nSUaL91UAfpt5Yo9JSzOIbj4hGdhV4o1y7iOy3OBSutOu5EuNjvcbf7xscuyxayuviJEUN3vuTvxA5gM8KkqQNJgyR77fXA3plwFID+ZG2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dUe1jN18jWWq4Ze7IiOmnbHmJfYxKknW/E6pkVqq8lGjPzs5j5apJLKl97zvc40y2MAO5j3J5GAAp65LEz6R/bB471ufbnzvSr+EkWnfF9g6lyHqX+XzLGiWJMlz9IwPSdNK2JPmF3LXarnKdJVSu6qNhNh6DFGR6ZjWvzdASY0x8y5E4qx1P/9k=";">
<canvas id="canvas"></canvas>
<button id="rotate" style="display:block">
Rotate
</button>
My question is regarding this section:
// A
canvas.height = image.height;
canvas.width = image.width;
var context = canvas.getContext('2d');
// B
context.translate(image.width / 2, image.height / 2);
// C
context.rotate(angle * Math.PI / 180)
// D
context.drawImage(image, -image.width / 2, -image.height / 2);
A) The canvas
is defined with the same image dimensions (height and width).
B) The translate
of canvas.getContext('2d')
method is called in
then set the first parameter with the width of the image divided by 2, and the second, the height divided by 2.
C) A degree conversion is made to some other unit that I do not know what it is. This snippet is angle * Math.PI / 180
. This is the moment that rotate
is called.
D) The drawImage
method is then called by setting the parameters x
and y
to have the values of the width and height of the image divided by two and negative respectively.
About the steps listed above:
- Why in A, does the canvas need to have the same dimensions as the image?