Skip to content

Path2D: keep half-circle SVG arcs from turning into straight lines - #1910

Closed
SwastikTripathi wants to merge 1 commit into
melonjs:masterfrom
SwastikTripathi:fix/svg-arc-semicircle-nan
Closed

SwastikTripathi wants to merge 1 commit into
melonjs:masterfrom
SwastikTripathi:fix/svg-arc-semicircle-nan

Conversation

@SwastikTripathi

Copy link
Copy Markdown
Contributor

Description

An SVG A command that describes exactly a half circle is sometimes drawn as a straight line between its endpoints. For example M 0 0 A 13 13 0 0 1 24 10 (endpoints 26 apart, radius 13) or M 0 0 A 1 1 0 0 1 1 12 (radius too small, so it is scaled up to fit, which always gives a half circle).

Root cause: vec2Angle() in toarccanvas.ts computes the sweep as Math.acos(dot / (|u| * |v|)). For a half circle the two radius vectors are opposite, and rounding can put that cosine just below -1 (for example -1.0000000000000002). Math.acos then returns NaN, so endAngle is NaN, the point loop in Path2D.parseSVGPath runs zero times, and only the final lineTo(x, y) is drawn. In a quick sweep of half circles with integer endpoints and radii, about 5.7% hit this, and about 14% of the "radius too small" arcs did.

The fix clamps the cosine to [-1, 1] before Math.acos, the same way most arc-conversion code does.

Type of change

  • Bug fix
  • New feature
  • Documentation update
  • Performance improvement
  • Refactoring (no functional changes)

Checklist

  • I have read the Contributing Guide
  • My code follows the existing code style (pnpm lint passes)
  • I have tested my changes locally (pnpm test passes)
  • I have added tests that cover my changes (if applicable)
  • The build succeeds (pnpm build)

Testing: two new cases in tests/path2d.spec.js check that every generated point lies on the expected circle. Both fail before the change (only 2 points, the straight line) and pass after. The full browser suite passes except audio-stop-onend.spec.js (2 tests), which fails the same way on master here, and one flaky timeout in the WebGL fuzz test that passed on rerun. ESLint and Biome are clean on the changed files. I ran ESLint and Biome on the changed files rather than the whole pnpm lint, and didn't run pnpm build.

Changelog entry added under Fixed.

Related issues

None, found while checking SVG arc conversion in a few renderers.

vec2Angle() passed the normalized dot product of the two radius vectors
straight to Math.acos. For a half circle the vectors are opposite and
rounding can put the cosine just below -1, so the sweep angle was NaN,
no arc points were generated and only the closing lineTo was drawn.
Clamp the cosine to [-1, 1].
Copilot AI balanced review requested due to automatic review settings November 21, 2026 02:54

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@obiot

obiot commented Nov 21, 2026

Copy link
Copy Markdown
Member

nice catch ! I was just about to publish version 20.17, will wait to confirm and merge this one, thanks !

@obiot

obiot commented Nov 21, 2026

Copy link
Copy Markdown
Member

I'll add a test case here as well https://melonjs.github.io/melonJS/examples/#/svg-shapes

obiot pushed a commit that referenced this pull request Nov 21, 2026
Six shapes whose arcs are exactly half a circle with their ends on a slant,
or ask for a radius too small to reach between them: the cases in which an
SVG arc is drawn as the straight line between its ends (#1910).

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JnzwA4a7ZyjLBpbeEpFLu6
obiot pushed a commit that referenced this pull request Nov 21, 2026
Path2D: keep half-circle SVG arcs from turning into straight lines, by
Swastik Tripathi (#1910), merged here to resolve its conflict
with master in the changelog: both sets of entries are kept.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JnzwA4a7ZyjLBpbeEpFLu6

obiot commented Nov 21, 2026

Copy link
Copy Markdown
Member

Confirmed, thanks for the catch and the clear write-up.

  • On master, both arcs you name convert with a NaN end angle, so only the closing lineTo is drawn. With your change they convert correctly.
  • In a sweep of chords with integer endpoints, 8% of exact half circles and 14.7% of arcs whose radius is too small failed before, none after.
  • No regression: the existing rows of the SVG Shapes example are pixel for pixel identical before and after, and the path, arc and ellipse specs pass with your two new cases.

The SVG Shapes example has a new last row for this: six shapes whose arcs are exactly half a circle with their ends on a slant, or ask for a radius too small to reach (a half disc, a disc, a capsule, a wave, an awning, a half moon). Without the fix they are straight lines; with it they draw as intended:

https://melonjs.github.io/melonJS/examples/#/svg-shapes

One practical note: this branch now conflicts with master in the changelog (another change landed in the same Fixed section just before). So that you don't have to rebase, your commit is carried unchanged, under your name, in #1912, merged with master with both sets of changelog entries kept, including yours. It will be merged from there, and the fix goes out with 20.17.0.


Generated by Claude Code

obiot added a commit that referenced this pull request Nov 21, 2026
…1912)

An SVG `A` arc that is exactly a half circle, as when its radius is too small and is scaled up to fit, is drawn as an arc. Rounding could push the cosine of its sweep angle just past -1, `Math.acos` returned `NaN`, and the arc became a straight line between its endpoints. `vec2Angle()` now clamps the cosine to [-1, 1].

The fix and its tests are by Swastik Tripathi, from #1910, carried here unchanged to resolve a conflict with master in the changelog.

Co-authored-by: Swastik Tripathi <88632085+SwastikTripathi@users.noreply.github.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JnzwA4a7ZyjLBpbeEpFLu6
@obiot obiot closed this in #1912 Nov 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants