为什么 Bitmap.scale 不起作用?

Why does Bitmap.scale not work?

我想将我的图像添加到 canvas,然后对其进行定位和缩放。 我知道如何定位它,但每次我缩放它时,它都会从我的 canvas.

中消失

我当前的代码与此类似:

function init() {
    var canvas = new createjs.Stage("canvas");

    var image = new createjs.Bitmap("assets/image.png");
    image.scale(600, 600);
    canvas.addChild(image);
    canvas.update(image);
}

我的图片不会出现在 canvas 上,除非我删除 image.scale(600, 600)

根据 documentationscale 属性 是比例 因子。例如,比例为 2 表示图像两倍大。您正在将图像放大 600 倍。所以 64x64px 的图像将变成 38400x38400px 的图像。您需要根据图像的尺寸计算系数:

const imageBounds = image.getBounds()
image.scaleX = 600 / imageBounds.width;
image.scaleY = 600 / imageBounds.height;

还要注意scale是一个属性,你也可以直接赋值给它:

image.scale = 1.6;