-
Notifications
You must be signed in to change notification settings - Fork 0
feat(canvas): add 90 deg quick-rotate button for step-rotation objects #53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| import { useEffect, useRef, useState } from "react"; | ||
| import { Group, Circle, Path } from "react-konva"; | ||
| import type Konva from "konva"; | ||
|
|
||
| /** | ||
| * Small floating "rotate 90°" button rendered next to the selected object's | ||
| * top-right corner. Only used for step-rotation objects (text, serial, | ||
| * barcodes) where rotation is N/R/I/B rather than a free angle from the | ||
| * Konva Transformer. | ||
| * | ||
| * Positioned in stage-space (relative to the Layer) — the caller resolves | ||
| * the selected node's bbox via getClientRect({ relativeTo: stage }) so the | ||
| * button stays at the visual top-right even when the underlying object is | ||
| * itself rotated to R/I/B. | ||
| */ | ||
| interface Props { | ||
| x: number; | ||
| y: number; | ||
| color: string; | ||
| onClick: () => void; | ||
| } | ||
|
|
||
| const RADIUS = 11; | ||
| // Lucide "rotate-cw" (24x24). Single-arrow rotation glyph — the | ||
| // universally-recognised "rotate 90°" icon (Photoshop / Figma / Word all | ||
| // use this shape). Drawn as a Konva Path scaled down and centred on the | ||
| // button origin via a wrapping Group. | ||
| const ARROW_PATH_ICON = | ||
| "M21 12a9 9 0 1 1-9-9c2.52 0 4.93 1 6.74 2.74L21 8 M21 3v5h-5"; | ||
| const ICON_SCALE = 0.55; | ||
| const ICON_OFFSET = 12; // 24x24 viewBox → centre at (12, 12) | ||
|
|
||
| export function RotationButton({ x, y, color, onClick }: Props) { | ||
| const [hover, setHover] = useState(false); | ||
| // Track the stage we set a cursor on so unmount-while-hovering can still | ||
| // clean up (onMouseLeave never fires in that case — e.g. user hits Delete | ||
| // or Esc while the cursor is over the button). | ||
| const cursorStageRef = useRef<Konva.Stage | null>(null); | ||
|
|
||
| const cursorIn = (e: Konva.KonvaEventObject<MouseEvent>) => { | ||
| const stage = e.target.getStage(); | ||
| if (stage) { | ||
| stage.container().style.cursor = "pointer"; | ||
| cursorStageRef.current = stage; | ||
| } | ||
| setHover(true); | ||
| }; | ||
| const cursorOut = (e: Konva.KonvaEventObject<MouseEvent>) => { | ||
| const stage = e.target.getStage(); | ||
| if (stage) stage.container().style.cursor = ""; | ||
| cursorStageRef.current = null; | ||
| setHover(false); | ||
| }; | ||
|
u8array marked this conversation as resolved.
|
||
|
|
||
| useEffect(() => { | ||
| return () => { | ||
| const stage = cursorStageRef.current; | ||
| if (stage) stage.container().style.cursor = ""; | ||
| }; | ||
| }, []); | ||
|
|
||
| return ( | ||
| <Group | ||
| x={x} | ||
| y={y} | ||
| onMouseEnter={cursorIn} | ||
| onMouseLeave={cursorOut} | ||
| onClick={(e) => { | ||
| e.cancelBubble = true; | ||
| onClick(); | ||
| }} | ||
| onTap={(e) => { | ||
| e.cancelBubble = true; | ||
| onClick(); | ||
| }} | ||
| > | ||
| {/* Invisible hitbox — gives the icon a comfortable click target | ||
| even though the icon path itself is thin. */} | ||
| <Circle radius={RADIUS} fill="transparent" /> | ||
| <Group | ||
| scaleX={ICON_SCALE} | ||
| scaleY={ICON_SCALE} | ||
| offsetX={ICON_OFFSET} | ||
| offsetY={ICON_OFFSET} | ||
| listening={false} | ||
| > | ||
| <Path | ||
| data={ARROW_PATH_ICON} | ||
| stroke={color} | ||
| strokeWidth={hover ? 3 : 2} | ||
| lineCap="round" | ||
| lineJoin="round" | ||
| fill="transparent" | ||
| /> | ||
| </Group> | ||
| </Group> | ||
| ); | ||
| } | ||
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.