GithubHelp home page GithubHelp logo

kid-1912 / fabricjs-history Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 2 KB

Maintain fabric history, support undo redo, etc.

Home Page: https://www.npmjs.com/package/fabricjs-history

JavaScript 100.00%
fabric fabricjs history plugin

fabricjs-history's Introduction

fabricjs-history

fabric.js添加历史记录维护,支持撤销重做等操作; Maintain fabric history, support undo redo, etc.



NPM URL version

Install

npm install fabricjs-history -S

Usage

Initialization

createHistory() 将初始化历史记录状态

import { fabric } from "fabric";
import { createHistory } from "fabricjs-history";

const canvas = new fabric.Canvas($canvas, {
  width,
  height,
  selection: false,
  hoverCursor: "default",
  freeDrawingCursor: "none",
});
// ...
createHistory({ canvas });

Operation

record:记录当前画布状态

canvas.setBackgroundColor(color, canvas.renderAll.bind(canvas));
canvas.record();

fabric.Image.fromURL(
  url,
  function (img) {
    canvas.setBackgroundImage(img);
    canvas.renderAll();
    canvas.record(); // 记录当前画布状态
  },
  { crossOrigin: "Anonymous" }
);

除了手动 record 历史记录,还可以监听画布事件,自动记录历史记录

import { fabric } from "fabric";
import { createHistory } from "fabricjs-history";

const canvas = new fabric.Canvas($canvas, {
  width,
  height,
});

// ...

createHistory({
  canvas,
  maxHistoryLength: 20, //可选,最大历史记录长度,默认20
  historyEvent: ["path:created", "erasing:end", ...], // 可选,监听的画布事件自动记录,默认为[]
});

注:fabricjs 相关基础事件可见 http://fabricjs.com/events

undo:撤销操作

redo:重做操作

reset:重置历史记录

Properties

canvas._stack:历史记录栈,查看历史记录条数 canvas._stack.length

canvas._historyIndex:当前历史记录索引

Event

canvas.on("history:change", function (action) {
  console.log(action); // undo | redo | reset | record
  console.log(canvas._historyIndex, canvas.canvas._stack);
});

fabricjs-history's People

Contributors

kid-1912 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.