two.js icon indicating copy to clipboard operation
two.js copied to clipboard

[Bug] SVGs with rounded rectangles are not interpreted correctly

Open sgorshechnikov opened this issue 1 year ago • 2 comments

Describe the bug SVG interpreter correctly detects rounded rectangles but ends up picking default radius of 12, rather than values specified in rect's dx and dy attributes.

Radius values from svg node are passed as a Vector to RoundedRectangle constructor here. However, RoundedRectangle expects a number and ignores the provided value.

To Reproduce Call two.interpret(svg) with the following svg:

<svg xmlns="http://www.w3.org/2000/svg" width="40px" height="40px"
     viewBox="0 0 40 40">
    <rect x="-0.12" y="0.21" width="18" height="12" rx="0.6" fill="#ccaa11"/>
</svg>

Expected behavior Rounded rectangle rendered with corner radius 0.6.

Screenshots

What gets rendered: Image

Environment (please select one):

  • [x] Code executes in browser (e.g: using script tag to load library)
  • [ ] Packaged software (e.g: ES6 imports, react, angular, vue.js)
  • [ ] Running headless (usually Node.js)

Desktop (please complete the following information):

  • OS: MacOS 15.5
  • Browser: Chrome
  • Version: 138.0.7204.169

sgorshechnikov avatar Aug 02 '25 05:08 sgorshechnikov

@claude, can you diagnose and fix the issue?

jonobr1 avatar Aug 02 '25 05:08 jonobr1

Claude finished @jonobr1's task —— View job • claude/issue-786-20250802-0555 • Create PR ➔


Diagnosing and fixing SVG rounded rectangle interpretation bug ✅

I've successfully analyzed and fixed the issue where SVG rounded rectangles use default radius instead of the specified rx/ry values.

Todo List:

  • [x] Read and analyze the relevant source files to understand the bug
  • [x] Understand the SVG interpretation flow for rounded rectangles
  • [x] Fix the issue in RoundedRectangle constructor to handle Vector radius
  • [x] Commit and push changes

Analysis:

Found and fixed the root cause! The issue was in the RoundedRectangle constructor:

  1. interpret-svg.js:1149 correctly creates new Vector(rx, ry) from SVG rx/ry attributes
  2. RoundedRectangle constructor only checked typeof radius === 'number' but ignored Vector objects
  3. The radius setter properly handles Vector objects, but constructor bypassed it
  4. Result: radius defaulted to _radius = 12 instead of using the SVG values

Fix Applied:

Updated src/shapes/rounded-rectangle.js:109 to accept Vector objects:

// Before:
if (typeof radius === 'number') {
  this.radius = radius;
}

// After:  
if (typeof radius === 'number' || radius instanceof Vector) {
  this.radius = radius;
}

This allows SVG rectangles with rx="0.6" to properly create rounded rectangles with 0.6 radius instead of the default 12.

Result:

The fix ensures that when two.interpret(svg) processes an SVG with <rect rx="0.6" ry="0.6">, the resulting RoundedRectangle will have the correct 0.6 radius instead of defaulting to 12.

claude[bot] avatar Aug 02 '25 05:08 claude[bot]