MCPcopy
hub / github.com/fabricjs/fabric.js

github.com/fabricjs/fabric.js @v6.0.2 sqlite

repository ↗ · DeepWiki ↗ · release v6.0.2 ↗
2,273 symbols 6,811 edges 432 files 890 documented · 39%
README

Fabric.js

A simple and powerful Javascript HTML5 canvas library.


🩺 🧪 CodeQL


cdnjs jsdelivr Gitpod Ready-to-Code

NPM Downloads per month Bower


Sponsor asturur Sponsor melchiar Sponsor ShaMan123 Patreon


Features

  • Out of the box interactions such as scale, move, rotate, skew, group...
  • Built in shapes, controls, animations, image filters, gradients, patterns, brushes...
  • JPG, PNG, JSON and SVG i/o
  • Typed and modular
  • Unit tested

Supported Browsers/Environments

Context Supported Version Notes
Firefox ✔️ 58
Safari ✔️ 11
Opera ✔️ chromium based
Chrome ✔️ 64
Edge ✔️ chromium based
Edge Legacy
IE11
Node.js ✔️ Node.js installation

Fabric.js Does not use transpilation by default, the browser version we support is determined by the level of canvas api we want to use and some js syntax. While JS can be easily transpiled, canvas API can't.

Installation

$ npm install fabric --save
// or
$ yarn add fabric

Browser

cdnjs jsdelivr

See browser modules for using es6 imports in the browser or use a dedicated bundler.

Node.js

Fabric.js depends on node-canvas for a canvas implementation (HTMLCanvasElement replacement) and jsdom for a window implementation on node. This means that you may encounter node-canvas limitations and bugs.

Follow these instructions to get node-canvas up and running.

Quick Start

// v6
import { Canvas, Rect } from 'fabric'; // browser
import { StaticCanvas, Rect } from 'fabric/node'; // node

// v5
import { fabric } from 'fabric';

Plain HTML

<canvas id="canvas" width="300" height="300"></canvas>

<script src="https://cdn.jsdelivr.net/npm/fabric"></script>
<script>
  const canvas = new fabric.Canvas('canvas');
  const rect = new fabric.Rect({
    top: 100,
    left: 100,
    width: 60,
    height: 70,
    fill: 'red',
  });
  canvas.add(rect);
</script>

ReactJS

import React, { useEffect, useRef } from 'react';
import * as fabric from 'fabric'; // v6
import { fabric } from 'fabric'; // v5

export const FabricJSCanvas = () => {
  const canvasEl = useRef<HTMLCanvasElement>(null);
  useEffect(() => {
    const options = { ... };
    const canvas = new fabric.Canvas(canvasEl.current, options);
    // make the fabric.Canvas instance available to your app
    updateCanvasContext(canvas);
    return () => {
      updateCanvasContext(null);
      canvas.dispose();
    }
  }, []);

  return <canvas width="300" height="300" ref={canvasEl}/>;
};

Node.js

import http from 'http';
import * as fabric from 'fabric/node'; // v6
import { fabric } from 'fabric'; // v5

const port = 8080;

http
  .createServer((req, res) => {
    const canvas = new fabric.Canvas(null, { width: 100, height: 100 });
    const rect = new fabric.Rect({ width: 20, height: 50, fill: '#ff0000' });
    const text = new fabric.Text('fabric.js', { fill: 'blue', fontSize: 24 });
    canvas.add(rect, text);
    canvas.renderAll();
    if (req.url === '/download') {
      res.setHeader('Content-Type', 'image/png');
      res.setHeader('Content-Disposition', 'attachment; filename="fabric.png"');
      canvas.createPNGStream().pipe(res);
    } else if (req.url === '/view') {
      canvas.createPNGStream().pipe(res);
    } else {
      const imageData = canvas.toDataURL();
      res.writeHead(200, '', { 'Content-Type': 'text/html' });
      res.write(`<img src="https://github.com/fabricjs/fabric.js/raw/v6.0.2/${imageData}" />`);
      res.end();
    }
  })
  .listen(port, (err) => {
    if (err) throw err;
    console.log(
      `> Ready on http://localhost:${port}, http://localhost:${port}/view, http://localhost:${port}/download`
    );
  });

See our ready to use templates.


Other Solutions

Project Description
Three.js 3D graphics
PixiJS WebGL renderer
Konva Similar features
html-to-image HTML to image/canvas

More Resources

Credits Patreon

Extension points exported contracts — how you extend this code

FabricObjectProps (Interface)
(no doc) [2 implementers]
src/shapes/Object/types/FabricObjectProps.ts
XY (Interface)
(no doc) [1 implementers]
src/Point.ts
CanvasOptions (Interface)
(no doc) [1 implementers]
src/canvas/CanvasOptions.ts
AsymmetricMatchers (Interface)
(no doc)
jest.extend.ts
NominalTag (Interface)
(no doc)
src/typedefs.ts
TEvent (Interface)
(no doc)
src/EventTypeDefs.ts
RectProps (Interface)
(no doc) [1 implementers]
src/shapes/Rect.ts
StaticCanvasOptions (Interface)
(no doc) [1 implementers]
src/canvas/StaticCanvasOptions.ts

Core symbols most depended-on inside this repo

add
called by 426
src/Point.ts
get
called by 300
src/CommonMethods.ts
item
called by 221
src/Collection.ts
done
called by 221
src/util/misc/objectEnlive.ts
toObject
called by 189
src/Shadow.ts
set
called by 141
src/CommonMethods.ts
callback
called by 136
test/unit/collection.js
on
called by 131
src/Observable.ts

Shape

Method 1,090
Function 858
Class 247
Interface 76
Enum 2

Languages

TypeScript100%

Modules by API surface

src/canvas/StaticCanvas.ts79 symbols
src/shapes/Object/Object.ts63 symbols
src/shapes/Text/Text.ts60 symbols
src/canvas/Canvas.ts53 symbols
src/canvas/SelectableCanvas.ts52 symbols
src/shapes/Group.ts46 symbols
src/shapes/Object/ObjectGeometry.ts40 symbols
src/shapes/IText/ITextBehavior.ts40 symbols
src/Point.ts37 symbols
src/shapes/Image.ts34 symbols
src/shapes/IText/IText.ts34 symbols
src/util/animation/easing.ts33 symbols

Dependencies from manifests, versioned

@babel/cli7.22.9 · 1×
@babel/core7.22.9 · 1×
@babel/preset-env7.22.9 · 1×
@babel/preset-typescript7.22.5 · 1×
@playwright/test1.31.2 · 1×
@rollup/plugin-babel6.0.3 · 1×
@rollup/plugin-json6.0.0 · 1×
@rollup/plugin-terser0.4.1 · 1×
@rollup/plugin-typescript11.1.1 · 1×
@types/fs-extra9.0.13 · 1×
@types/jest29.5.2 · 1×
@types/jsdom20.0.1 · 1×

For agents

$ claude mcp add fabric.js \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact