Skip to content

fix(gcode): cut rotated shapes where they are drawn - #18

Open
inzuno wants to merge 1 commit into
SJLS27:mainfrom
inzuno:fix-rotated-cut-offset
Open

inzuno wants to merge 1 commit into
SJLS27:mainfrom
inzuno:fix-rotated-cut-offset

Conversation

@inzuno

@inzuno inzuno commented Dec 5, 2026

Copy link
Copy Markdown

Fixes #17

Problem

Rotated shapes were cut several millimetres away from where the canvas draws them (about 7 mm for a 34 mm disc at 45°, up to 15 mm for larger parts), and unrotated ones by half the stroke width. Details and measurements in the issue.

Change

src/geometry/paperOps.ts: objectToScenePathItems, objectToSceneCompoundPathData and objectToScenePathData no longer translate the imported geometry onto getBoundingRect(). toSVG() already carries the object's full transform, so the imported geometry is where the canvas draws it. These three functions feed line cuts, fills and offset fills. Booleans, convert-to-path and the rest do not go through them.

Testing

  • New src/geometry/paperOps.test.ts: rects rotated by 0°, 30°, 45°, 90° and 137°, and a rotated, scaled group, are cut where Fabric draws them (within 0.005 mm, the rounding of toSVG()), for contours, fills and offset fills. All six tests fail without the fix.
  • src/gcode/job.test.ts (device origin): the test rect used Fabric's default 1 mm stroke, whose outline really is half the stroke inside left/top. It now has no stroke, so the expected coordinates are unchanged.
  • Checked in the app against Fabric's own geometry (each leaf's points through its absolute matrix) for rects, ellipses, circles, polygons, curved paths, groups, nested groups and imported SVG groups, at 0°, 30°, 45°, 120° with a 1.5× scale and 200° mirrored: worst offset 0.03 mm (curve flattening), against up to 15 mm before.
  • Manual check in the app: generateJob (the plan used by the preview and for sending to the machine) for a circle rotated 45° and an L-shaped polygon rotated 40°. The cut path is now centred on the drawn shape (offset 0.000 / 0.001 mm, against 11.9 / 19.3 mm on main). There are no console errors. Not run through the GRBL simulator: the change sits before G-code emission, and the emitted G-code follows the same plan.
  • npm run typecheck, npm run test (485 tests), npm run build and npm run test:e2e (38 passed, 3 skipped) pass.

Note: the X/Y fields of the shape properties still show the bounding box including the stroke, so with a thick stroke the cut now sits half a stroke inside the displayed X/Y, exactly where the outline is drawn.

🤖 Generated with Claude Code

The scene geometry used for cuts, fills and offset fills was imported from
the object's own SVG (which already carries its full transform) and then
translated so its top-left matched getBoundingRect(). That box includes the
stroke and, for a rotated object, is the axis-aligned hull of the rotated
box, so it is larger than the shape itself: rotated parts were cut several
millimetres away from where the canvas draws them (about 7 mm for a 34 mm
disc at 45°, up to 15 mm for larger parts), and unrotated ones by half the
stroke width.

Drop the re-alignment. Measured against Fabric's own geometry (each leaf's
points through its absolute matrix), rects, ellipses, circles, polygons,
curved paths, groups and nested groups now match within 0.03 mm at any
angle, scale or mirror.

The origin tests used a rect with Fabric's default 1 mm stroke, whose
outline really is half the stroke inside left/top; they now use no stroke
so the expected coordinates stay the same.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

Rotated shapes are cut several millimetres away from where they are drawn

1 participant