import { useEffect, useRef, useState } from 'react' import QRect from '@/components/fabric/QRect' import QPolygon from '@/components/fabric/QPolygon' import { getStartIndex, rearrangeArray, findTopTwoIndexesByDistance, getDirection, getCenterPoint } from '@/util/canvas-util' import { useRecoilState, useSetRecoilState } from 'recoil' import { fontSizeState, roofState, sortedPolygonArray, wallState } from '@/store/canvasAtom' import { QLine } from '@/components/fabric/QLine' export const Mode = { DRAW_LINE: 'drawLine', // 기준선 긋기모드 EDIT: 'edit', TEMPLATE: 'template', PATTERNA: 'patterna', PATTERNB: 'patternb', TEXTBOX: 'textbox', DRAW_RECT: 'drawRect', DEFAULT: 'default', } export function useMode() { const [mode, setMode] = useState() const points = useRef([]) const historyPoints = useRef([]) const historyLines = useRef([]) const [canvas, setCanvas] = useState(null) const [zoom, setZoom] = useState(100) const [fontSize] = useRecoilState(fontSizeState) const [shape, setShape] = useState(0) const [sortedArray, setSortedArray] = useRecoilState(sortedPolygonArray) const [roof, setRoof] = useRecoilState(roofState) const [wall, setWall] = useRecoilState(wallState) const addEvent = (mode) => { switch (mode) { case 'default': canvas?.off('mouse:down') break case 'drawLine': drawLineMode() break case 'edit': editMode() break case 'template': templateMode() break case 'patterna': break case 'patternb': applyTemplateB() break case 'textbox': textboxMode() break case 'drawRect': drawRectMode() break } } const changeMode = (canvas, mode) => { setMode(mode) // mode변경 시 이전 이벤트 제거 setCanvas(canvas) canvas?.off('mouse:down') addEvent(mode) } const editMode = () => { canvas?.on('mouse:down', function (options) { const pointer = canvas?.getPointer(options.e) const circle = new fabric.Circle({ radius: 1, fill: 'transparent', // 원 안을 비웁니다. stroke: 'black', // 원 테두리 색상을 검은색으로 설정합니다. left: pointer.x, top: pointer.y, originX: 'center', originY: 'center', selectable: false, }) historyPoints.current.push(circle) points.current.push(circle) canvas?.add(circle) if (points.current.length === 2) { const length = Number(prompt('길이를 입력하세요:')) // length 값이 숫자가 아닌 경우 if (isNaN(length) || length === 0) { //마지막 추가 된 points 제거합니다. const lastPoint = historyPoints.current[historyPoints.current.length - 1] canvas?.remove(lastPoint) historyPoints.current.pop() points.current.pop() return } if (length) { const vector = { x: points.current[1].left - points.current[0].left, y: points.current[1].top - points.current[0].top, } const slope = Math.abs(vector.y / vector.x) // 기울기 계산 let scaledVector if (slope >= 1) { // 기울기가 1 이상이면 x축 방향으로 그림 scaledVector = { x: 0, y: vector.y >= 0 ? Number(length) : -Number(length), } } else { // 기울기가 1 미만이면 y축 방향으로 그림 scaledVector = { x: vector.x >= 0 ? Number(length) : -Number(length), y: 0, } } const line = new QLine( [points.current[0].left, points.current[0].top, points.current[0].left + scaledVector.x, points.current[0].top + scaledVector.y], { stroke: 'black', strokeWidth: 2, selectable: false, viewLengthText: true, direction: getDirection(points.current[0], points.current[1]), fontSize: fontSize, }, ) pushHistoryLine(line) // 라인의 끝에 점을 추가합니다. const endPointCircle = new fabric.Circle({ radius: 1, fill: 'transparent', // 원 안을 비웁니다. stroke: 'black', // 원 테두리 색상을 검은색으로 설정합니다. left: points.current[0].left + scaledVector.x, top: points.current[0].top + scaledVector.y, originX: 'center', originY: 'center', selectable: false, }) canvas?.add(endPointCircle) historyPoints.current.push(endPointCircle) points.current.forEach((point) => { canvas?.remove(point) }) points.current = [endPointCircle] } } canvas?.renderAll() }) } const pushHistoryLine = (line) => { if (historyLines.current.length > 0 && historyLines.current[historyLines.current.length - 1].direction === line.direction) { // 같은 방향의 선이 두 번 연속으로 그려지면 이전 선을 제거하고, 새로운 선과 merge한다. const lastLine = historyLines.current.pop() canvas?.remove(lastLine) const mergedLine = new QLine([lastLine.x1, lastLine.y1, line.x2, line.y2], { stroke: 'black', strokeWidth: 2, selectable: false, viewLengthText: true, direction: lastLine.direction, fontSize: fontSize, }) historyLines.current.push(mergedLine) canvas?.add(mergedLine) } else { historyLines.current.push(line) canvas?.add(line) } } /** * 마우스로 그린 점 기준으로 외벽선을 완성시켜준다. * makePolygon 함수에 포함되어있던 내용을 다른 템플릿 적용에서도 사용할수 있도록 함수로 대체 */ const drawWallPolygon = () => { const firstPoint = historyPoints.current[0] const lastPoint = historyPoints.current[historyPoints.current.length - 1] historyPoints.current.forEach((point) => { canvas?.remove(point) }) drawLineWithLength(lastPoint, firstPoint) points.current = [] historyPoints.current = [] // handleOuterlines() const wall = makePolygon() setWall(wall) return wall } const templateMode = () => { changeMode(canvas, Mode.EDIT) if (historyPoints.current.length >= 4) { // handleOuterlinesTest() //외곽선 그리기 테스트 // drawWallPolygon() //아래 내용 drawWallPolygon()으로 대체 const firstPoint = historyPoints.current[0] const lastPoint = historyPoints.current[historyPoints.current.length - 1] historyPoints.current.forEach((point) => { canvas?.remove(point) }) drawLineWithLength(lastPoint, firstPoint) points.current = [] historyPoints.current = [] const roof = handleOuterlinesTest() const wall = makePolygon() roof.setWall(wall) setWall(wall) roof.drawHelpLine() } } const textboxMode = () => { canvas?.on('mouse:down', function (options) { if (canvas?.getActiveObject()?.type === 'textbox') return const pointer = canvas?.getPointer(options.e) const textbox = new fabric.Textbox('텍스트를 입력하세요', { left: pointer.x, top: pointer.y, width: 150, // 텍스트박스의 너비를 설정합니다. fontSize: fontSize, // 텍스트의 크기를 설정합니다. }) canvas?.add(textbox) canvas?.setActiveObject(textbox) // 생성된 텍스트박스를 활성 객체로 설정합니다. canvas?.renderAll() // textbox가 active가 풀린 경우 editing mode로 변경 textbox?.on('editing:exited', function () { changeMode(canvas, Mode.EDIT) }) }) } const drawLineMode = () => { canvas?.on('mouse:down', function (options) { const pointer = canvas?.getPointer(options.e) const line = new QLine( [pointer.x, 0, pointer.x, canvas.height], // y축에 1자 선을 그립니다. { stroke: 'black', strokeWidth: 2, viewLengthText: true, selectable: false, fontSize: fontSize, }, ) canvas?.add(line) canvas?.renderAll() }) } const drawRectMode = () => { let rect, isDown, origX, origY canvas.on('mouse:down', function (o) { isDown = true const pointer = canvas.getPointer(o.e) origX = pointer.x origY = pointer.y rect = new fabric.Rect({ left: origX, top: origY, originX: 'left', originY: 'top', width: pointer.x - origX, height: pointer.y - origY, angle: 0, fill: 'transparent', stroke: 'black', transparentCorners: false, }) canvas.add(rect) }) canvas.on('mouse:move', function (o) { if (!isDown) return const pointer = canvas.getPointer(o.e) if (origX > pointer.x) { rect.set({ left: Math.abs(pointer.x) }) } if (origY > pointer.y) { rect.set({ top: Math.abs(pointer.y) }) } rect.set({ width: Math.abs(origX - pointer.x) }) rect.set({ height: Math.abs(origY - pointer.y) }) }) canvas.on('mouse:up', function (o) { const pointer = canvas.getPointer(o.e) const qRect = new QRect({ left: origX, top: origY, originX: 'left', originY: 'top', width: pointer.x - origX, height: pointer.y - origY, angle: 0, viewLengthText: true, fill: 'transparent', stroke: 'black', transparentCorners: false, fontSize: fontSize, }) canvas.remove(rect) canvas.add(qRect) isDown = false }) } /** * 두 점을 연결하는 선과 길이를 그립니다. * a : 시작점, b : 끝점 */ const drawLineWithLength = (a, b) => { const line = new QLine([a.left, a.top, b.left, b.top], { stroke: 'black', strokeWidth: 2, selectable: false, viewLengthText: true, direction: getDirection(a, b), fontSize: fontSize, }) pushHistoryLine(line) canvas?.renderAll() } const makePolygon = (otherLines) => { // 캔버스에서 모든 라인 객체를 찾습니다. const lines = otherLines || historyLines.current if (!otherLines) { //외각선 기준 const topIndex = findTopTwoIndexesByDistance(sortedArray) //배열중에 큰 2값을 가져옴 TODO: 나중에는 인자로 받아서 다각으로 수정 해야됨 //일단 배열 6개 짜리 기준의 선 번호 if (topIndex[0] === 4) { if (topIndex[1] === 5) { //1번 setShape(1) } } else if (topIndex[0] === 1) { //4번 if (topIndex[1] === 2) { setShape(4) } } else if (topIndex[0] === 0) { if (topIndex[1] === 1) { //2번 setShape(2) } else if (topIndex[1] === 5) { setShape(3) } } historyLines.current = [] } // 각 라인의 시작점과 끝점을 사용하여 다각형의 점 배열을 생성합니다. const points = lines.map((line) => ({ x: line.x1, y: line.y1 })) // 모든 라인 객체를 캔버스에서 제거합니다. lines.forEach((line) => { canvas?.remove(line) }) // 점 배열을 사용하여 새로운 다각형 객체를 생성합니다. const polygon = new QPolygon( points, { stroke: 'black', fill: 'transparent', viewLengthText: true, selectable: true, fontSize: fontSize, }, canvas, ) // 새로운 다각형 객체를 캔버스에 추가합니다. canvas.add(polygon) // 캔버스를 다시 그립니다. if (!otherLines) { // polygon.fillCell() canvas.renderAll() polygon.setViewLengthText(false) setMode(Mode.DEFAULT) } return polygon } /** * 해당 캔버스를 비운다. */ const handleClear = () => { canvas?.clear() points.current = [] historyPoints.current = [] historyLines.current = [] } const zoomIn = () => { canvas?.setZoom(canvas.getZoom() + 0.1) setZoom(Math.round(zoom + 10)) } const zoomOut = () => { canvas?.setZoom(canvas.getZoom() - 0.1) setZoom(Math.ceil(zoom - 10)) } const handleOuterlines = () => { const newOuterlines = [] for (let i = 0; i < historyLines.current.length; i++) { const next = historyLines.current[i + 1] const prev = historyLines.current[i - 1] ?? historyLines.current[historyLines.current.length - 1] if (next) { if (next.direction === 'right') { // 다름 라인이 오른쪽으로 이동 if (historyLines.current[i].direction === 'top') { if (prev.direction !== 'right') { if (historyLines.current.length === 4) { newOuterlines.push({ x1: historyLines.current[i].x1 - 50, y1: historyLines.current[i].y1 + 50, x2: historyLines.current[i].x2 - 50, y2: historyLines.current[i].y2 - 50, }) } else if (historyLines.current.length === 6) { newOuterlines.push({ x1: historyLines.current[i].x1 - 50, y1: historyLines.current[i].y1 + 50, x2: historyLines.current[i].x2 - 50, y2: historyLines.current[i].y2 - 50, }) } else if (historyLines.current.length === 8) { newOuterlines.push({ x1: historyLines.current[i].x1 + 50, y1: historyLines.current[i].y1 - 50, x2: historyLines.current[i].x2 + 50, y2: historyLines.current[i].y2 + 50, }) } } else { newOuterlines.push({ x1: historyLines.current[i].x1 + 50, y1: historyLines.current[i].y1 + 50, x2: historyLines.current[i].x2 + 50, y2: historyLines.current[i].y2 + 50, }) } } else { // bottom if (prev?.direction !== 'right') { if (historyLines.current.length === 4) { newOuterlines.push({ x1: historyLines.current[i].x1 - 50, y1: historyLines.current[i].y1 - 50, x2: historyLines.current[i].x2 - 50, y2: historyLines.current[i].y2 + 50, }) } else if (historyLines.current.length === 6) { newOuterlines.push({ x1: historyLines.current[i].x1 - 50, y1: historyLines.current[i].y1 - 50, x2: historyLines.current[i].x2 - 50, y2: historyLines.current[i].y2 + 50, }) } else if (historyLines.current.length === 8) { newOuterlines.push({ x1: historyLines.current[i].x1 + 50, y1: historyLines.current[i].y1 + 50, x2: historyLines.current[i].x2 + 50, y2: historyLines.current[i].y2 - 50, }) } } else { newOuterlines.push({ x1: historyLines.current[i].x1 - 50, y1: historyLines.current[i].y1 + 50, x2: historyLines.current[i].x2 - 50, y2: historyLines.current[i].y2 + 50, }) } } } else if (next.direction === 'left') { if (historyLines.current[i].direction === 'top') { if (prev?.direction !== 'left') { if (historyLines.current.length === 4) { newOuterlines.push({ x1: historyLines.current[i].x1 + 50, y1: historyLines.current[i].y1 + 50, x2: historyLines.current[i].x2 + 50, y2: historyLines.current[i].y2 - 50, }) } else if (historyLines.current.length === 6) { newOuterlines.push({ x1: historyLines.current[i].x1 + 50, y1: historyLines.current[i].y1 + 50, x2: historyLines.current[i].x2 + 50, y2: historyLines.current[i].y2 - 50, }) } else if (historyLines.current.length === 8) { newOuterlines.push({ x1: historyLines.current[i].x1 - 50, y1: historyLines.current[i].y1 - 50, x2: historyLines.current[i].x2 - 50, y2: historyLines.current[i].y2 + 50, }) } } else { newOuterlines.push({ x1: historyLines.current[i].x1 + 50, y1: historyLines.current[i].y1 - 50, x2: historyLines.current[i].x2 + 50, y2: historyLines.current[i].y2 - 50, }) } } else { // bottom if (prev?.direction !== 'left') { if (historyLines.current.length === 4) { newOuterlines.push({ x1: historyLines.current[i].x1 + 50, y1: historyLines.current[i].y1 - 50, x2: historyLines.current[i].x2 + 50, y2: historyLines.current[i].y2 + 50, }) } else if (historyLines.current.length === 6) { newOuterlines.push({ x1: historyLines.current[i].x1 + 50, y1: historyLines.current[i].y1 - 50, x2: historyLines.current[i].x2 + 50, y2: historyLines.current[i].y2 + 50, }) } else if (historyLines.current.length === 8) { newOuterlines.push({ x1: historyLines.current[i].x1 - 50, y1: historyLines.current[i].y1 + 50, x2: historyLines.current[i].x2 - 50, y2: historyLines.current[i].y2 - 50, }) } } else { newOuterlines.push({ x1: historyLines.current[i].x1 - 50, y1: historyLines.current[i].y1 - 50, x2: historyLines.current[i].x2 - 50, y2: historyLines.current[i].y2 - 50, }) } } } else if (next.direction === 'top') { if (historyLines.current[i].direction === 'right') { if (prev?.direction !== 'top') { if (historyLines.current.length === 4) { newOuterlines.push({ x1: historyLines.current[i].x1 - 50, y1: historyLines.current[i].y1 + 50, x2: historyLines.current[i].x2 + 50, y2: historyLines.current[i].y2 + 50, }) } else if (historyLines.current.length === 6) { newOuterlines.push({ x1: historyLines.current[i].x1 - 50, y1: historyLines.current[i].y1 + 50, x2: historyLines.current[i].x2 + 50, y2: historyLines.current[i].y2 + 50, }) } else if (historyLines.current.length === 8) { newOuterlines.push({ x1: historyLines.current[i].x1 + 50, y1: historyLines.current[i].y1 - 50, x2: historyLines.current[i].x2 - 50, y2: historyLines.current[i].y2 - 50, }) } } else { newOuterlines.push({ x1: historyLines.current[i].x1 + 50, y1: historyLines.current[i].y1 + 50, x2: historyLines.current[i].x2 + 50, y2: historyLines.current[i].y2 + 50, }) } } else { // left if (prev?.direction !== 'top') { if (historyLines.current.length === 4) { newOuterlines.push({ x1: historyLines.current[i].x1 + 50, y1: historyLines.current[i].y1 + 50, x2: historyLines.current[i].x2 - 50, y2: historyLines.current[i].y2 + 50, }) } else if (historyLines.current.length === 6) { newOuterlines.push({ x1: historyLines.current[i].x1 + 50, y1: historyLines.current[i].y1 + 50, x2: historyLines.current[i].x2 - 50, y2: historyLines.current[i].y2 + 50, }) } else if (historyLines.current.length === 8) { newOuterlines.push({ x1: historyLines.current[i].x1 - 50, y1: historyLines.current[i].y1 - 50, x2: historyLines.current[i].x2 + 50, y2: historyLines.current[i].y2 - 50, }) } } else { newOuterlines.push({ x1: historyLines.current[i].x1 + 50, y1: historyLines.current[i].y1 - 50, x2: historyLines.current[i].x2 + 50, y2: historyLines.current[i].y2 - 50, }) } } } else if (next.direction === 'bottom') { if (historyLines.current[i].direction === 'right') { if (prev?.direction !== 'bottom') { if (historyLines.current.length === 4) { newOuterlines.push({ x1: historyLines.current[i].x1 - 50, y1: historyLines.current[i].y1 - 50, x2: historyLines.current[i].x2 + 50, y2: historyLines.current[i].y2 - 50, }) } else if (historyLines.current.length === 6) { newOuterlines.push({ x1: historyLines.current[i].x1 - 50, y1: historyLines.current[i].y1 - 50, x2: historyLines.current[i].x2 + 50, y2: historyLines.current[i].y2 - 50, }) } else if (historyLines.current.length === 8) { newOuterlines.push({ x1: historyLines.current[i].x1 + 50, y1: historyLines.current[i].y1 + 50, x2: historyLines.current[i].x2 - 50, y2: historyLines.current[i].y2 + 50, }) } } else { newOuterlines.push({ x1: historyLines.current[i].x1 - 50, y1: historyLines.current[i].y1 + 50, x2: historyLines.current[i].x2 - 50, y2: historyLines.current[i].y2 + 50, }) } } else { // left if (prev.direction !== 'bottom') { if (historyLines.current.length === 4) { newOuterlines.push({ x1: historyLines.current[i].x1 + 50, y1: historyLines.current[i].y1 - 50, x2: historyLines.current[i].x2 - 50, y2: historyLines.current[i].y2 - 50, }) } else if (historyLines.current.length === 6) { newOuterlines.push({ x1: historyLines.current[i].x1 + 50, y1: historyLines.current[i].y1 - 50, x2: historyLines.current[i].x2 - 50, y2: historyLines.current[i].y2 - 50, }) } else if (historyLines.current.length === 8) { newOuterlines.push({ x1: historyLines.current[i].x1 - 50, y1: historyLines.current[i].y1 + 50, x2: historyLines.current[i].x2 + 50, y2: historyLines.current[i].y2 + 50, }) } } else { newOuterlines.push({ x1: historyLines.current[i].x1 - 50, y1: historyLines.current[i].y1 - 50, x2: historyLines.current[i].x2 + 50, y2: historyLines.current[i].y2 - 50, }) } } } } else { const tmp = newOuterlines[newOuterlines.length - 1] newOuterlines.push({ x1: tmp.x2, y1: tmp.y2, x2: newOuterlines[0].x1, y2: newOuterlines[0].y1, }) } } makePolygon(newOuterlines) } /** *벽 지붕 외곽선 생성 */ const handleOuterlinesTest = (offset = 71) => { var offsetPoints = [] const sortedIndex = getStartIndex(historyLines.current) let tmpArraySorted = rearrangeArray(historyLines.current, sortedIndex) if (tmpArraySorted[0].direction === 'right') { //시계방향 tmpArraySorted = tmpArraySorted.reverse() //그럼 배열을 거꾸로 만들어서 무조건 반시계방향으로 배열 보정 } setSortedArray(tmpArraySorted) //recoil에 넣음 const points = tmpArraySorted.map((line) => ({ x: line.x1, y: line.y1, })) for (var i = 0; i < points.length; i++) { var prev = points[(i - 1 + points.length) % points.length] var current = points[i] var next = points[(i + 1) % points.length] // 두 벡터 계산 (prev -> current, current -> next) var vector1 = { x: current.x - prev.x, y: current.y - prev.y } var vector2 = { x: next.x - current.x, y: next.y - current.y } // 벡터의 길이 계산 var length1 = Math.sqrt(vector1.x * vector1.x + vector1.y * vector1.y) var length2 = Math.sqrt(vector2.x * vector2.x + vector2.y * vector2.y) // 벡터를 단위 벡터로 정규화 var unitVector1 = { x: vector1.x / length1, y: vector1.y / length1 } var unitVector2 = { x: vector2.x / length2, y: vector2.y / length2 } // 법선 벡터 계산 (왼쪽 방향) var normal1 = { x: -unitVector1.y, y: unitVector1.x } var normal2 = { x: -unitVector2.y, y: unitVector2.x } // 법선 벡터 평균 계산 var averageNormal = { x: (normal1.x + normal2.x) / 2, y: (normal1.y + normal2.y) / 2, } // 평균 법선 벡터를 단위 벡터로 정규화 var lengthNormal = Math.sqrt(averageNormal.x * averageNormal.x + averageNormal.y * averageNormal.y) var unitNormal = { x: averageNormal.x / lengthNormal, y: averageNormal.y / lengthNormal, } // 오프셋 적용 var offsetPoint = { x1: current.x + unitNormal.x * offset, y1: current.y + unitNormal.y * offset, } offsetPoints.push(offsetPoint) } const roof = makePolygon(offsetPoints) setRoof(roof) return roof } /** *벽 지붕 외곽선 생성 polygon을 입력받아 만들기 */ const handleOuterlinesTest2 = (polygon, offset = 71) => { const offsetPoints = [] const sortedIndex = getStartIndex(polygon.lines) let tmpArraySorted = rearrangeArray(polygon.lines, sortedIndex) if (tmpArraySorted[0].direction === 'right') { //시계방향 tmpArraySorted = tmpArraySorted.reverse() //그럼 배열을 거꾸로 만들어서 무조건 반시계방향으로 배열 보정 } setSortedArray(tmpArraySorted) //recoil에 넣음 const points = tmpArraySorted.map((line) => ({ x: line.x1, y: line.y1, })) for (let i = 0; i < points.length; i++) { const prev = points[(i - 1 + points.length) % points.length] const current = points[i] const next = points[(i + 1) % points.length] // 두 벡터 계산 (prev -> current, current -> next) const vector1 = { x: current.x - prev.x, y: current.y - prev.y } const vector2 = { x: next.x - current.x, y: next.y - current.y } // 벡터의 길이 계산 const length1 = Math.sqrt(vector1.x * vector1.x + vector1.y * vector1.y) const length2 = Math.sqrt(vector2.x * vector2.x + vector2.y * vector2.y) // 벡터를 단위 벡터로 정규화 const unitVector1 = { x: vector1.x / length1, y: vector1.y / length1 } const unitVector2 = { x: vector2.x / length2, y: vector2.y / length2 } // 법선 벡터 계산 (왼쪽 방향) const normal1 = { x: -unitVector1.y, y: unitVector1.x } const normal2 = { x: -unitVector2.y, y: unitVector2.x } // 법선 벡터 평균 계산 const averageNormal = { x: (normal1.x + normal2.x) / 2, y: (normal1.y + normal2.y) / 2, } // 평균 법선 벡터를 단위 벡터로 정규화 const lengthNormal = Math.sqrt(averageNormal.x * averageNormal.x + averageNormal.y * averageNormal.y) const unitNormal = { x: averageNormal.x / lengthNormal, y: averageNormal.y / lengthNormal, } // 오프셋 적용 const offsetPoint = { x1: current.x + unitNormal.x * offset, y1: current.y + unitNormal.y * offset, } offsetPoints.push(offsetPoint) } const roof = makePolygon(offsetPoints) roof.setWall(polygon) setRoof(roof) roof.drawHelpLine() } const togglePolygonLine = (obj) => { const rtnLines = [] if (obj.type === 'QPolygon') { const points = obj.getCurrentPoints() points.forEach((point, index) => { const nextPoint = points[(index + 1) % points.length] // 마지막 점이면 첫 번째 점으로 연결 const line = new QLine([point.x, point.y, nextPoint.x, nextPoint.y], { stroke: 'black', strokeWidth: 2, selectable: false, fontSize: fontSize, // fontSize는 필요에 따라 조정 parent: obj, }) obj.visible = false canvas.add(line) rtnLines.push(line) }) canvas.renderAll() } if (obj.type === 'QLine') { const parent = obj.parent canvas ?.getObjects() .filter((obj) => obj.parent === parent) .forEach((obj) => { rtnLines.push(obj) canvas.remove(obj) }) parent.visible = true canvas.renderAll() } return rtnLines } /** * 템플릿 B 적용 */ const applyTemplateB = () => { changeMode(canvas, Mode.EDIT) const polygon = drawWallPolygon() const params = { eaves: 50, edge: 20, polygon, } handleInnerLineColor(polygon) // handleOuterLineTemplateB(params) console.log(polygon.lines.length) if (polygon.lines.length === 4) { handleTemplateBRect(params) } else if (polygon.length === 6) { handleTemplateB(params) } } /** * 4각형일때 계산 로직 * @param {obj} params */ const handleTemplateBRect = (params) => { const { eaves, edge, polygon } = params const centerLinePoint = {} const centerDashLinePoint = {} const qlineOpt = { stroke: 'blue', strokeWidth: 2, selectable: false, fontSize: fontSize, } const qlineOptDash = { stroke: 'black', strokeWidth: 2, strokeDashArray: [5, 5], selectable: false, fontSize: fontSize, } const qlineOptDashWithoutLength = { ...qlineOptDash, isActiveLengthText: false, } polygon.lines.forEach((line, index) => { let outline if (index === 0) { outline = new QLine([line.x1 - edge, line.y1 - eaves, line.x2 - edge, line.y2 + eaves], qlineOpt) const centeredPoint = getCenterPoint(line.y1, line.y2) centerLinePoint.x1 = line.x1 - edge centerLinePoint.y1 = centeredPoint centerLinePoint.y2 = centeredPoint centerDashLinePoint.y1 = line.y1 - eaves centerDashLinePoint.y2 = line.y2 + eaves } else if (index === 1) { outline = new QLine([line.x1 - edge, line.y1 + eaves, line.x2 + edge, line.y2 + eaves], qlineOpt) const centeredPoint = getCenterPoint(line.x1, line.x2) centerLinePoint.x2 = line.x2 + edge centerDashLinePoint.x1 = centeredPoint centerDashLinePoint.x2 = centeredPoint } else if (index === 2) { outline = new QLine([line.x1 + edge, line.y1 + eaves, line.x2 + edge, line.y2 - eaves], qlineOpt) } else if (index === 3) { outline = new QLine([line.x1 + edge, line.y1 - eaves, line.x2 - edge, line.y2 - eaves], qlineOpt) } canvas.add(outline) }) const centerLine = new QLine([centerLinePoint.x1, centerLinePoint.y1, centerLinePoint.x2, centerLinePoint.y2], qlineOpt) canvas.add(centerLine) const centerDashLine1 = new QLine( [centerDashLinePoint.x1, centerDashLinePoint.y1, centerDashLinePoint.x2, getCenterPoint(centerDashLinePoint.y1, centerDashLinePoint.y2)], qlineOptDashWithoutLength, ) canvas.add(centerDashLine1) const centerDashLine2 = new QLine( [centerDashLinePoint.x1, getCenterPoint(centerDashLinePoint.y1, centerDashLinePoint.y2), centerDashLinePoint.x2, centerDashLinePoint.y2], qlineOptDashWithoutLength, ) canvas.add(centerDashLine2) canvas.renderAll() } /** * 6각형일때 계산 로직 * @param {obj} params */ const handleTemplateB = (params) => { const { eaves, edge, polygon } = params // 가장 긴 라인이 첫번째일때 let shapeType = 0 console.log(polygon) const odd = polygon.lines.filter((line, index) => index % 2 === 0) const even = polygon.lines.filter((line, index) => index % 2 !== 0) const rerangeOdd = chgLineDirectionVertical(odd) const rerangeEven = chgLineDirectionHorizontal(even) // 가장 긴 라인이 첫번째인지 판단 chkLengthIndex({ arr: odd, type: 'L' }) !== 0 ? (shapeType = 1) : null // 가장 짧은 라인의 인덱스 반환 const shortIndex = chkLengthIndex({ arr: odd, type: 'S' }) const centralLinePoint = { x1: 0, y1: 0, x2: 0, y2: 0, } const centralSubLinePoint = { x1: 0, y1: 0, x2: 0, y2: 0, } const centralDashLinePoint = { x1: 0, y1: 0, x2: 0, y2: 0, } const centralSubDashLinePoint = { x1: 0, y1: 0, x2: 0, y2: 0, } const qlineOpt = { stroke: 'blue', strokeWidth: 2, selectable: false, fontSize: fontSize, } const qlineOptDash = { stroke: 'black', strokeWidth: 2, strokeDashArray: [5, 5], selectable: false, fontSize: fontSize, } rerangeOdd.forEach((line, index) => { const centeredPoint = getCenterPoint(line.y1, line.y2) let points1 = [] let points2 = [] if (polygon.shape === 2 || polygon.shape === 3) { if (index === 0) { points1 = [line.x1 - edge, line.y1 - eaves, line.x1 - edge, centeredPoint] points2 = [line.x2 - edge, centeredPoint, line.x2 - edge, line.y2 + eaves] centralLinePoint.x1 = line.x1 - edge centralLinePoint.y1 = centeredPoint centralLinePoint.y2 = centeredPoint centralDashLinePoint.y1 = line.y1 - eaves centralDashLinePoint.y2 = line.y2 + eaves } else if (index === 1) { if (polygon.shape === 2) { points1 = [line.x1 + edge, line.y1 - eaves, line.x1 + edge, centeredPoint] points2 = [line.x1 + edge, centeredPoint, line.x2 + edge, line.y2 + eaves] centralSubLinePoint.x2 = line.x1 + edge centralSubLinePoint.y2 = centeredPoint } else { points1 = [line.x1 + edge, getCenterPoint(rerangeOdd[2].y1, rerangeOdd[2].y2), line.x2 + edge, line.y2 + eaves] points2 = [line.x1, getCenterPoint(rerangeOdd[2].y1, rerangeOdd[2].y2), line.x1, line.y1 + eaves] centralLinePoint.x2 = line.x1 + edge centralSubLinePoint.y1 = getCenterPoint(rerangeOdd[2].y1, rerangeOdd[2].y2) centralSubLinePoint.y2 = getCenterPoint(rerangeOdd[2].y1, rerangeOdd[2].y2) } } else if (index === 2) { if (polygon.shape === 2) { points1 = [line.x1 + edge, line.y1 - eaves, line.x2 + edge, centralSubLinePoint.y2] points2 = [line.x2, line.y2 - eaves, line.x2, centralSubLinePoint.y2] } else { points1 = [line.x1 + edge, line.y1 - eaves, line.x2 + edge, centeredPoint] points2 = [line.x2 + edge, centeredPoint, line.x2 + edge, line.y2 + eaves] } } } else { if (index === 0) { points1 = [line.x1 - edge, line.y1 - eaves, line.x2 - edge, line.y2 + eaves] centralSubLinePoint.x1 = line.x1 - edge centralSubLinePoint.y1 = getCenterPoint(line.y1, line.y2) centralSubLinePoint.y2 = getCenterPoint(line.y1, line.y2) } else if (index === 1) { if (polygon.shape === 1) { points1 = [line.x1 - edge, centralSubLinePoint.y1, line.x2 - edge, line.y2 + eaves] points2 = [line.x1, centralSubLinePoint.y1, line.x1, line.y1 + eaves] centralLinePoint.x1 = line.x1 - edge centralSubLinePoint.x2 = line.x2 } else { points1 = [line.x1 + edge, line.y1 - eaves, line.x2 + edge, centeredPoint] points2 = [line.x2 + edge, centeredPoint, line.x2 + edge, line.y2 + eaves] centralLinePoint.x2 = line.x1 + edge centralLinePoint.y1 = centeredPoint centralLinePoint.y2 = centeredPoint centralDashLinePoint.y1 = line.y1 - eaves centralDashLinePoint.y2 = line.y2 + eaves } } else { if (polygon.shape === 1) { points1 = [line.x1 + edge, line.y1 - eaves, line.x1 + edge, centeredPoint] points2 = [line.x2 + edge, centeredPoint, line.x2 + edge, line.y2 + eaves] centralLinePoint.x2 = line.x1 + edge centralLinePoint.y1 = centeredPoint centralLinePoint.y2 = centeredPoint centralDashLinePoint.y1 = line.y1 - eaves centralDashLinePoint.y2 = line.y2 + eaves } else { points1 = [line.x1 - edge, line.y1 - eaves, line.x2 - edge, centralSubLinePoint.y1] points2 = [line.x2, line.y2 - eaves, line.x2, centralSubLinePoint.y2] centralLinePoint.x1 = line.x1 - edge centralSubLinePoint.x2 = line.x2 } } } if (points1.length > 0) { const subLine1 = new QLine(points1, qlineOpt) canvas.add(subLine1) } if (points2.length > 0) { const subLine2 = new QLine(points2, qlineOpt) canvas.add(subLine2) } }) rerangeEven.forEach((line, index) => { let points = [] if (polygon.shape === 2 || polygon.shape === 3) { if (index === 0) { points = [line.x1 - edge, line.y1 + eaves, line.x2 + edge, line.y2 + eaves] if (polygon.shape === 3) { centralDashLinePoint.x1 = getCenterPoint(line.x1, line.x2) centralDashLinePoint.x2 = getCenterPoint(line.x1, line.x2) } } else if (index === 2) { points = [line.x1 - edge, line.y1 - eaves, line.x2 + edge, line.y2 - eaves] if (polygon.shape === 2) { centralDashLinePoint.x1 = getCenterPoint(line.x1, line.x2) centralDashLinePoint.x2 = getCenterPoint(line.x1, line.x2) centralLinePoint.x2 = line.x2 + edge } } else { if (polygon.shape === 2) { const subLines = [ [line.x1, line.y1 - eaves, line.x2 + edge, line.y2 - eaves], [line.x1, centralSubLinePoint.y2, centralSubLinePoint.x2, centralSubLinePoint.y2], ] subLines.forEach((sLine, index) => { const subLine = new QLine(sLine, qlineOpt) canvas.add(subLine) }) centralSubDashLinePoint.x1 = getCenterPoint(line.x1, line.x2 + edge) centralSubDashLinePoint.x2 = getCenterPoint(line.x1, line.x2 + edge) centralSubDashLinePoint.y1 = line.y1 - eaves centralSubDashLinePoint.y2 = centralSubLinePoint.y2 } else { const subLines = [ [line.x1, line.y1 + eaves, line.x2 + edge, line.y2 + eaves], [line.x1, centralSubLinePoint.y1, line.x2 + edge, centralSubLinePoint.y2], ] subLines.forEach((sLine, index) => { const subLine = new QLine(sLine, qlineOpt) canvas.add(subLine) }) centralSubDashLinePoint.x1 = getCenterPoint(line.x1, line.x2 + edge) centralSubDashLinePoint.x2 = getCenterPoint(line.x1, line.x2 + edge) centralSubDashLinePoint.y1 = centralSubLinePoint.y1 centralSubDashLinePoint.y2 = line.y2 + eaves } } } else { if (index === 0) { if (polygon.shape === 1) { const subLines = [ [centralSubLinePoint.x1, centralSubLinePoint.y1, centralSubLinePoint.x2, centralSubLinePoint.y2], [line.x1 - edge, line.y1 + eaves, line.x2, line.y1 + eaves], ] subLines.forEach((sLine, index) => { const subLine = new QLine(sLine, qlineOpt) canvas.add(subLine) }) centralSubDashLinePoint.x1 = getCenterPoint(line.x1 - edge, line.x2) centralSubDashLinePoint.x2 = getCenterPoint(line.x1 - edge, line.x2) centralSubDashLinePoint.y1 = centralSubLinePoint.y1 centralSubDashLinePoint.y2 = line.y2 + eaves } else { points = [line.x1 - edge, line.y1 + eaves, line.x2 + edge, line.y2 + eaves] } } else if (index === 1) { if (polygon.shape === 1) { points = [line.x1 - edge, line.y1 + eaves, line.x2 + edge, line.y2 + eaves] centralDashLinePoint.x1 = getCenterPoint(line.x1, line.x2) centralDashLinePoint.x2 = getCenterPoint(line.x1, line.x2) } else { points = [line.x1 - edge, line.y1 - eaves, line.x2 + edge, line.y2 - eaves] centralDashLinePoint.x1 = getCenterPoint(line.x1, line.x2) centralDashLinePoint.x2 = getCenterPoint(line.x1, line.x2) } } else { if (polygon.shape === 1) { points = [line.x1 - edge, line.y1 - eaves, line.x2 + edge, line.y2 - eaves] } else { const subLines = [ [centralSubLinePoint.x1, centralSubLinePoint.y1, centralSubLinePoint.x2, centralSubLinePoint.y2], [line.x1 - edge, line.y1 - eaves, line.x2, line.y1 - eaves], ] subLines.forEach((sLine, index) => { const subLine = new QLine(sLine, qlineOpt) canvas.add(subLine) }) centralSubDashLinePoint.x1 = getCenterPoint(line.x1 - edge, line.x2) centralSubDashLinePoint.x2 = getCenterPoint(line.x1 - edge, line.x2) centralSubDashLinePoint.y1 = line.y1 - eaves centralSubDashLinePoint.y2 = centralSubLinePoint.y2 } } } if (points.length > 0) { const subLine = new QLine(points, qlineOpt) canvas.add(subLine) } }) const centralLine = new QLine([centralLinePoint.x1, centralLinePoint.y1, centralLinePoint.x2, centralLinePoint.y2], qlineOpt) canvas.add(centralLine) const centralDashLine1 = new QLine([centralDashLinePoint.x1, centralDashLinePoint.y1, centralDashLinePoint.x1, centralLinePoint.y2], qlineOptDash) canvas.add(centralDashLine1) const centralDashLine2 = new QLine([centralDashLine1.x2, centralDashLine1.y2, centralDashLinePoint.x2, centralDashLinePoint.y2], qlineOptDash) canvas.add(centralDashLine2) const centralSubDashLine = new QLine( [centralSubDashLinePoint.x1, centralSubDashLinePoint.y1, centralSubDashLinePoint.x2, centralSubDashLinePoint.y2], qlineOptDash, ) canvas.add(centralSubDashLine) canvas.renderAll() } /** * 세로 방샹 라인의 좌표 순서를 위에서 아래로 변경 * @param {array} arr * @returns */ const chgLineDirectionVertical = (arr) => { const newArr = arr.map((line, index) => { if (line.direction !== 'bottom') { const newPoint = { x1: line.x2, y1: line.y2, x2: line.x1, y2: line.y1 } return newPoint } else { return line } }) return newArr } /** * 가로 방향 라인의 좌표 순서를 왼쪽에서 오른쪽으로 변경 * @param {array} arr * @returns */ const chgLineDirectionHorizontal = (arr) => { const newArr = arr.map((line, index) => { if (line.direction !== 'right') { const newPoint = { x1: line.x2, y1: line.y2, x2: line.x1, y2: line.y1 } return newPoint } else { return line } }) return newArr } /** * 라인 배열 중 가장 긴 라인의 인덱스를 반환합니다. */ const chkLengthIndex = (params) => { const { arr, type } = params let maxIndex = 0 for (let i = 1; i < arr.length; i++) { if (type === 'L') { if (arr[maxIndex].length < arr[i].length) { maxIndex = i } } else { if (arr[maxIndex].length > arr[i].length) { maxIndex = i } } } return maxIndex } /** * 외벽선 색깔을 변경 * @param {array} polygon */ const handleInnerLineColor = (polygon) => { polygon.lines.forEach((line, index) => { if (index % 2 === 0) { line.set('stroke', 'rgb(0, 220, 221)') } else { line.set('stroke', 'rgb(0, 224, 61)') } const overLine = new QLine([line.x1, line.y1, line.x2, line.y2], { stroke: line.stroke, strokeWidth: 2, selectable: false, fontSize: fontSize, isActiveLengthText: false, }) canvas.add(overLine) }) canvas.renderAll() } const handleOuterLineTemplateB = (params) => { const { eaves, edge, polygon } = params polygon.points.forEach((point, index) => { let x2 = index === polygon.points.length - 1 ? polygon.points[0].x : polygon.points[index + 1].x let y2 = index === polygon.points.length - 1 ? polygon.points[0].y : polygon.points[index + 1].y let x1 = point.x let y1 = point.y if (index % 2 === 0) { if (polygon.lines[index].direction === 'bottom') { y1 = y1 - eaves y2 = y2 + eaves x1 = x1 - edge x2 = x2 - edge } else { y1 = y1 + eaves y2 = y2 - eaves x1 = x1 + edge x2 = x2 + edge } } else { if (polygon.lines[index].direction === 'right') { x1 = x1 - edge x2 = x2 + edge y1 = y1 + eaves y2 = y2 + eaves } else { x1 = x1 + edge x2 = x2 - edge y1 = y1 - eaves y2 = y2 - eaves } } switch (polygon.shape) { case 1: if (index === 0) { const subLine = new QLine( [ point.x - edge, polygon.points[0].y + (polygon.points[1].y - polygon.points[0].y) / 2, polygon.points[2].x, polygon.points[0].y + (polygon.points[1].y - polygon.points[0].y) / 2, ], { stroke: 'blue', strokeWidth: 2, selectable: false, fontSize: fontSize, }, ) canvas.add(subLine) } if (index === 1) { x2 = x2 - edge const subLine = new QLine([x2, y2, x2, polygon.points[0].y + (polygon.points[1].y - polygon.points[0].y) / 2], { stroke: 'blue', strokeWidth: 2, selectable: false, fontSize: fontSize, }) canvas.add(subLine) } if (index === 2) { y1 = point.y - (polygon.points[1].y - polygon.points[0].y) / 2 const centeredPoint = getCenterPoint(polygon.points[1].x, point.x) const subLine = new QLine( [centeredPoint, point.y + eaves, centeredPoint, polygon.points[0].y + (polygon.points[1].y - polygon.points[0].y) / 2], { stroke: 'black', strokeWidth: 2, strokeDashArray: [5, 5], selectable: false, fontSize: fontSize, }, ) canvas.add(subLine) } if (index === 3) { const centeredPoint = getCenterPoint(point.x, polygon.points[4].x) const subLine = new QLine([centeredPoint, point.y + eaves, centeredPoint, polygon.points[5].y - eaves], { stroke: 'black', strokeWidth: 2, strokeDashArray: [5, 5], selectable: false, fontSize: fontSize, }) canvas.add(subLine) } if (index === 5) { const centeredPoint = getCenterPoint(point.y, polygon.points[4].y) const subLine = new QLine([point.x + edge, centeredPoint, polygon.points[2].x - edge, centeredPoint], { stroke: 'blue', strokeWidth: 2, selectable: false, fontSize: fontSize, }) canvas.add(subLine) } break case 2: const centerPoint = polygon.points[3].y + (polygon.points[2].y - polygon.points[3].y) / 2 if (index === 0) { const subLine = new QLine( [ point.x - egde, polygon.points[0].y + (polygon.points[1].y - polygon.points[0].y) / 2, polygon.points[5].x + egde, polygon.points[0].y + (polygon.points[1].y - polygon.points[0].y) / 2, ], { stroke: 'blue', strokeWidth: 2, selectable: false, fontSize: fontSize, }, ) canvas.add(subLine) } if (index === 3) { x2 = x2 + egde const subLine = new QLine([x2, y2, x2, centerPoint], { stroke: 'blue', strokeWidth: 2, selectable: false, fontSize: fontSize, }) canvas.add(subLine) } if (index === 4) { y1 = point.y + (polygon.points[index - 2].y - polygon.points[index - 1].y) / 2 const subLine = new QLine([point.x, centerPoint, polygon.points[2].x + 20, centerPoint], { stroke: 'blue', strokeWidth: 2, selectable: false, fontSize: fontSize, }) const subVerticalLine = new QLine( [ getCenterPoint(point.x, polygon.points[2].x + egde), polygon.points[3].y - eaves, getCenterPoint(point.x, polygon.points[2].x + egde), centerPoint, ], { stroke: 'black', strokeWidth: 2, strokeDashArray: [5, 5], selectable: false, fontSize: fontSize, }, ) canvas.add(subVerticalLine) } if (index === 5) { const centeredPoint = getCenterPoint(polygon.points[0].x, polygon.points[5].x) const verticalSubLine = new QLine([centeredPoint, polygon.points[0].y - eaves, centeredPoint, polygon.points[1].y + eaves], { stroke: 'black', strokeWidth: 2, strokeDashArray: [5, 5], selectable: false, fontSize: fontSize, }) canvas.add(verticalSubLine) } break case 3: break case 4: break default: break } const line = new QLine([x1, y1, x2, y2], { stroke: 'blue', strokeWidth: 2, selectable: false, fontSize: fontSize, }) canvas.add(line) }) canvas.renderAll() } return { mode, changeMode, setCanvas, handleClear, zoomIn, zoomOut, zoom, togglePolygonLine, handleOuterlinesTest2, } }