diff --git a/src/components/Roof2.jsx b/src/components/Roof2.jsx index 9c06cff2..55b9c219 100644 --- a/src/components/Roof2.jsx +++ b/src/components/Roof2.jsx @@ -39,6 +39,8 @@ export default function Roof2() { }) canvas?.add(rect) + + setTimeout(() => rect.delete(), 500) } } @@ -52,6 +54,8 @@ export default function Roof2() { }) canvas?.add(line) + + setTimeout(() => line.delete(), 500) } } @@ -75,9 +79,7 @@ export default function Roof2() { canvas?.add(polygon) - setTimeout(() => { - console.log(canvas?.getObjects()) - }, 1000) + setTimeout(() => polygon.delete(), 500) } } diff --git a/src/components/fabric/QLine.js b/src/components/fabric/QLine.js index 4fa81751..b7e9777c 100644 --- a/src/components/fabric/QLine.js +++ b/src/components/fabric/QLine.js @@ -1,16 +1,38 @@ +import { fabric } from 'fabric' export default class QLine extends fabric.Line { length + group + constructor(points, option) { super(points, option) this.on('added', () => { if (this.isLengthText) { - this.addLengthText() + this.#addLengthText() + } else { + this.#makeGroupItem([this]) } }) } - addLengthText() { + delete() { + this.group.canvas.remove(this.group) + } + + #makeGroupItem(groupItems) { + const group = new fabric.Group(groupItems, { + selectable: false, + type: 'QRect', + canvas: this.canvas, + }) + + this.group = group + this.canvas.add(group) + this.canvas.renderAll() + this.canvas.remove(this) + } + + #addLengthText() { const dx = this.x2 - this.x1 const dy = this.y2 - this.y1 const length = Math.sqrt(dx * dx + dy * dy) @@ -24,13 +46,6 @@ export default class QLine extends fabric.Line { selectable: false, }) - const group = new fabric.Group([this, text], { - selectable: false, - type: 'QLine', - }) - - this.canvas.add(group) - this.canvas.renderAll() - this.canvas.remove(this) + this.#makeGroupItem([this, text]) } } diff --git a/src/components/fabric/QPolygon.js b/src/components/fabric/QPolygon.js index c747e04b..efbb2491 100644 --- a/src/components/fabric/QPolygon.js +++ b/src/components/fabric/QPolygon.js @@ -1,15 +1,37 @@ +import { fabric } from 'fabric' export default class QPolygon extends fabric.Polygon { + group + constructor(points, option) { super(points, option) this.on('added', () => { if (this.isLengthText) { - this.addLengthText() + this.#addLengthText() + } else { + this.#makeGroupItem([this]) } }) } - addLengthText() { + #makeGroupItem(groupItems) { + const group = new fabric.Group(groupItems, { + selectable: false, + type: 'QRect', + canvas: this.canvas, + }) + + this.group = group + this.canvas.add(group) + this.canvas.renderAll() + this.canvas.remove(this) + } + + delete() { + this.group.canvas.remove(this.group) + } + + #addLengthText() { const groupItems = [this] for (let i = 0; i < this.points.length; i++) { @@ -30,13 +52,6 @@ export default class QPolygon extends fabric.Polygon { groupItems.push(text) } - const group = new fabric.Group(groupItems, { - selectable: false, - type: 'QPolygon', - }) - - this.canvas.add(group) - this.canvas.renderAll() - this.canvas.remove(this) + this.#makeGroupItem(groupItems) } } diff --git a/src/components/fabric/QRect.js b/src/components/fabric/QRect.js index ceae98f5..ffb21d57 100644 --- a/src/components/fabric/QRect.js +++ b/src/components/fabric/QRect.js @@ -1,15 +1,36 @@ +import { fabric } from 'fabric' export default class QRect extends fabric.Rect { + group constructor(options) { super(options) this.on('added', () => { if (this.isLengthText) { - this.addLengthText() + this.#addLengthText() + } else { + this.#makeGroupItem([this]) } }) } - addLengthText() { + delete() { + this.group.canvas.remove(this.group) + } + + #makeGroupItem(groupItems) { + const group = new fabric.Group(groupItems, { + selectable: false, + type: 'QRect', + canvas: this.canvas, + }) + + this.group = group + this.canvas.add(group) + this.canvas.renderAll() + this.canvas.remove(this) + } + + #addLengthText() { const lines = [ { start: { x: this.left, y: this.top }, @@ -46,13 +67,6 @@ export default class QRect extends fabric.Rect { groupItems.push(text) }) - const group = new fabric.Group(groupItems, { - selectable: false, - type: 'QRect', - }) - - this.canvas.add(group) - this.canvas.renderAll() - this.canvas.remove(this) + this.#makeGroupItem(groupItems) } } diff --git a/src/hooks/useCanvas.js b/src/hooks/useCanvas.js index 4864cfbd..1fcca833 100644 --- a/src/hooks/useCanvas.js +++ b/src/hooks/useCanvas.js @@ -116,7 +116,7 @@ export function useCanvas(id) { const onChange = (e) => { const target = e.target if (target) { - settleDown(target) + // settleDown(target) } if (!isLocked) { diff --git a/src/hooks/useMode.js b/src/hooks/useMode.js index b8421315..ec9f142b 100644 --- a/src/hooks/useMode.js +++ b/src/hooks/useMode.js @@ -241,7 +241,7 @@ export function useMode() { const pointer = canvas.getPointer(o.e) origX = pointer.x origY = pointer.y - rect = new QRect({ + rect = new fabric.Rect({ left: origX, top: origY, originX: 'left', @@ -250,7 +250,6 @@ export function useMode() { height: pointer.y - origY, angle: 0, fill: 'transparent', - isLengthText: true, stroke: 'black', transparentCorners: false, }) @@ -269,10 +268,25 @@ export function useMode() { rect.set({ width: Math.abs(origX - pointer.x) }) rect.set({ height: Math.abs(origY - pointer.y) }) - canvas.renderAll() }) 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, + isLengthText: true, + fill: 'transparent', + stroke: 'black', + transparentCorners: false, + }) + canvas.remove(rect) + canvas.add(qRect) isDown = false }) } @@ -308,6 +322,7 @@ export function useMode() { stroke: 'black', strokeWidth: 2, selectable: false, + isLengthText: true, direction: getDirection(a, b), }) historyLines.current.push(line) @@ -325,7 +340,9 @@ export function useMode() { const points = lines.map((line) => ({ x: line.x1, y: line.y1 })) // 모든 라인 객체를 캔버스에서 제거합니다. - lines.forEach((line) => canvas.remove(line)) + lines.forEach((line) => { + line.delete() + }) // 점 배열을 사용하여 새로운 다각형 객체를 생성합니다. const polygon = new QPolygon(points, { @@ -337,9 +354,6 @@ export function useMode() { // 새로운 다각형 객체를 캔버스에 추가합니다. canvas.add(polygon) - - // 캔버스를 다시 그립니다. - canvas.renderAll() } /**