Skip to content

[Bug]: SVG import bounds ignore viewBox/viewport; group dimensions larger than SVG width/height and hidden overflow becomes visible (v7) #10866

@ab-etemadi

Description

@ab-etemadi

CheckList

  • I agree to follow this project's Code of Conduct
  • I have read and followed the Contributing Guide
  • I have read and followed the Issue Tracker Guide
  • I have searched and referenced existing issues and discussions
  • I am filing a BUG report.
  • I have managed to reproduce the bug after upgrading to the latest version
  • I have created an accurate and minimal reproduction

Version

7.1.0

In What environments are you experiencing the problem?

Chrome

Node Version (if applicable)

22.1.0

Link To Reproduction

https://codesandbox.io/p/sandbox/h2nlfp

Steps To Reproduce

  1. Load an SVG that declares width="1486" height="1080" and viewBox="0 0 1486 1080", but contains geometry that extends outside the viewBox (overflow that is hidden in browser rendering).
  2. Import using Fabric v7 loadSVGFromURL / loadSVGFromString.
  3. Create a group from returned objects (either fabric.util.groupSVGElements(objects, options) or new fabric.Group(objects)).
  4. Observe:
      1. The resulting Fabric group has larger width/height than the SVG viewport (e.g. ~2156×1359 instead of 1486×1080).
      1. Parts of the SVG that are hidden/clipped in browser become visible or affect bounding/selection.

Minimal code:

const { objects, options } = await fabric.loadSVGFromURL(svgURL);

const group = new fabric.Group(objects); // also reproducible with groupSVGElements(objects, options)

console.log("SVG declared:", options.width, options.height, options.viewBoxWidth, options.viewBoxHeight);
console.log("Fabric group:", group.width, group.height);

// Add to canvas
canvas.add(group);
canvas.setActiveObject(group);
canvas.requestRenderAll();

Expected Behavior

  • Imported SVG group dimensions should match the SVG viewport/viewBox (or at least provide an option to):

    • treat SVG bounds as the viewBox/width/height
    • clip overflow consistently with browser SVG viewport behavior
  • Hidden overflow content (outside viewBox / clipped in browser) should not:

    • expand Fabric object bounding boxes
    • become visible unexpectedly
  • Scaling/centering based on SVG width/height/viewBox should produce a correct bounding box aligned with visible content.

Actual Behavior

  • Fabric v7 produces a group bounding box larger than the SVG declared size:
    • SVG: 1486×1080
    • Fabric group: around 2156×1359
  • Because bounds are larger than visible viewport, scaling-to-fit using group bounds makes visible artwork appear smaller than expected inside its bounding box.
  • Additionally, content that is hidden/clipped in browser SVG rendering can appear or contribute to bounding box calculations in Fabric.

SVG
Image

ISSUE
Image

Error Message & Stack Trace

None (no thrown error). This is a rendering / bounds calculation issue.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions