This repository was archived by the owner on Apr 24, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
46266194: Board zoom toolbar #158
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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 |
|---|---|---|
| @@ -1 +1,3 @@ | ||
| export const boardGridStep = 40; | ||
| export const baseGridStep = 40; | ||
| export const baseDotSize = 2; | ||
| export const minDotSize = 1; |
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 |
|---|---|---|
| @@ -1 +1 @@ | ||
| export { useWheelZoom } from './useWheelZoom'; | ||
| export { useZoom } from './useWheelZoom'; |
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 |
|---|---|---|
| @@ -1,37 +1,49 @@ | ||
| /* eslint-disable @typescript-eslint/no-explicit-any */ | ||
| // hooks/useWheelZoom.ts | ||
| import Konva from 'konva'; | ||
| import { useCallback } from 'react'; | ||
| import { calculateZoom, defaultZoomConfig } from '../utils'; | ||
| import { useUIStore } from '../store'; | ||
| import { roundScale, zoomLevels } from '../utils/zoomConfig'; | ||
|
|
||
| /** | ||
| * Хук для обработки масштабирования (зума) при помощи колесика мыши/тачпада. | ||
| * Принимает ссылку на Stage и возвращает обработчик onWheel. | ||
| */ | ||
| export const useWheelZoom = (stageRef: React.RefObject<any>) => { | ||
| const { setScale } = useUIStore(); | ||
| export const useZoom = (stageRef: React.RefObject<Konva.Stage | null>) => { | ||
| const { setScale, setStagePosition } = useUIStore(); | ||
|
|
||
| const handleWheel = useCallback( | ||
| (e: any) => { | ||
| (e: Konva.KonvaEventObject<WheelEvent>) => { | ||
| e.evt.preventDefault(); | ||
|
|
||
| const stage = stageRef.current; | ||
| if (!stage) return; | ||
|
|
||
| const scaleBy = 1.1; | ||
| const oldScale = stage.scaleX(); | ||
| const pointer = stage.getPointerPosition(); | ||
| if (!pointer) return; | ||
|
|
||
| // Определяем координаты точки, на которую указывает курсор, относительно текущего масштаба | ||
| const mousePointTo = { | ||
| x: (pointer.x - stage.x()) / oldScale, | ||
| y: (pointer.y - stage.y()) / oldScale, | ||
| }; | ||
|
|
||
| // Если прокрутка вниз – уменьшаем масштаб, иначе – увеличиваем | ||
| let newScale = e.evt.deltaY > 0 ? oldScale / scaleBy : oldScale * scaleBy; | ||
| // Ограничиваем масштаб от 50% до 300% | ||
| newScale = Math.max(0.5, Math.min(newScale, 3)); | ||
| const delta = e.evt.deltaY; | ||
|
|
||
| const baseScaleStep = 0.01; | ||
|
|
||
| const adjustedFactor = Math.max(0.1, oldScale * 2); | ||
|
|
||
| const scaleStep = baseScaleStep * adjustedFactor; | ||
|
|
||
| let newScale = delta > 0 ? oldScale - scaleStep : oldScale + scaleStep; | ||
|
|
||
| newScale = Math.max( | ||
| defaultZoomConfig.minScale, | ||
| Math.min(newScale, defaultZoomConfig.maxScale), | ||
| ); | ||
|
|
||
| newScale = roundScale(newScale); | ||
| setScale(newScale); | ||
|
|
||
| // Обновляем масштаб и позицию Stage, чтобы точка под курсором оставалась на месте | ||
|
|
@@ -46,5 +58,78 @@ export const useWheelZoom = (stageRef: React.RefObject<any>) => { | |
| [stageRef, setScale], | ||
| ); | ||
|
|
||
| return handleWheel; | ||
| const handleZoom = useCallback( | ||
| (direction: 'in' | 'out') => { | ||
| const stage = stageRef.current; | ||
| if (!stage) return; | ||
|
|
||
| const oldScale = stage.scaleX(); | ||
|
|
||
| const uniqueZoomLevels = [...new Set([...zoomLevels, oldScale])].sort((a, b) => a - b); | ||
| const currentIndex = uniqueZoomLevels.indexOf(oldScale); | ||
|
|
||
| let newScale = oldScale; | ||
|
|
||
| if (direction === 'in') { | ||
| if (currentIndex < uniqueZoomLevels.length - 1) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. А нельзя ли объединить этот if с if выше? |
||
| newScale = uniqueZoomLevels[currentIndex + 1]; | ||
| } | ||
| } else if (currentIndex > 0) { | ||
| newScale = uniqueZoomLevels[currentIndex - 1]; | ||
| } | ||
|
|
||
| if (newScale === oldScale) return; | ||
|
|
||
| // Ограничиваем масштаб в рамках minScale и maxScale | ||
| newScale = Math.max( | ||
| defaultZoomConfig.minScale, | ||
| Math.min(newScale, defaultZoomConfig.maxScale), | ||
| ); | ||
|
|
||
| const result = calculateZoom(stageRef, newScale, null, defaultZoomConfig); | ||
| if (!result) return; | ||
|
|
||
| const { newScale: finalScale, newPos } = result; | ||
|
|
||
| stage.to({ | ||
| scaleX: finalScale, | ||
| scaleY: finalScale, | ||
| x: newPos.x, | ||
| y: newPos.y, | ||
| duration: defaultZoomConfig.animationDuration / 1000, | ||
| easing: Konva.Easings.Linear, | ||
| onUpdate: () => { | ||
| setScale(finalScale); | ||
| }, | ||
| }); | ||
|
|
||
| setStagePosition({ x: newPos.x, y: newPos.y }); | ||
| stage.batchDraw(); | ||
| }, | ||
| [setScale, setStagePosition, stageRef], | ||
| ); | ||
|
|
||
| const handleZoomIn = useCallback(() => handleZoom('in'), [handleZoom]); | ||
| const handleZoomOut = useCallback(() => handleZoom('out'), [handleZoom]); | ||
|
|
||
| const handleResetZoom = useCallback(() => { | ||
| const stage = stageRef.current; | ||
| if (!stage) return; | ||
|
|
||
| setScale(1); | ||
| setStagePosition({ x: stage.x(), y: stage.y() }); | ||
|
|
||
| stage.to({ | ||
| scaleX: 1, | ||
| scaleY: 1, | ||
| x: stage.x(), | ||
| y: stage.y(), | ||
| duration: defaultZoomConfig.animationDuration / 1000, | ||
| easing: Konva.Easings.Linear, | ||
| }); | ||
|
|
||
| stage.batchDraw(); | ||
| }, [stageRef, setScale, setStagePosition]); | ||
|
|
||
| return { handleWheel, handleZoomIn, handleZoomOut, handleResetZoom }; | ||
| }; | ||
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,36 @@ | ||
| import { Stage } from 'konva/lib/Stage'; | ||
| import { Point, ZoomConfig } from '../types'; | ||
|
|
||
| export const calculateZoom = ( | ||
| stageRef: React.RefObject<Stage | null>, | ||
| newScale: number, | ||
| pointer: Point | null, | ||
| config: ZoomConfig, | ||
| ): { newScale: number; newPos: Point } | null => { | ||
| const stage = stageRef?.current; | ||
| if (!stage) return null; | ||
|
|
||
| const { minScale, maxScale } = config; | ||
|
|
||
| if (newScale > maxScale || newScale < minScale) return null; | ||
|
|
||
| const oldScale = stage.scaleX(); | ||
|
|
||
| const center = { x: stage.width() / 2, y: stage.height() / 2 }; | ||
| const zoomPointer = pointer || center; | ||
|
|
||
| const mousePointTo = { | ||
| x: zoomPointer.x / oldScale - stage.x() / oldScale, | ||
| y: zoomPointer.y / oldScale - stage.y() / oldScale, | ||
| }; | ||
|
|
||
| const newPos = { | ||
| x: zoomPointer.x - mousePointTo.x * newScale, | ||
| y: zoomPointer.y - mousePointTo.y * newScale, | ||
| }; | ||
|
|
||
| newPos.x = Math.round(newPos.x * 100) / 100; | ||
| newPos.y = Math.round(newPos.y * 100) / 100; | ||
|
|
||
| return { newScale, newPos }; | ||
| }; |
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Как будто константы можно вне компонента выносить