Search code examples
bitmapsizeeaseljscreatejs

How to get Bitmap width/height createjs


I am using createjs 0.8.0 and having a problem getting Bitmap size. It always return 0

Here's my snippet:

    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;

it will have imgVisa.image.width = 0.

and if I am using :

imgSplash.Bitmap.width

It will throw me error : Uncaught TypeError: Cannot read property 'width' of undefined

Anyone can help ?


Solution

  • You should try to preload the image first:

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

    Keep in mind, that the onload function is called when the image is loaded, so it's async.