52 lines
1.4 KiB
JavaScript
52 lines
1.4 KiB
JavaScript
import { useRecoilState, useRecoilValue, useResetRecoilState } from 'recoil'
|
|
import { canvasState, dotLineGridSettingState, dotLineIntervalSelector } from '@/store/canvasAtom'
|
|
import { calculateDistance } from '@/util/canvas-util'
|
|
import { useAdsorptionPoint } from '@/hooks/useAdsorptionPoint'
|
|
|
|
export function useDotLineGrid() {
|
|
const canvas = useRecoilValue(canvasState)
|
|
const [dotLineGridSetting, setDotLineGridSettingState] = useRecoilState(dotLineGridSettingState)
|
|
const interval = useRecoilValue(dotLineIntervalSelector) // 가로 세로 간격
|
|
|
|
const { adsorptionRange } = useAdsorptionPoint()
|
|
|
|
const resetDotLineGridSetting = useResetRecoilState(dotLineGridSettingState)
|
|
|
|
const getLineGrids = () => {
|
|
return canvas.getObjects().filter((obj) => obj.name === 'lineGrid')
|
|
}
|
|
|
|
const getClosestLineGrid = (point) => {
|
|
const lines = getLineGrids()
|
|
if (lines.length === 0) {
|
|
return null
|
|
}
|
|
|
|
let closestLine = null
|
|
let minDistance = Infinity
|
|
|
|
lines.forEach((line) => {
|
|
const distance = calculateDistance(point, line)
|
|
|
|
if (distance < minDistance) {
|
|
minDistance = distance
|
|
closestLine = line
|
|
}
|
|
})
|
|
|
|
if (minDistance > adsorptionRange) {
|
|
return null
|
|
}
|
|
|
|
return closestLine
|
|
}
|
|
|
|
return {
|
|
dotLineGridSetting,
|
|
resetDotLineGridSetting,
|
|
getLineGrids,
|
|
getClosestLineGrid,
|
|
interval,
|
|
}
|
|
}
|