Skip to content

Latest commit

 

History

History
127 lines (97 loc) · 3.47 KB

File metadata and controls

127 lines (97 loc) · 3.47 KB

Fabric Erase2d

Fabric bindings for erase2d. Uses PencilBrush for the drawing interaction.

Caveat

Fabric has clipping limitations that must be worked around in order to support erasing. I decided not to hack into fabric using prototype mutation to avoid coupling.

This means that an object's clip path is subject to change by ClippingGroup when erasing is committed. It will replace an exiting clip path with new ClippingGroup([existingClipPath]) if it didn't already so you can expect clip paths to change after their object was erased for the first time. This has an advantage as well. It allows to clip an object with a number of clip paths simply by adding them to the clipping group so it can be used regardless of erasing. Best practice might be to use ClippingGroup for every clip path. This will keep your object changes to a minimum.

In case you wish to remove the existing clip path but not affect erasing:

// check if the object was erased
object.clipPath instanceof ClippingGroup
  ? object.clipPath.remove(
      object.clipPath.item(0) /** the existing clip path is first */
    )
  : delete object.clipPath;

Type Caveat

The erasable property is added by this module. In case typescript fails to augment fabric, update your tsconfig.ts:

  ...
  "compilerOptions": {
    "types": ["node_modules/@erase2d/fabric/types"]
  }

Quick Start

npm i fabric @erase2d/fabric --save
import { EraserBrush, ClippingGroup } from '@erase2d/fabric';
import { Circle } from 'fabric';

const circle = new Circle({ radius: 50, erasable: true });
canvas.add(circle);

const eraser = new EraserBrush(canvas);
eraser.width = 30;

eraser.on('start', (e) => {
  // inform something
});

eraser.on('end', async (e) => {
  // prevent from committing erasing to the tree
  e.preventDefault();

  const { path, targets } = e.detail;
  const isErased = targets.includes(circle);

  // commit erasing manually
  const pathPerObjectMap = await eraser.commit({ path, targets });

  const committedEraser = circle.clipPath instanceof ClippingGroup;
});

canvas.freeDrawingBrush = eraser;
canvas.isDrawingMode = true;

Refer to the example app for in depth usage.

Sponsors

RemBG.com Logo RemBG Background Remover API
https://www.rembg.com

Accurate and affordable background remover API - from 1,90$ per mo

Migrating from fabric@5

The logic has been reworked from the bottom. Eraser has been removed in favor of the leaner ClippingGroup. Replacing the type in your data should be enough.

static type = 'clipping';

- type: 'eraser'
+ type: 'clipping'

Dev

The build command will build src and copy core into dist.

For the rest, see the main README.