This commit is contained in:
hyojun.choi 2024-07-21 11:20:23 +09:00
parent 3d126dbfc7
commit 0449d512f2
3 changed files with 51 additions and 15 deletions

View File

@ -205,7 +205,7 @@ export default function Roof2() {
const makeQLine = () => { const makeQLine = () => {
if (canvas) { if (canvas) {
const line = new fabric.QLine( const line = new QLine(
[200, 200, 500, 500], [200, 200, 500, 500],
{ {
stroke: 'black', stroke: 'black',
@ -217,7 +217,6 @@ export default function Roof2() {
) )
canvas?.add(line) canvas?.add(line)
console.log(line)
} }
} }

View File

@ -18,6 +18,10 @@ export class QLine extends fabric.Group {
interSectionPoints = [] interSectionPoints = []
lengthTxt = 0 lengthTxt = 0
initPoints
initOption
initLengthTxt
constructor(points, option = { isActiveLengthText: true }, lengthTxt) { constructor(points, option = { isActiveLengthText: true }, lengthTxt) {
// 소수점 전부 제거 // 소수점 전부 제거
points.forEach((point) => { points.forEach((point) => {
@ -33,6 +37,10 @@ export class QLine extends fabric.Group {
const line = new fabric.Line(points, { ...option, strokeWidth: 1 }) const line = new fabric.Line(points, { ...option, strokeWidth: 1 })
super([line], {}) super([line], {})
this.initPoints = points
this.initOption = option
this.initLengthTxt = lengthTxt
this.x1 = x1 this.x1 = x1
this.y1 = y1 this.y1 = y1
this.x2 = x2 this.x2 = x2
@ -117,15 +125,13 @@ export class QLine extends fabric.Group {
} }
fromObject(object, callback) { fromObject(object, callback) {
fabric.util.enlivenObjects(object.objects, function (enlivenedObjects) { console.log('fromObject', object, callback)
delete object.objects
callback && callback(new fabric.Frindle(enlivenedObjects, object))
})
} }
async = true async = true
toObject(propertiesToInclude) { toObject(propertiesToInclude) {
console.log('toObject')
return fabric.util.object.extend(this.callSuper('toObject'), { return fabric.util.object.extend(this.callSuper('toObject'), {
length: this.length, length: this.length,
line: this.line, line: this.line,
@ -146,6 +152,13 @@ export class QLine extends fabric.Group {
addLengthText: this.addLengthText, addLengthText: this.addLengthText,
init: this.init, init: this.init,
addControl: this.addControl, addControl: this.addControl,
initPoints: this.initPoints,
initOption: this.initOption,
initLengthTxt: this.initLengthTxt,
}) })
} }
_set(key, value) {
this.callSuper('_set', key, value)
}
} }

View File

@ -117,7 +117,15 @@ export function useCanvas(id) {
QLine.prototype.canvas = canvas QLine.prototype.canvas = canvas
QRect.prototype.canvas = canvas QRect.prototype.canvas = canvas
fabric.QLine = QLine fabric.QLine = fabric.util.createClass(fabric.Group, {})
// fromObject 메서드를 QLine 클래스에 직접 추가
fabric.QLine.fromObject = function (object, callback) {
const { initOption, initPoints, initLengthTxt } = object
fabric.util.enlivenObjects(object.objects, function (enlivenedObjects) {
return callback(new QLine(initPoints, initOption, initLengthTxt))
})
}
} }
/** /**
@ -539,19 +547,35 @@ export function useCanvas(id) {
} }
const addCanvas = () => { const addCanvas = () => {
const canvasState = canvas?.getObjects() // const canvasState = canvas
canvasState.forEach((state) => { const objs = canvas
console.log(state)
})
const str = JSON.stringify(canvasState) console.log(objs)
const str = JSON.stringify(objs)
canvas?.clear() canvas?.clear()
JSON.parse(str).forEach((state) => { console.log(str)
canvas?.add(state) console.log(JSON.parse(str))
})
// 역직렬화하여 캔버스에 객체를 다시 추가합니다.
canvas?.loadFromJSON(
JSON.parse(str),
function () {
// 모든 객체가 로드되고 캔버스에 추가된 후 호출됩니다.
canvas?.renderAll() // 캔버스를 다시 그립니다.
console.log('Objects are reloaded and rendered on canvas.')
},
function (o, object) {
// 각 객체가 로드될 때마다 호출됩니다.
console.log('Object loaded: ', o, object)
},
)
/*canvas?.loadFromJSON(JSON.parse(str), () => {
console.log('load done')
})*/
/*const stateArr = canvasState.map((state) => state.getObject().getObjects()) /*const stateArr = canvasState.map((state) => state.getObject().getObjects())