How come I can't reference texture.image.width outside?

I haven’t coded in js before, can someone explain why I can print the width+height inside the local function but I get a null if I use it in the bottom? Is there a way I can grab the width outside or would that not be preferred?

The image isn’t loaded yet right after the load() call. load() is no sync method.

With the async/await pattern you can do this:

const loader = new THREE.TextureLoader();
const texture = await loader.loadAsync('./image.png');
geometry.scale.x = texture.image.width;

This code has to run in an async context though.

1 Like