polygon Qline추가 및 currentPoints 수정
This commit is contained in:
parent
0c9ba38209
commit
7218f7f9ea
@ -45,6 +45,7 @@ export default function Roof2() {
|
|||||||
zoomIn,
|
zoomIn,
|
||||||
zoomOut,
|
zoomOut,
|
||||||
zoom,
|
zoom,
|
||||||
|
togglePolygonLine,
|
||||||
} = useMode()
|
} = useMode()
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@ -147,16 +148,18 @@ export default function Roof2() {
|
|||||||
strokeWidth: 1,
|
strokeWidth: 1,
|
||||||
selectable: true,
|
selectable: true,
|
||||||
fontSize: fontSize,
|
fontSize: fontSize,
|
||||||
|
name: 'QPolygon1',
|
||||||
},
|
},
|
||||||
canvas, // 필수로 넣어줘야 함
|
canvas, // 필수로 넣어줘야 함
|
||||||
)
|
)
|
||||||
|
|
||||||
canvas?.add(polygon)
|
canvas?.add(polygon)
|
||||||
|
|
||||||
polygon.drawOuterLine(-50)
|
polygon.drawRoof(-50)
|
||||||
addBackgroundInPolygon(polygon)
|
addBackgroundInPolygon(polygon)
|
||||||
|
|
||||||
console.log(polygon.outerPolygon)
|
const lines = togglePolygonLine(polygon)
|
||||||
|
togglePolygonLine(lines[0])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -192,11 +195,19 @@ export default function Roof2() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
polygon.fillBackground(pattern)
|
polygon.fillBackground(pattern)
|
||||||
|
|
||||||
console.log(polygon)
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function PolygonToLine() {
|
||||||
|
const polygon = canvas?.getActiveObject()
|
||||||
|
|
||||||
|
if (polygon.type !== 'QPolygon') {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const lines = togglePolygonLine(polygon)
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
{canvas && (
|
{canvas && (
|
||||||
@ -297,6 +308,9 @@ export default function Roof2() {
|
|||||||
<Button className="m-1 p-2" onClick={makeQLine}>
|
<Button className="m-1 p-2" onClick={makeQLine}>
|
||||||
QLine
|
QLine
|
||||||
</Button>
|
</Button>
|
||||||
|
<Button className="m-1 p-2" onClick={PolygonToLine}>
|
||||||
|
PolygonToLine
|
||||||
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
<div className="flex justify-center flex-col items-center">
|
<div className="flex justify-center flex-col items-center">
|
||||||
<div className="m-2 p-2 w-80">
|
<div className="m-2 p-2 w-80">
|
||||||
|
|||||||
@ -11,6 +11,7 @@ export class QLine extends fabric.Group {
|
|||||||
y2
|
y2
|
||||||
direction
|
direction
|
||||||
type = 'QLine'
|
type = 'QLine'
|
||||||
|
parent
|
||||||
|
|
||||||
constructor(points, option) {
|
constructor(points, option) {
|
||||||
const [x1, y1, x2, y2] = points
|
const [x1, y1, x2, y2] = points
|
||||||
@ -28,6 +29,8 @@ export class QLine extends fabric.Group {
|
|||||||
this.line = line
|
this.line = line
|
||||||
this.fontSize = option.fontSize
|
this.fontSize = option.fontSize
|
||||||
this.direction = option.direction
|
this.direction = option.direction
|
||||||
|
this.parent = option.parent
|
||||||
|
|
||||||
this.#init()
|
this.#init()
|
||||||
this.#addControl()
|
this.#addControl()
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,15 +1,22 @@
|
|||||||
import { fabric } from 'fabric'
|
import { fabric } from 'fabric'
|
||||||
import { distanceBetweenPoints } from '@/util/canvas-util'
|
import {
|
||||||
|
distanceBetweenPoints,
|
||||||
|
getDirection,
|
||||||
|
getDirectionByPoint,
|
||||||
|
} from '@/util/canvas-util'
|
||||||
|
import { QLine } from '@/components/fabric/QLine'
|
||||||
|
|
||||||
export default class QPolygon extends fabric.Group {
|
export default class QPolygon extends fabric.Group {
|
||||||
type = 'QPolygon'
|
type = 'QPolygon'
|
||||||
polygon
|
polygon
|
||||||
points
|
points
|
||||||
texts = []
|
texts = []
|
||||||
|
lines = []
|
||||||
canvas
|
canvas
|
||||||
fontSize
|
fontSize
|
||||||
qCells = []
|
qCells = []
|
||||||
outerPolygon
|
outerPolygon
|
||||||
|
name
|
||||||
constructor(points, options, canvas) {
|
constructor(points, options, canvas) {
|
||||||
if (!options.fontSize) {
|
if (!options.fontSize) {
|
||||||
throw new Error('Font size is required.')
|
throw new Error('Font size is required.')
|
||||||
@ -19,15 +26,31 @@ export default class QPolygon extends fabric.Group {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const polygon = new fabric.Polygon(points, options)
|
const polygon = new fabric.Polygon(points, options)
|
||||||
|
|
||||||
super([polygon], {})
|
super([polygon], {})
|
||||||
|
|
||||||
this.fontSize = options.fontSize
|
this.fontSize = options.fontSize
|
||||||
this.points = points
|
this.points = points
|
||||||
this.polygon = polygon
|
this.polygon = polygon
|
||||||
// this.canvas = canvas
|
this.name = options.name
|
||||||
|
|
||||||
this.#init()
|
this.#init()
|
||||||
this.#addEvent()
|
this.#addEvent()
|
||||||
|
this.#initLines()
|
||||||
|
}
|
||||||
|
|
||||||
|
#initLines() {
|
||||||
|
this.points.forEach((point, i) => {
|
||||||
|
const nextPoint = this.points[(i + 1) % this.points.length]
|
||||||
|
const line = new QLine([point.x, point.y, nextPoint.x, nextPoint.y], {
|
||||||
|
stroke: 'black',
|
||||||
|
strokeWidth: 1,
|
||||||
|
fontSize: this.fontSize,
|
||||||
|
direction: getDirectionByPoint(point, nextPoint),
|
||||||
|
})
|
||||||
|
|
||||||
|
this.lines.push(line)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#init() {
|
#init() {
|
||||||
@ -241,19 +264,28 @@ export default class QPolygon extends fabric.Group {
|
|||||||
const scaleX = this.scaleX
|
const scaleX = this.scaleX
|
||||||
const scaleY = this.scaleY
|
const scaleY = this.scaleY
|
||||||
|
|
||||||
|
const left = this.left
|
||||||
|
const top = this.top
|
||||||
|
|
||||||
|
// 시작점
|
||||||
|
const point = this.points[0]
|
||||||
|
|
||||||
|
const movingX = left - point.x * scaleX
|
||||||
|
const movingY = top - point.y * scaleY
|
||||||
|
|
||||||
return this.points.map((point) => {
|
return this.points.map((point) => {
|
||||||
return {
|
return {
|
||||||
x: point.x * scaleX,
|
x: point.x * scaleX + movingX,
|
||||||
y: point.y * scaleY,
|
y: point.y * scaleY + movingY,
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 외곽선 그리기
|
* 지붕 그리기
|
||||||
* @param offset
|
* @param offset
|
||||||
*/
|
*/
|
||||||
drawOuterLine(offset = -10) {
|
drawRoof(offset = -10) {
|
||||||
const points = this.getCurrentPoints()
|
const points = this.getCurrentPoints()
|
||||||
const offsetPoints = []
|
const offsetPoints = []
|
||||||
for (let i = 0; i < points.length; i++) {
|
for (let i = 0; i < points.length; i++) {
|
||||||
|
|||||||
@ -5,6 +5,7 @@ import {
|
|||||||
getStartIndex,
|
getStartIndex,
|
||||||
rearrangeArray,
|
rearrangeArray,
|
||||||
findTopTwoIndexesByDistance,
|
findTopTwoIndexesByDistance,
|
||||||
|
getDirection,
|
||||||
} from '@/util/canvas-util'
|
} from '@/util/canvas-util'
|
||||||
import { useRecoilState } from 'recoil'
|
import { useRecoilState } from 'recoil'
|
||||||
import { fontSizeState, sortedPolygonArray } from '@/store/canvasAtom'
|
import { fontSizeState, sortedPolygonArray } from '@/store/canvasAtom'
|
||||||
@ -313,33 +314,11 @@ export function useMode() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 두 점 사이의 방향을 반환합니다.
|
|
||||||
*/
|
|
||||||
const getDirection = (a, b) => {
|
|
||||||
const vector = {
|
|
||||||
x: b.left - a.left,
|
|
||||||
y: b.top - a.top,
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Math.abs(vector.x) > Math.abs(vector.y)) {
|
|
||||||
// x축 방향으로 더 많이 이동
|
|
||||||
return vector.x > 0 ? 'right' : 'left'
|
|
||||||
} else {
|
|
||||||
// y축 방향으로 더 많이 이동
|
|
||||||
return vector.y > 0 ? 'bottom' : 'top'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 두 점을 연결하는 선과 길이를 그립니다.
|
* 두 점을 연결하는 선과 길이를 그립니다.
|
||||||
* a : 시작점, b : 끝점
|
* a : 시작점, b : 끝점
|
||||||
*/
|
*/
|
||||||
const drawLineWithLength = (a, b) => {
|
const drawLineWithLength = (a, b) => {
|
||||||
const vector = {
|
|
||||||
x: b.left - a.left,
|
|
||||||
y: b.top - a.top,
|
|
||||||
}
|
|
||||||
const line = new QLine([a.left, a.top, b.left, b.top], {
|
const line = new QLine([a.left, a.top, b.left, b.top], {
|
||||||
stroke: 'black',
|
stroke: 'black',
|
||||||
strokeWidth: 2,
|
strokeWidth: 2,
|
||||||
@ -800,7 +779,7 @@ export function useMode() {
|
|||||||
|
|
||||||
offsetPoints.push(offsetPoint)
|
offsetPoints.push(offsetPoint)
|
||||||
}
|
}
|
||||||
|
|
||||||
makePolygon(offsetPoints)
|
makePolygon(offsetPoints)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -225,3 +225,42 @@ export const getRoofHypotenuse = (base) => {
|
|||||||
export const getDegreeByChon = (chon) => {
|
export const getDegreeByChon = (chon) => {
|
||||||
return chon * 5.45
|
return chon * 5.45
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 두 점 사이의 방향을 반환합니다.
|
||||||
|
* @param a {fabric.Object}
|
||||||
|
* @param b {fabric.Object}
|
||||||
|
* @returns {string}
|
||||||
|
*/
|
||||||
|
export const getDirection = (a, b) => {
|
||||||
|
const vector = {
|
||||||
|
x: b.left - a.left,
|
||||||
|
y: b.top - a.top,
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Math.abs(vector.x) > Math.abs(vector.y)) {
|
||||||
|
// x축 방향으로 더 많이 이동
|
||||||
|
return vector.x > 0 ? 'right' : 'left'
|
||||||
|
} else {
|
||||||
|
// y축 방향으로 더 많이 이동
|
||||||
|
return vector.y > 0 ? 'bottom' : 'top'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 두 점 사이의 방향을 반환합니다.
|
||||||
|
*/
|
||||||
|
export const getDirectionByPoint = (a, b) => {
|
||||||
|
const vector = {
|
||||||
|
x: b.x - a.x,
|
||||||
|
y: b.y - a.y,
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Math.abs(vector.x) > Math.abs(vector.y)) {
|
||||||
|
// x축 방향으로 더 많이 이동
|
||||||
|
return vector.x > 0 ? 'right' : 'left'
|
||||||
|
} else {
|
||||||
|
// y축 방향으로 더 많이 이동
|
||||||
|
return vector.y > 0 ? 'bottom' : 'top'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user