In the pure Java version, calling width() or height() on Image or on ImageLayer works. But with HTML5 backend I get zero if I call it too early.
Image image = assets().getImage("images/myimage.png");
ImageLayer layer = graphics().createImageLayer(image);
float w1 = image.width();
float w2 = layer.width();
I tried right after the getImage() in the initialization code of the game and also from the paint() method, the first time paint() is called. But I always get zero with the HTML5 backend (Chrome, MacOs).
However, if I call width() after the second call to the paint() method I get the correct width.
So it seems that with the HTML5 backend the semantics is different: the width() method returns the correct width of an image / layer after it has been rendered a first time.
Is my interpretation correct and how to get the size before rendering?
Have you tried using the AssetWatcher? It looks like those first
widthcalls are outracing the document’s ability to load the image.Here’s a simple example of the
AssetWatcherin action:And here’s an implementation of
AssetWatcherfrom the PlayN Showcase sample: