如何获取位图 width/height createjs

How to get Bitmap width/height createjs

我正在使用 createjs 0.8.0,但在获取位图大小时遇到​​了问题。 它总是 return 0

这是我的片段:

    var imgVisa     = new createjs.Bitmap("assets/images/Visa_logo.png");
    console.log("imgVisa x : " + imgVisa.image.width);
    var imgVisaX    = initialXpos;
    var imgVisaY    = canvas.height - 300;

它将有 imgVisa.image.width = 0

如果我使用:

imgSplash.Bitmap.width

它会抛出错误:Uncaught TypeError: Cannot read 属性 'width' of undefined

有人可以帮忙吗?

您应该先尝试预加载图像:

    var img = new Image()
    img.onload = function() {
        // gets called when the img is loaded
    };
    img.src = 'img.png';

请记住,加载图像时会调用 onload 函数,因此它是异步的。

您可以使用 Bitmap.getBounds().width