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.

[Firefox] SVGResource cannot load SVG without explicitly specified width and height

See original GitHub issue

Current Behavior

Not sure if this is a bug or feature.

While working on my project with Pixi, some of my SVG fails to load in Firefox and reported: The SVG image must have width and height defined (in pixels), canvas API needs them.

My SVG has viewBox specification, which means it should take all available space and scale accordingly. (Well, Scalable Vector Graphics, it is in the name)

Well, instead of fixing all my SVG, which is provided by designer with their tool that generate the same format. I would like to make Pixi work with them.

Expected Behavior

Pixi should load SVG without explicitly specified width/height (with only viewBox specification). This is an issue tracked here 10 years ago. User webrant has provided an workaround, and I made a slight modification to use the viewBox in this jsfiddle as proof of concept for image with only viewBox specification.

Pixi could detect missing width/height from SVG and fall back to use viewBox if available.

Possible Solution

Update the SVGResource’s error handler. Instead of reporting 0 and abort, fallback to parse the base64 and get viewBox If viewBox is also not available, then abort. https://github.com/pixijs/pixijs/blob/bb47818dfa2c388f8adb6dbb3a8f32fdfb1e72a0/packages/core/src/textures/resources/SVGResource.ts#L158-L175

Steps to Reproduce

Load any SVG with only viewBox in Firefox

Environment

  • pixi.js version: 6.1.0
  • Browser & Version: _e.g. Chrome 94 and FireFox91.2.0esr
  • OS & Version: _e.g. Windows 10 20H2

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
zero41120commented, Apr 20, 2022

My team needs the SVG fixed, so I made this CLI that reads SVG’s ViewBox and writes them as width and height:

https://www.npmjs.com/package/@zero41120/svg-dimension-from-viewbox

0reactions
Zyiecommented, Jul 1, 2022

Closing this ticket for now

As mentioned in #7878 we don’t think Pixi should be handling invalid data in this case, especially when it adds a lot of complexity to the SVGResource

Please use the solution provided above as a workaround

Read more comments on GitHub >

github_iconTop Results From Across the Web

Firefox won't scale svg image properly
I saved the image as SVG without a mark in the "set viewbox" option, and then used that SVG in the Muse ......
Read more >
Firefox error rendering an SVG image to HTML5 canvas ...
There is a workaround. Firefox requires explicit width and height attributes in the SVG itself. We can add these by getting the SVG...
Read more >
SvgImageSource Class (Windows.UI.Xaml.Media.Imaging)
Examples. Here's an example of how to set the source of an Image to a SVG file. Since there is no height or...
Read more >
Use SVG Resources
Upload the SVG resource as a static resource and import it in your component's JavaScript ... <svg version="1.1" baseProfile="full" width="300" height="200" ...
Read more >
How to Scale SVG
Raster images like JPG, PNG, and GIF, have a clearly defined size. ... If you use an <img> to embed your SVG, setting...
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