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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@fabric-fusion/core

v1.2.5

Published

Core library for Fabric.js canvas initialization

Downloads

39

Readme

Switch to Chinese Version (切换到中文版)

Fabric Fusion Core

Module Features

fabric-fusion/core is a high-performance canvas management module based on Fabric.js. It encapsulates common canvas operations, configuration management, and event listeners, aiming to improve the efficiency and maintainability of developing with Fabric.js.

Main Modules

1. Core Encapsulation

  • Provides core Fabric.js canvas management functions, including canvas initialization, object addition, event binding, drag-and-zoom support.
  • Supports adaptive external container size with dynamic canvas resizing.
  • Offers resource cleanup mechanisms suitable for complex application scenarios.

2. Parameterized Default Configuration

  • Stores default module configurations, such as constants for zoom range (ZOOM_RANGE).
  • Facilitates centralized management and unified parameter modification.

Encapsulation of Common Fabric.js Event Logic

  • Mouse wheel zoom
  • Canvas drag
  • Object movement
  • Mouse click
  • Supports extension and custom event listeners

Debounce Mechanism

  • Provides a general debounce function to optimize performance and prevent issues caused by high-frequency events (e.g., resize and mouse:move).

Module Features

1. Flexible Configuration

  • Supports customizing the module's default configuration via config.ts.
  • Users can override default settings for personalized adjustments.

2. Highly Modular

  • Separates event handling, utility functions, and core logic for clear code structure, facilitating maintenance and extension.
  • All functions are importable as needed, avoiding unnecessary code loading.

3. Extensive Event Support

  • Built-in common Fabric.js events such as mouse wheel zoom and canvas drag.
  • Provides event extension interfaces for users to add custom event listeners.

4. Performance Optimization

  • Optimizes high-frequency event handling through debouncing and event management to improve rendering performance.
  • Supports resource release and event unbinding to avoid memory leaks.

5. Adaptive Containers

  • Supports dynamic canvas resizing to automatically adapt to external container size changes.
  • Optionally enables ResizeObserver or global resize event listening.

Usage

Installation

Ensure that fabric-fusion and fabric modules are installed:

Note: The fabric module needs to be installed and supports only version 5.x, not version 6.x.

npm install fabric fabric-fusion
<template>
  <div class="fabric-container">
    <h1>Fabric Canvas Demo</h1>
    <div class="toolbar">
      <button @click="addRectangle">Add Rectangle</button>
      <button @click="addCircle">Add Circle</button>
      <button @click="resetCanvas">Reset Canvas</button>
      <button @click="clearCanvas">Clear Canvas</button>
    </div>
    <div class="canvas-wrapper" ref="containerRef">
      <canvas id="fabric-canvas" ref="canvasRef"></canvas>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref, onMounted, onUnmounted } from "vue";
import { FabricRender, fabric } from "@fabric-fusion/core";

// Define references to the container and canvas
const containerRef = ref<HTMLDivElement | null>(null);
const canvasRef = ref<HTMLCanvasElement | null>(null);
const fabricRender = new FabricRender();

// Add a rectangle
const addRectangle = () => {
  fabricRender.add([
    new fabric.Rect({
      left: Math.random() * 200,
      top: Math.random() * 200,
      width: 50,
      height: 50,
      fill: "blue",
    }),
  ]);
};

// Add a circle
const addCircle = () => {
  fabricRender.add([
    new fabric.Circle({
      left: Math.random() * 200,
      top: Math.random() * 200,
      radius: 25,
      fill: "green",
    }),
  ]);
};

// Reset the canvas viewport
const resetCanvas = () => {
  fabricRender.canvas.setViewportTransform([1, 0, 0, 1, 0, 0]);
  fabricRender.canvas.requestRenderAll();
};

// Clear the canvas
const clearCanvas = () => {
  fabricRender.clear();
};

// Custom event listener - Mouse click event
const customMouseDownHandler = (opt: any) => {
  const pointer = fabricRender.canvas.getPointer(opt.e);
  console.log(
    `Custom Event: Mouse clicked at x: ${pointer.x}, y: ${pointer.y}`
  );
};

// Vue lifecycle hook - Initialize on component mount
onMounted(() => {
  if (canvasRef.value && containerRef.value) {
    fabricRender.init(
      canvasRef.value,
      {
        backgroundColor: "#f9f9f9", // Set canvas background color
        enableDefaultListeners: true, // Enable default event listeners
        customListeners: {
          "mouse:down": customMouseDownHandler, // Add custom mouse click event
        },
      },
      containerRef.value
    );
  }
});

// Vue lifecycle hook - Clean up resources on component unmount
onUnmounted(() => {
  fabricRender.dispose();
});
</script>

<style scoped>
.fabric-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 20px;
}

.toolbar {
  margin-bottom: 20px;
}

.toolbar button {
  margin-right: 10px;
  padding: 10px;
  font-size: 14px;
  cursor: pointer;
}

.canvas-wrapper {
  position: relative;
  width: 100%;
  height: 600px;
  border: 1px solid #ccc;
}
</style>

Fabric-Fusion Community Discussions

Fabric-Fusion Discussions is a place to ask questions, share ideas, and engage with the community.