Skip to content

FabricJs

Youngho Kim edited this page Nov 26, 2022 · 4 revisions

Init

// npm install fabric @types/fabirc --save

import { fabric } from "fabric";

function FabricCanvas() {
	useEffect(() => {
    canvas.current = initCanvas();

    // destroy fabric on unmount
    return () => {
      canvas.current.dispose();
      canvas.current = null;
    };
  }, []);

  const initCanvas = () =>
    new fabric.Canvas("canvas", {
      height: 800,
      width: 800,
      backgroundColor: "pink",
    });
	return (<>
		<canvas id="canvas" />
		</>
	);
}

image

CRUD

CREATE

const rect = new fabric.Rect({
      height: 280,
      width: 200,
      top: 100,
      left: 100,
      fill: "yellow",
    });

canvas.currnet.add(rect)

READ

const rect = new fabric.Rect({
      height: 280,
      width: 200,
      top: 100,
      left: 100,
      fill: "yellow",
    });

rect.get('์†์„ฑ') //ex) height, widht, top, left...

UPDATE

const rect = new fabric.Rect({
      height: 280,
      width: 200,
      top: 100,
      left: 100,
      fill: "yellow",
    });

rect.set('์†์„ฑ', value) 

DELETE

const rect = new fabric.Rect({
      height: 280,
      width: 200,
      top: 100,
      left: 100,
      fill: "yellow",
    });

canvas.currnet.remove(rect)

Event

Canvas

canvas.current.on("eventname", callback);
  • Canvas Event ์ข…๋ฅ˜
    • object:modified at the end of a transform or any change when statefull is true
    • object:rotating while an object is being rotated from the control
    • object:scaling while an object is being scaled by controls
    • object:moving while an object is being dragged
    • object:skewing while an object is being skewed from the controls
    • before:transform before a transform is is started
    • before:selection:cleared
    • selection:cleared
    • selection:updated
    • selection:created
    • path:created after a drawing operation ends and the path is added
    • mouse:down
    • mouse:move
    • mouse:up
    • mouse:down:before on mouse down,event: before the inner fabric logic runs
    • mouse:move:before on mouse move,event: before the inner fabric logic runs
    • mouse:up:before on mouse up,event: before the inner fabric logic runs
    • mouse:over
    • mouse:out
    • mouse:dblclick whenever a native dbl click event fires on the canvas.
    • event:dragover
    • event:dragenter
    • event:dragleave
    • drop:before before drop event. same native event.event: This is added to handle edge cases
    • event:drop
    • after:render at the end of the render process,event: receives the context in the callback
    • before:render at start the render process,event: receives the context in the callback

Object

const rect = new fabric.Rect({
      height: 280,
      width: 200,
      top: 100,
      left: 100,
      fill: "yellow",
    });

rect.on("eventname", callback);
  • Object Event ์ข…๋ฅ˜
    • event:added
    • event:removed
    • event:selected
    • event:deselected
    • event:modified
    • event:modified
    • event:moved
    • event:scaled
    • event:rotated
    • event:skewed
    • event:rotating
    • event:scaling
    • event:moving
    • event:skewing
    • event:mousedown
    • event:mouseup
    • event:mouseover
    • event:mouseout
    • event:mousewheel
    • event:mousedblclick
    • event:dragover
    • event:dragenter
    • event:dragleave
    • event:drop

์ฐธ๊ณ  :

[Working with events ยท fabricjs/fabric.js Wiki](https://github.com/fabricjs/fabric.js/wiki/Working-with-events)

Interactive Object

fabric

๊ทธ ์™ธ์˜ ๊ธฐ๋Šฅ

Customization

object.set({
	borderColor: 'red',
	cornerColor: 'black'
});

image

Free Drawing

new fabric.Canvas("canvas", {
      height: 800,
      width: 800,
      backgroundColor: "pink",
      isDrawingMode: true,
    });

fabric2

๐Ÿ“š ๊ทธ๋ผ์šด๋“œ ๋ฃฐ

โœ๏ธ ์ปจ๋ฒค์…˜

๐Ÿง‘โ€๐Ÿซ ๋ฉ˜ํ† ๋ง

๐Ÿ“ ์• ์ž์ผ ํ”„๋กœ์„ธ์Šค

๊ธฐํš
๋ฐ์ผ๋ฆฌ ์Šคํฌ๋Ÿผ
์Šคํ”„๋ฆฐํŠธ ๋ฆฌ๋ทฐ
์Šคํ”„๋ฆฐํŠธ ํšŒ๊ณ 
ํŠธ๋Ÿฌ๋ธ” ์ŠˆํŒ…
๊ธฐํƒ€ ์‚ฐ์ถœ๋ฌผ

๐Ÿ“– ๊ธฐ์ˆ ๋ฌธ์„œ

Week2
Week3
Week4
Week5

๐Ÿ—‚ ์ฐธ๊ณ ๋ฌธ์„œ

Clone this wiki locally