查找旋转图像的大小以根据角度匹配顶线

(参考插图:https://i.stack.imgur.com/Wsge0.png

代码示例 https://jsfiddle.net/waaentz/htrqdwjp/9/

const width = 1000;
const height = 100;
const canvas = document.createElement("canvas");
const stage = new createjs.Stage(canvas);
const img = new Image();
const bitmap = new createjs.Bitmap(img);
const baseGrid = new createjs.Shape();

const angle = 45; // Is dynamic
const magicScaleFormular = 1.39 // Find scale based on height and angle

img.src = getBase64();
img.addEventListener("load",()=>{

    stage.addChild(baseGrid,bitmap);
    Object.assign(canvas,{width,height});

    bitmap.regX = (img.width / 2);
    bitmap.regY = img.height;
    bitmap.y = height;
    bitmap.rotation = angle;
    bitmap.scaleY = magicScaleFormular; // <-- scale to match top line

    baseGrid.graphics
        .beginStroke("red")
        .setStrokeStyle(4)
        .moveTo(0,0)
        .lineTo(width,0)
        .moveTo(0,height)
        .lineTo(width,height)

    stage.update();
    document.body.append(canvas);
});

我有一个100像素高的图像,当根据其角度旋转时,该图像需要增大尺寸以匹配顶部y线。当前第一行是y0,但可以是任何电线。

Math.sin和Math.cos的大小似乎发生了奇怪的变化,因此我不再尝试为此使用它们。

当图像旋转90度时,理论上它将增长到无限大小。这种数学巫术超出了我的思维能力,我希望这里有个聪明的人能对我表示怜悯,让我了解这个数学秘密。

lost32 回答:查找旋转图像的大小以根据角度匹配顶线

比例为1.0/cos(angle)(角度45为1,414),并在90度附近变得无限大。

值得这样限制最大值:(我添加了0.3,以便在小提琴窗口中以88度观看所有图片)

 magicScaleFormular = Math.min(0.3 * canvas.width / img.width,1 / Math.cos(angle * Math.PI / 180)) 
本文链接:https://www.f2er.com/3156170.html

大家都在问