代码之家  ›  专栏  ›  技术社区  ›  mexicanChica

为什么位图。缩放不工作?

  •  0
  • mexicanChica  · 技术社区  · 7 年前

    我想将我的图像添加到画布上,并对其进行定位和缩放。 我知道如何定位它,但每次缩放它时,它都会从画布上消失。

    我当前的代码与此类似:

    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);
    }
    

    除非删除,否则我的图像不会显示在画布上 image.scale(600, 600) .

    1 回复  |  直到 7 年前
        1
  •  2
  •   cbr    7 年前

    根据 documentation 这个 scale 属性是比例 因素 . 例如,比例为2表示图像 两倍大 . 您正在将图像放大600倍。因此,64x64px图像将变成38400x3840px图像。您需要根据图像的尺寸计算系数:

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

    还请注意 规模 是属性,您也可以直接指定给它:

    image.scale = 1.6;