Repository navigation
feat: polygon tool (#96) - #197
Merged
Merged
Conversation
tool="polygon", Tools.Polygon and Types.Tool.POLYGON (#96). Each click or tap places a vertex where the pointer is released. The polygon is closed by a click or tap on the first vertex, a double click or double tap, or the Enter key, and the Escape key cancels it. The polygon in progress is drawn on the top canvas only, it becomes one fabric.Polygon and one undo step when it is closed. FabricCanvasTool gets finish(), called before dispose() when another tool takes over, and notifiesChange(), which replaces the check of the laser pointer in the events manager.
… what they do The eraser cuts the polygon, the cut is saved and undone. The select tool moves, scales and rotates it as a whole, by drags of the mouse. The export has nothing of the polygon in progress, and has the polygon once it is closed.
The vertices are in canvas coordinates, so a zoom or a pan of the view in the middle of a polygon keeps the vertices placed, places the next ones in the new view, draws the preview in it, and keeps the radius of the first vertex in screen pixels.
…nvas zoom() and the resize of the canvas scale the objects, so they scale the vertices of the polygon in progress too, through the new scale() hook of the tools. autoresize() returns the factors it scaled by.
The visible hint is short enough for the narrowest layout (a container of 228 pixels), and the button has the full instructions as its tooltip.
This was referenced Oct 28, 2026
Merged
- The first click after a polygon is closed or cancelled is a vertex, it was dropped as the second click of a double click. - A double click needs the two releases within 4 pixels (10 on touch), so two vertices placed quickly near one another do not close the polygon. - The polygon in progress is dropped when the sketch is cleared, a value is loaded, or an undo or a redo is done, through the new reset() hook of the tools. - The keys are taken when the focus is on the page or in the sketch, not on a button or a dialog. Of two sketches, the one pressed last takes them.
Draft
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds the polygon tool for 1.1.0 (issue #96):
tool="polygon",Tools.Polygon,Types.Tool.POLYGON.toJSON()ortoDataURL(), and not in the undo history. When it is closed it is onefabric.Polygonand one undo step,onObjectAddedis called once with it, andonChangeis called once, not for each vertex.onMouseDownandonMouseUpare still called for every click.lineColor,lineWidthandfillColor(transparent by default) as the rectangle does. It is a shape like the others: the select tool moves, scales and rotates it as a whole, the eraser cuts it, andtoJSON()andfromJSON()save and load it. The README says how to read its vertices frompoints.zoom()and the resize of the canvas scale the polygon in progress with the objects.finish()(called beforedispose()when another tool takes over),scale()(called when the sketch scales its objects) andnotifiesChange(), which replaces the check of the laser pointer in the events manager.autoresize()returns the factors it scaled by.Checks
yarn format:check,yarn lint,yarn typecheckandyarn buildpass.yarn test: 334 passing and 1 todo, on React 19 and on React 18 (58 tests for the polygon). Changing the code on purpose made the tests fail in each case I tried (the closing gestures, the radius in screen pixels, the eraser mark, the transforms of the select tool, the export, the scaling).In headless Chrome, driven through the DevTools protocol with real mouse and touch events: the closing gestures, the preview and the ring, Escape and Enter, a polygon closed by touch, the export with a polygon in progress (identical to the export of the empty sketch), a resize of the window in the middle of a polygon, and the hint on one line at every window width from 320 to 1920 pixels.
Not checked: a physical touch device, and browsers other than Chrome.
Notes for review
release/1.1.0, so Draw Polygons? #96 needs to be in theFixesline of the release PR (Release v1.1.0 #196).