I have a canvas with fixed width and height and I want draw a scaled/fitted image to the canvas and rotate the image on it's center. When I put any value in angle besides 0, the image is not centered and skews out of the canvas.
This is what I have right now:
const box = document.getElementById("box");
const box2 = document.getElementById("box2");
(function() {
const image = new Image();
image.onload = () => {
const myCanvas = drawImage(0, image);
const img = document.createElement("img");
img.src = myCanvas.toDataURL("image/png", 1);
img.style = "border: 1px solid black;";
box.append(img)
const myCanvas2 = drawImage(15, image);
const img2 = document.createElement("img");
img2.src = myCanvas2.toDataURL("image/png", 1);
img2.style = "border: 1px solid black;";
box2.append(img2)
};
image.src = "https://raw.githubusercontent.com/hamza-ameer/Google-Maps-SmoothCarAnimation/Uodated/car_marker.png";
image.crossOrigin="anonymous"
})()
function drawImage(angle, image) {
const canvasWidth = 100;
const canvasHeight = 100;
const myCanvas = document.createElement("canvas");
myCanvas.width = canvasWidth;
myCanvas.height = canvasHeight;
const context = myCanvas.getContext("2d");
const imageWidth = image.width;
const imageHeight = image.height;
const scaler = scalePreserveAspectRatio(imageWidth, imageHeight, canvasWidth, canvasHeight);
const scaledWidth = imageWidth * scaler;
const scaledHeight = imageHeight * scaler;
context.save();
context.clearRect(0, 0, canvasWidth, canvasHeight);
context.rotate((angle * Math.PI) / 180);
context.translate((canvasWidth - scaledWidth) / 2, (canvasHeight - scaledHeight) / 2);
context.drawImage(image, 0, 0, imageWidth, imageHeight, 0, 0, scaledWidth, scaledHeight);
context.strokeRect(0, 0, imageWidth * scaler, imageHeight * scaler);
context.restore();
return myCanvas;
}
function scalePreserveAspectRatio(imgW, imgH, maxW, maxH) {
return Math.min(maxW / imgW, maxH / imgH);
}
<div id="box"></div>
<div id="box2"></div>
Thanks for the help.