Jump to content

PIXI.Texture.from('path') is asynchronous?


Bobi Georgiev
 Share

Recommended Posts

I am loading a couple of images with PIXI.Texture.from('path'). 

I need to immediately store the width and height of the new Texture right after calling PIXI.Texture.from(). However, when I log the width and height there, both values are set to 1.

I have an Update() method in my program which is called every frame, like in Unity. When I log the values in the Update, the width and height of the textures are again 1 1 in the first frame, after which they get their original size and all consecutive logs show the correct image size. 

I tried calling await on PIXI.Texture.from(), but it does not seem to solve my issue. Has anyone encountered this problem and is there a way I can work around it? 

 

Link to comment
Share on other sites

Hello and Welcome to the forums!

Yes, it is async because there's no way in JS to stop thread and just wait when file loads.

one of related questions: https://github.com/pixijs/pixi.js/issues/6583

I tried calling await on PIXI.Texture.from(), but it does not seem to solve my issue. 

I recommend to look in sources first. "from" is a method that auto-detects whatever resource you want to load, creates Resource/BaseTexture/Texture or just takes it from cache. In your case its ImageResource that has promises and stuff and you can hook in it if you want extra features like createImageBitmap.

I need to immediately store the width and height of the new Texture right after calling PIXI.Texture.from()

Use pixi loader with single textures or atlases. Here's how to use it: https://github.com/kittykatattack/learningPixi#displaying-sprites

Preload everything, then at some point you will have all the sizes.

Another idea is to set texture size yourself if you want what it will be: 

https://github.com/pixijs/pixi.js/blob/93835dbbecbd0b4f31c9533bde8386a71c5662e3/packages/core/src/textures/BaseTexture.ts#L430

"texture.baseTexture.setRealSize(10, 10)`

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...