question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Better documentation on renderer.generateTexture with SVG resource

See original GitHub issue

In our system, we are trying to cache our SVG assets using the renderer.generateTexture1.

However, we found that if we call destroy() on the baseTexture and try to regenerate, we got empty sprite. But other part that uses simple Graphics generates texture correctly.

Later we found that it is due to SVG Resource load is asynchronized. The load with auto-load usually works, but we are caching with generateTexture which will need the SVG to be ready. Otherwise it will generate empty texture. https://github.com/pixijs/pixijs/blob/21e780da04f902a8b5d89ccc716daef190c89858/packages/core/src/textures/resources/SVGResource.ts#L81-L101

It might be a good idea to update the documentation for renderer.generateTexture to indicate:

  • If using SVG Resource, run with auto-load disabled
  • Explicitly await for SVG Resource to be ready
  • Then call generateTexture

Here is a snippet:

if (Asset[key].includes('svg')) {
  const svgResource = new SVGResource(Asset[key], { autoLoad: false });
  const resolvedResource = await svgResource.load();
  const baseTexture = new BaseTexture(resolvedResource, {/* config */});
  const texture = new Texture(baseTexture); 
  const sprite = new Sprite(texture);
  // Update sprite property...
  cache[key] = renderer.generateTexture(sprite, { /* config*/ });
}     

^1 I did not refer the source code due to my system is running 6.4.2, and there are serval updates to the generateTexture code at the time of writing. There are notes saying it will be a breaking change coming to 7.0

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:1
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
MasonJason23commented, Nov 19, 2022

Hey @zero41120, Instead of using the code snippet that you provided, have you tried using the PIXI.Texture class? Here is an example code snippet that you can use for your SVG type images.

if (Asset[key].includes('svg')) {
    const texture = Texture.from(Asset[key]);
    var img = new Sprite(texture);
}
1reaction
terekitecommented, Nov 13, 2022

Hi @zero41120 , I’m working on this issue with @MasonJason23 and @sunveer2001. we’ve got your code snippet running, how exactly are you destroying and regenerating the base texture you have? Can you share with us another snippet to give us a better idea of what you are trying to do?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Container.generateTexture Issue · Issue #2531 · pixijs/pixijs
generateTexture () I'm loading several base64 images into the loader (which works fine). In the resource loader callback, I am able to render...
Read more >
PIXI.Renderer - PixiJS API Documentation
This manages the WebGL state variables like blend mode, depth testing, etc. PIXI.TextureSystem, This manages textures and their resources on the GPU. PIXI....
Read more >
Tools for SVG - SVG: Scalable Vector Graphics | MDN
Tools for SVG · Inkscape · Adobe Illustrator · Apache Batik · Snap.svg · Google Docs · Science · More tools! · MDN....
Read more >
PIXI.WebGLRenderer - PixiJS API Documentation
The WebGLRenderer draws the scene and all its content onto a webGL enabled canvas. This renderer should be used for browsers that support...
Read more >
SVG Files: What They Are and How to Make One - HubSpot Blog
An SVG file, short for scalable vector graphic file, is a standard graphics file type used for rendering two-dimensional images on the internet....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found