polygon 백그라운드 이미지 추가

This commit is contained in:
hyojun.choi 2024-07-08 16:44:08 +09:00
parent 17d82d63db
commit a16e4cccba
4 changed files with 28 additions and 1 deletions

BIN
public/assets/img/check.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.0 KiB

View File

@ -154,6 +154,7 @@ export default function Roof2() {
canvas?.add(polygon)
polygon.drawOuterLine(-50)
addBackgroundInPolygon(polygon)
console.log(polygon.outerPolygon)
}
@ -182,6 +183,20 @@ export default function Roof2() {
}
}
const addBackgroundInPolygon = (polygon) => {
fabric.Image.fromURL('assets/img/check.jpg', function (img) {
// .
const pattern = new fabric.Pattern({
source: img.getElement(),
repeat: 'repeat',
})
polygon.fillBackground(pattern)
console.log(polygon)
})
}
return (
<>
{canvas && (

View File

@ -24,7 +24,7 @@ export default class QPolygon extends fabric.Group {
this.fontSize = options.fontSize
this.points = points
this.polygon = polygon
this.canvas = canvas
// this.canvas = canvas
this.#init()
this.#addEvent()
@ -317,4 +317,9 @@ export default class QPolygon extends fabric.Group {
this.addWithUpdate(outPolygon)
this.canvas.renderAll()
}
fillBackground(pattern) {
this.polygon.set({ fill: pattern })
this.canvas.renderAll()
}
}

View File

@ -8,6 +8,9 @@ import {
import { useRecoilState } from 'recoil'
import { canvasSizeState, fontSizeState } from '@/store/canvasAtom'
import QPolygon from '@/components/fabric/QPolygon'
import { QLine } from '@/components/fabric/QLine'
import QRect from '@/components/fabric/QRect'
export function useCanvas(id) {
const [canvas, setCanvas] = useState()
@ -35,6 +38,10 @@ export function useCanvas(id) {
fabric.Object.prototype.cornerStrokeColor = '#2BEBC8'
fabric.Object.prototype.cornerSize = 6
QPolygon.prototype.canvas = c
QLine.prototype.canvas = c
QRect.prototype.canvas = c
setCanvas(c)
return () => {
c.dispose()