npm 中文文档 npm 中文文档
指南
npmjs.com (opens new window)
指南
npmjs.com (opens new window)
  • 快速入门

    • npm 是什么?
    • npm 安装和更新
    • npm 防止权限错误
    • npm package.json 文件
    • npm 安装包
    • npm 更新包
    • npm 卸载包
    • npm 创建 Node.js 模块
    • npm 发布和更新包
    • npm 使用语义化版本
    • npm 使用 Dist-tags 标记包
    • npm 包和模块的了解
  • 命令行
  • 配置 npm

Fabric.js


undefined

A simple and powerful Javascript HTML5 canvas library.

Website
GOTCHAS
Contributing, Developing and More

🩺 🧪 CodeQL

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 ✔️ modern version (tbd)
Safari ✔️ version >= 10.1
Opera ✔️ chromium based
Chrome ✔️ modern version (tbd)
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.

Migrating to v6


v6 is a MAJOReffort including migrating to TS and es6, countless fixes, rewrites and features.Currently in beta, refer to #8299 for guidance.

  1. ``` shell
  2. $ npm install fabric@beta --save
  3. // or
  4. $ yarn add fabric@beta
  5. ```

Installation


  1. ``` shell
  2. $ npm install fabric --save
  3. // or
  4. $ yarn add fabric
  5. ```

Browser


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


  1. ``` js
  2. // v6
  3. import { Canvas, Rect } from 'fabric'; // browser
  4. import { StaticCanvas, Rect } from 'fabric/node'; // node

  5. // v5
  6. import { fabric } from 'fabric';
  7. ```

Plain HTML
  1. ``` html
  2. <canvas id="canvas" width="300" height="300"></canvas>
  3. <script src="https://cdn.jsdelivr.net/npm/fabric"></script>
  4. <script>
  5.   const canvas = new fabric.Canvas('canvas');
  6.   const rect = new fabric.Rect({
  7.     top: 100,
  8.     left: 100,
  9.     width: 60,
  10.     height: 70,
  11.     fill: 'red',
  12.   });
  13.   canvas.add(rect);
  14. </script>
  15. ```

ReactJS
  1. ``` tsx
  2. import React, { useEffect, useRef } from 'react';
  3. import * as fabric from 'fabric'; // v6
  4. import { fabric } from 'fabric'; // v5

  5. export const FabricJSCanvas = () => {
  6.   const canvasEl = useRef<HTMLCanvasElement>(null);
  7.   useEffect(() => {
  8.     const options = { ... };
  9.     const canvas = new fabric.Canvas(canvasEl.current, options);
  10.     // make the fabric.Canvas instance available to your app
  11.     updateCanvasContext(canvas);
  12.     return () => {
  13.       updateCanvasContext(null);
  14.       canvas.dispose();
  15.     }
  16.   }, []);

  17.   return <canvas width="300" height="300" ref={canvasEl}/>;
  18. };
  19. ```

Node.js
  1. ``` js
  2. import http from 'http';
  3. import * as fabric from 'fabric/node'; // v6
  4. import { fabric } from 'fabric'; // v5

  5. const port = 8080;

  6. http
  7.   .createServer((req, res) => {
  8.     const canvas = new fabric.Canvas(null, { width: 100, height: 100 });
  9.     const rect = new fabric.Rect({ width: 20, height: 50, fill: '#ff0000' });
  10.     const text = new fabric.Text('fabric.js', { fill: 'blue', fontSize: 24 });
  11.     canvas.add(rect, text);
  12.     canvas.renderAll();
  13.     if (req.url === '/download') {
  14.       res.setHeader('Content-Type', 'image/png');
  15.       res.setHeader('Content-Disposition', 'attachment; filename="fabric.png"');
  16.       canvas.createPNGStream().pipe(res);
  17.     } else if (req.url === '/view') {
  18.       canvas.createPNGStream().pipe(res);
  19.     } else {
  20.       const imageData = canvas.toDataURL();
  21.       res.writeHead(200, '', { 'Content-Type': 'text/html' });
  22.       res.write(`<img src="${imageData}" />`);
  23.       res.end();
  24.     }
  25.   })
  26.   .listen(port, (err) => {
  27.     if (err) throw err;
  28.     console.log(
  29.       `> Ready on http://localhost:${port}, http://localhost:${port}/view, http://localhost:${port}/download`
  30.     );
  31.   });
  32. ```

See our ready to use templates.

Other Solutions


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

More Resources


Demos on fabricjs.com
Fabric.js on Twitter
Fabric.js on CodeTriage
Fabric.js on Stack Overflow
Fabric.js on jsfiddle
Fabric.js on Codepen.io

Credits


kangax
asturur on Twitter
melchiar
ShaMan123
Ernest Delgado for the original idea of manipulating images on canvas
Maxim "hakunin" Chernyak for ideas, and help with various parts of the library throughout its life
Sergey Nisnevich for help with geometry logic
Stefan Kienzle for help with bugs, features, documentation, GitHub issues
Shutterstock for the time and resources invested in using and improving Fabric.js
and all the other contributors
Last Updated: 2023-05-15 10:22:02