Javascript Fabric.js setHeight()和scaleToHeight()提供不同的高度值

Javascript Fabric.js setHeight()和scaleToHeight()提供不同的高度值,javascript,html5-canvas,fabricjs,Javascript,Html5 Canvas,Fabricjs,我在调整图像大小方面有问题。我想调整图像大小,但不保留比例。当我使用setHeight()时,它给了我错误的值 image.setHeight(100); image.getHeight(); // gives me 140px output 当我使用比例函数时,一切都是正确的,但它也保持了图像比例 image.scaleToHeight(100); image.getHeight(); // correct 100px output 这个测试也给了我奇怪的值: console.log("h

我在调整图像大小方面有问题。我想调整图像大小,但不保留比例。当我使用setHeight()时,它给了我错误的值

image.setHeight(100);
image.getHeight(); // gives me 140px output
当我使用比例函数时,一切都是正确的,但它也保持了图像比例

image.scaleToHeight(100);
image.getHeight(); // correct 100px output
这个测试也给了我奇怪的值:

console.log("h: " + image.getHeight()); // 348.25870646766174
image.set({ height : image.getHeight()-10 });
console.log("h: " + image.getHeight()); // 235.6030791317047
编辑:

更多代码:

var flatImage = document.getElementById("flat-image");

var canvas = new fabric.Canvas("frame-canvas",{                         
        width: 992,
        height: 450
});

var image = new fabric.Image(flatImage, {});
    image.selectable = false;
    // image.scaleToWidth( 250 );

canvas.add(image);
canvas.centerObject(image);
canvas.renderAll();

console.log("h: " + image.getHeight()); // 334
image.set({ height : image.getHeight()-10 });
console.log("h: " + image.getHeight()); // 324 works good
但是,当我添加scaleToWidth()高度值时,会感到奇怪:

image.scaleToWidth( 250 );

console.log("h: " + image.getHeight()); // h: 166.66666666666666
image.set({ height : image.getHeight()-10 });
console.log("h: " + image.getHeight()); // h: 78.17697937458415
基本上是对半切割,然后减去给定的值

为什么会这样?

这个片段看起来不错。 我想不出还有什么其他的理由可以得到不同的价值观, 请查看标尺属性
img.scaleX
img.scaleY
,确保它们等于1,否则会导致问题


祝你好运

如果你能分享更多的代码,那就太好了,因为它工作得很好,我应该多放一些代码。是的。scaleToWidth()似乎更改了这些属性,所以我必须手动将图像宽度更改为所需的值,然后更改图像高度。非常感谢。