group 숫자 적용 작업중

This commit is contained in:
hyojun.choi 2024-06-20 16:47:22 +09:00
parent 67f519d6fa
commit 86522f67e1
2 changed files with 94 additions and 31 deletions

View File

@ -2,6 +2,7 @@ import { createGroupWithLineAndText, getDistance } from '@/app/util/canvas-util'
import { useCanvas } from '@/hooks/useCanvas' import { useCanvas } from '@/hooks/useCanvas'
import { fabric } from 'fabric' import { fabric } from 'fabric'
import { v4 as uuidv4 } from 'uuid' import { v4 as uuidv4 } from 'uuid'
import { useEffect } from 'react'
export default function Roof() { export default function Roof() {
const { const {
@ -21,6 +22,19 @@ export default function Roof() {
updateTextOnLineChange, updateTextOnLineChange,
} = useCanvas('canvas') } = useCanvas('canvas')
useEffect(() => {
// IText
const text = new fabric.IText('Hello', {
left: 100,
top: 100,
fill: 'red',
})
text.on('editing:entered', () => {
console.log('editing:entered')
})
canvas?.add(text)
}, [canvas])
const addRect = () => { const addRect = () => {
const rect = new fabric.Rect({ const rect = new fabric.Rect({
height: 200, height: 200,
@ -92,9 +106,9 @@ export default function Roof() {
const trapezoid = new fabric.Polygon( const trapezoid = new fabric.Polygon(
[ [
{ x: 100, y: 100 }, // { x: 100, y: 100 }, //
{ x: 300, y: 100 }, // { x: 500, y: 100 }, //
{ x: 250, y: 200 }, // { x: 750, y: 400 }, //
{ x: 150, y: 200 }, // { x: 250, y: 400 }, //
], ],
{ {
name: uuidv4(), name: uuidv4(),
@ -102,35 +116,89 @@ export default function Roof() {
opacity: 0.4, opacity: 0.4,
strokeWidth: 3, strokeWidth: 3,
selectable: true, selectable: true,
objectCaching: false,
}, },
) )
attachCustomControlOnPolygon(trapezoid) // attachCustomControlOnPolygon(trapezoid)
addShape(trapezoid)
const group = addDistanceTextToPolygon(trapezoid)
addGroupClickEvent(group)
group.getObjects().forEach(function (object, index) {
if (object.type === 'i-text') {
addTextModifiedEvent(object, trapezoid, index)
}
})
canvas?.add(group)
canvas?.renderAll()
} }
const addTextWithLine = () => { // group group object
const { x1, y1, x2, y2 } = { x1: 20, y1: 100, x2: 220, y2: 100 } function addGroupClickEvent(group) {
/** group.on('mousedown', function () {
* 시작X,시작Y,도착X,도착Y 좌표 const objects = group.getObjects()
*/ canvas?.remove(group)
const horizontalLine = new fabric.Line([x1, y1, x2, y2], { objects.forEach(function (object) {
name: uuidv4(), canvas?.add(object)
stroke: 'red', })
strokeWidth: 3, canvas?.renderAll()
})
}
// polygon IText
function addDistanceTextToPolygon(polygon) {
const points = polygon.get('points')
const texts = []
for (let i = 0; i < points.length; i++) {
const start = points[i]
const end = points[(i + 1) % points.length] // ( )
const distance = Math.sqrt(
Math.pow(end.x - start.x, 2) + Math.pow(end.y - start.y, 2),
) //
const text = new fabric.IText(distance.toFixed(2), {
//
left: (start.x + end.x) / 2, //
top: (start.y + end.y) / 2,
fontSize: 10,
editable: true,
})
texts.push(text)
}
return new fabric.Group([polygon, ...texts], {
// polygon
selectable: true, selectable: true,
}) })
}
const text = new fabric.Text(getDistance(x1, y1, x2, y2).toString(), { // IText polygon
fontSize: 20, function addTextModifiedEvent(text, polygon, index) {
left: (x2 - x1) / 2, text.on('editing:entered', function () {
top: y1 - 20, console.log(123)
const newLength = parseFloat(text.text)
const points = polygon.get('points')
const start = points[index]
const end = points[(index + 1) % points.length]
const vector = { x: end.x - start.x, y: end.y - start.y } // start end
const length = Math.sqrt(vector.x * vector.x + vector.y * vector.y) // ( )
const normalizedVector = { x: vector.x / length, y: vector.y / length } // ( 1)
const scaledVector = {
x: normalizedVector.x * newLength,
y: normalizedVector.y * newLength,
} //
// end
end.x = start.x + scaledVector.x
end.y = start.y + scaledVector.y
// polygon
const newGroup = addDistanceTextToPolygon(polygon)
addGroupClickEvent(newGroup)
canvas.add(newGroup)
canvas.renderAll()
}) })
const group = createGroupWithLineAndText(horizontalLine, text)
addShape(group)
// .
group.on('modified', () => updateTextOnLineChange(group, text))
} }
const randomColor = () => { const randomColor = () => {
@ -232,12 +300,6 @@ export default function Roof() {
> >
도형반전 도형반전
</button> </button>
<button
className="w-30 mx-2 p-2 rounded bg-black text-white"
onClick={addTextWithLine}
>
숫자가 있는
</button>
</div> </div>
<div <div

View File

@ -96,7 +96,7 @@ export function useCanvas(id) {
// 작업 후에 event를 추가해준다. // 작업 후에 event를 추가해준다.
addEventOnCanvas() // addEventOnCanvas()
} }
/** /**
@ -440,6 +440,7 @@ export function useCanvas(id) {
const lastControl = poly.points?.length - 1 const lastControl = poly.points?.length - 1
poly.cornerStyle = 'rect' poly.cornerStyle = 'rect'
poly.cornerColor = 'rgba(0,0,255,0.5)' poly.cornerColor = 'rgba(0,0,255,0.5)'
poly.objectCaching = false
poly.controls = poly.points.reduce(function (acc, point, index) { poly.controls = poly.points.reduce(function (acc, point, index) {
acc['p' + index] = new fabric.Control({ acc['p' + index] = new fabric.Control({
positionHandler: polygonPositionHandler, positionHandler: polygonPositionHandler,