Repository navigation
Path2D: keep half-circle SVG arcs from turning into straight lines - #1910
SwastikTripathi wants to merge 1 commit into
Conversation
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].
|
nice catch ! I was just about to publish version 20.17, will wait to confirm and merge this one, thanks ! |
|
I'll add a test case here as well https://melonjs.github.io/melonJS/examples/#/svg-shapes |
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
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
|
Confirmed, thanks for the catch and the clear write-up.
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 Generated by Claude Code |
…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
Description
An SVG
Acommand that describes exactly a half circle is sometimes drawn as a straight line between its endpoints. For exampleM 0 0 A 13 13 0 0 1 24 10(endpoints 26 apart, radius 13) orM 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()intoarccanvas.tscomputes the sweep asMath.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.acosthen returnsNaN, soendAngleisNaN, the point loop inPath2D.parseSVGPathruns zero times, and only the finallineTo(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
Checklist
pnpm lintpasses)pnpm testpasses)pnpm build)Testing: two new cases in
tests/path2d.spec.jscheck 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 exceptaudio-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 wholepnpm lint, and didn't runpnpm build.Changelog entry added under Fixed.
Related issues
None, found while checking SVG arc conversion in a few renderers.