【发布时间】:2020-01-13 17:19:57
【问题描述】:
我在 Canvas 上有一张图片。 渲染图像后,我将画布顺时针旋转 90 度,然后必须缩放以适合画布。
Scaling an image to fit on canvas Resize image and rotate canvas 90 degrees
有很多可用的解决方案,但不幸的是,它们都不适合我
这就是我现在所拥有的。
width = 300; // after rotation
height = 400; // after rotation
var scale = width / img.height; // how much to scale the image to fit
console.log(scale);
canvas.width = width;
canvas.height = height;
ctx.setTransform(
0, scale, // x axis down the screen
-scale, 0, // y axis across the screen from right to left
width, // x origin is on the right side of the canvas
0 // y origin is at the top
);
ctx.drawImage(img, 0, 0);
ctx.setTransform(1, 0, 0, 1, 0, 0); // restore default
目前,画布旋转正常,但图像无法根据画布宽度和高度正确调整或缩放。 原始图像很大,我想将其缩放以适合画布上的 300x400。
实际结果图像如下所示,未缩放完整图像 -
【问题讨论】:
标签: javascript canvas html5-canvas image-resizing drawimage