npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@yassidev/fabric-extensions

v1.1.13

Published

Extensions to fabric.js

Downloads

207

Readme

Fabric Extensions

This package contains a list of different plugins/extensions to use with fabric.js. 🎉️

Installation

Simply install the package by running:

yarn add @yassidev/fabric-extensions

Then simply include it into your application.

import { fabric } from 'fabric'
import { install } from '@yassidev/fabric-extensions'

install(fabric)

You may also simply need:

import 'fabric'
import '@yassidev/fabric-extensions'

You may also only install the extensions you want:

import { fabric } from 'fabric'
import { textVerticalAlign, canvasShortcuts, canvasDrop } from '@yassidev/fabric-extensions'

textVerticalAlign(fabric)
canvasShortcuts(fabric)
canvasDrop(fabric)

Extensions

This is the list of current extensions.

NEW Image controls

This extension will allow you to crop your images. Simply double click on your object to enter the "editing mode". Unselect or double click on your object to exit the "editing mode".

import { fabric } from 'fabric'
import { imageControls } from '@yassidev/fabric-extensions'

imageControls(fabric)

const image = new fabric.Image(img, {
  /**
   * Length of the corner sides in editing mode.
   */
  cornerLengthEditing: 5,

  /**
   * Color of the corner stroke in editing mode.
   */
  cornerStrokeColorEditing: 'black',

  /**
   * Size of the corner stroke in editing mode.
   */
  cornerSizeEditing: 2,
})
NEW Polyline controls

This extension will allow you to edit the points of your polygons/polylines/arrows. Simply double click on your object to enter the "editing mode". You may then move your points by dragging them, remove a point by right clicking it or adding a new point by double clicking on a segment. Unselect or double click on your object to exit the "editing mode".

import { fabric } from 'fabric'
import { polylineControls } from '@yassidev/fabric-extensions'

polylineControls(fabric)

const polyline = new fabric.Polyline(
  [
    { x: 0, y: 10 },
    { x: 10, y: 50 },
  ],
  {
    /**
     * Whether borders should be visible in editing mode.
     */
    hasBordersEditing: false,

    /**
     * The type of corner style in editing mode.
     */
    cornerStyleEditing: 'circle',

    /**
     * The corner color in editing mode.
     */
    cornerColorEditing: 'rgba(0,0,255,0.5)',

    /**
     * Tolerance distance from line to add a point.
     */
    toleranceFromLine: 10,
  }
)
Canvas Cover Background

This extension will simply automatically scale the canvas background/overlay image to cover it while keeping its aspect ratio. This will also saintore the backgroundImage/overlayImage property of the canvas.toJSON() as a string rather than an object.

import { fabric } from 'fabric'
import { canvasCoverBackground } from '@yassidev/fabric-extensions'

canvasCoverBackground(fabric)

fabric.backgroundImage = 'https://source.unsplash.com/random'
Canvas Drop

This extension will allow you to drop elements on the canvas directly ! By default, the canvas can already insert images or text but you can extend your canvas to allow for other DataTransferItem types to be included.

import { fabric } from 'fabric'
import { canvasDrop } from '@yassidev/fabric-extensions'

canvasDrop(fabric)

fabric.util.registerDataTransferType('image', (data: DataTransferItem, canvas: fabric.Canvas) => {
  // do whatever you want with images.
})

fabric.util.registerDataTransferType(
  'image/jpeg',
  (data: DataTransferItem, canvas: fabric.Canvas) => {
    // do whatever you want with JPEG images specifically.
  }
)

Canvas Guidelines

This extension will add centering and aligning guidelines to your canvas when placing objects.

import { fabric } from 'fabric'
import { canvasGuidelines } from '@yassidev/fabric-extensions'

canvasGuidelines(fabric)

Canvas Shortcuts

This extension will allow you to register keyboard shortcuts to interact with you canvas. You may choose the shortcuts you want to include or register new ones using the below helper method.

import { fabric } from 'fabric'
import { canvasShortcutsCopy, canvasShortcutsGroup } from '@yassidev/fabric-extensions'

canvasShortcutsCopy(fabric)
canvasShortcutsGroup(fabric)

fabric.util.registerShortcut('ctrl+alt+m', (canvas: Canvas, event: KeyboardEvent) => {
  // do something
})

These are the currently available shortcuts:

  • [x] copy/paste: using the native copy/paste ClipboardEvent
  • [x] delete: Delete or Backspace key to remove an object
  • [x] group: Ctrl+G to group/ungroup the selection
  • [x] move: ArrowLeft/Right/Up/Down to move your selection (+/- 1)
  • [x] move more: ArrowLeft/Right/Up/Down + Shift to move your selection (+/- 10)
  • [x] rotate: ArrowLeft/Right + Ctrl to rotate your selection (+/- 1deg)
  • [x] rotate more: ArrowLeft/Right + Ctrl + Shift to rotate your selection (+/- 10deg)
  • [x] order: ArrowUp/Down + Ctrl to bringForward / sendBackwards your selection.
  • [x] order more: ArrowUp/Down + Ctrl + Shift to bringToFront / sendToBack your selection.
  • [x] select all: Ctrl + A to select all objects.

Image Import

This extension will allow you to very easily create new fabric.Image instances from a URL or a File. The extension only adds three helper methosd on the fabric.Image constructor.

import { fabric } from 'fabric'
import { imageImport } from '@yassidev/fabric-extensions'

imageImport(fabric)

fabric.Image.from(string|File, ...options) => Promise<fabric.Image>
fabric.Image.fromSrc(string, ...options) => Promise<fabric.Image>
fabric.Image.fromFile(File, ...options) => Promise<fabric.Image>

Object Export Animations

This extension will allow you to export animations in your JSON. The concept is simple: you can register animations on your fabric instance and include them in the new "animations" array property of your fabric.Object instances.

import { fabric } from 'fabric'
import { objectExportAnimations } from '@yassidev/fabric-extensions'

objectExportAnimations(fabric)

fabric.util.registerAnimation('rotate', (object: fabric.Object, animation: fabric.CustomAnimation) => {
	return {
		duration: 1000,
		delay: 0,
		times: 0, // 0 = Infinite, default: 1
		reverse: false, // Reverse after each iteration
		easing: 'easeInBack', // default: linear
		originX: 'center', // defaut: left
		originY: 'center', // defaut: top
		to: { angle: 360 },
		data: null // set any data you want
		...animation // merge with customized animation
	}
})

const object = new fabric.Rect({
	animations: [
		{
			name: 'rotate',
			trigger: 'mousedown',
			duration: 2000 // customize the default animation
		},
		{
			trigger: null,  // no trigger = on added
			duration: 1000,
			from: { opacity: 0 },
			to: { opacity: 1},
			delay: 1000
		}
	]
})

Object Export Events

This extension will allow you to export events in your JSON. The concept is simple: you can register events on your fabric instance and include them in the new "events" array property of your fabric.Object instances.

import { fabric } from 'fabric'
import { objectExportAnimations } from '@yassidev/fabric-extensions'

objectExportAnimations(fabric)

fabric.util.regiterEvent(
  'link',
  (object: fabric.Object, event: fabric.CustomEvent, e: fabric.IEvent) => {
    window.open(event.data.url)
  }
)

const object = new fabric.Rect({
  events: [
    {
      name: 'link',
      data: { url: 'https://github.com' },
    },
  ],
})

Text Vertical Align

This extension will allow you to set the vertical alignment of your fabric.Text/fabric.Textbox instances. The value of the new "verticalAlign" property can be set to "top" (default), "middle" or "bottom". This extension will also prevent the distorsion of your text while scaling.

import { fabric } from 'fabric'
import { textVerticalAlign } from '@yassidev/fabric-extensions'

textVerticalAlign(fabric)

const object = new fabric.Textbox('Some text', {
  verticalAlign: 'middle',
  textAlign: 'center',
})

Icons

This extension will allow you to add SVG path icons into your canvases. The extensions creates a new class: fabric.Icon.

import { fabric } from 'fabric'
import { icons } from '@yassidev/fabric-extensions'
import * as mdi from '@mdi/js'
import { kebabCase } from 'lodash'

icons(fabric)

fabric.util.registerIconLibrary('mdi', () => {
  return Object.entries(mdi).reduce((all, [key, value]) => {
    const normalized = kebabCase(key.replace(/^mdi/, ''))
    all[normalized] = value
    return all
  }, {})
})

const object = new fabric.Icon({
  iconName: 'emoticon-happy-outline',
  iconLibrary: 'mdi', // unnecessary here as there is only one library
  width: 100,
  height: 100,
})

Map

This extension will allow you to add dynamic GeoJson content as a group of paths into your canvas. The extensions creates a new class: fabric.Map.

fabric.util.registerGeoJson('world', async () => {
  const fetched = await fetch(
    'https://raw.githubusercontent.com/johan/world.geo.json/master/countries.geo.json'
  )
  return await fetched.json()
})

const map = new fabric.Map({
  features: ['France'], // select the features you want to display or leave an empty array to display all
  byKey: 'properties.name', // set the key by which the features are filtered
  geojson: 'world', // unnecessary here as there is only one registered geojson
  width: 500,
  height: 500,
})

/**
 * Create features-specific events!
 */

map.on('mouseover:France', ({ target }) => {
  target.set({ fill: 'blue' })
  instance.requestRenderAll()
})

map.on('mouseout:France', ({ target }) => {
  target.set({ fill: 'black' })
  instance.requestRenderAll()
})
License

MIT