Search code examples
createjseaseljs

Easeljs getBounds, when are dimensions set?


I'm importing someone's createjs application into an application loader which imports each developers application javascript to a global canvas. The application is working standalone but when I load the javascript dynamically I'm getting some strange behavior surrounding the getBounds() function.

I have some text created like this:

dialogText = new createjs.Text(text, 'bold ' + (28 * scale) + 'px Calibri', '#FFFFFF');

Then later on I use dialogText.getBounds() which returns null.

I try console logging dialogText and I can see that it is set and there is a value _bounds which reads null.

At what point do these values get set when the element in drawn to the canvas?

Note: I'm already adding the text to a createjs.Container() which has its bounds set before I run getBounds().


Solution

  • The _bounds property reflects a value that is manually set on the DisplayObject - which is helpful if you know the bounds of something that can't calculate it (like a Shape). It will always be null, unless you set it directly.

    I tested your code, and it seems fine. Are you sure the scale or text values are not null? Maybe hard-code them to see if it works.

    Do you have a sample somewhere? I noticed in the GitHub issue you posted that you mentioned it worked fine without RequireJS (though you don't mention that here). I would definitely ensure that the values are being set properly.

    Here is the fiddle, as well as a simpler one with no EaselJS stage. Note that I have Calibri installed on my machine, so you might get different results if you are loading it in.

    Sample code:

    var dialogText = new createjs.Text("This is some text", 'bold ' + (28 * 1.5) + 'px Calibrix', '#FFFFFF');
    console.log(">>",dialogText.getBounds());