首页 > 其他分享 >vue2 + fabric.js 实现撤销、恢复效果

vue2 + fabric.js 实现撤销、恢复效果

时间:2023-05-16 17:56:46浏览次数:35  
标签:canvasState index canvas fabric js stateIndex vue2 isLoadCanvas

示例

全部代码

<template>
  <div>
    <canvas id="canvas" width="400" height="400" style="border: 1px"></canvas>
    <button :disabled="!canUndo" @click="historyState(stateIndex - 1)">
      撤销
    </button>
    <button :disabled="!canRedo" @click="historyState(stateIndex + 1)">
      恢复
    </button>
  </div>
</template>

<script>
import { fabric } from "fabric";
export default {
  data() {
    return {
      canvas: null,
      isLoadCanvas: false,
      canvasState: [],
      stateIndex: -1,
    };
  },
  mounted() {
    this.canvas = new fabric.Canvas("canvas", {
      backgroundColor: "#f5deb3",
    });
    this.canvas.on("object:modified", this.updateCanvasState);
    this.canvas.on("object:added", this.updateCanvasState);
    this.addObject();
  },
  computed: {
    canUndo() {
      return this.stateIndex > 0;
    },
    canRedo() {
      return this.stateIndex < this.canvasState.length - 1;
    },
  },
  methods: {
    addObject() {
      const rect = new fabric.Rect({
        left: 100,
        top: 100,
        fill: "red",
        width: 200,
        height: 200,
      });
      this.canvas.add(rect);
      this.canvas.renderAll();
    },

    updateCanvasState() {
      if (!this.isLoadCanvas) {
        const canvasAsJson = JSON.stringify(this.canvas.toJSON());
        this.canvasState.splice(this.stateIndex + 1);
        this.canvasState.push(canvasAsJson);
        this.stateIndex = this.canvasState.length - 1;
      } else {
        this.isLoadCanvas = false;
      }
    },

    historyState(index) {
      if (this.canUndo || this.canRedo) {
        this.isLoadCanvas = true;
        this.canvas.loadFromJSON(this.canvasState[index], () => {
          this.canvas.renderAll();
          this.stateIndex = index;
        });
      }
    },
  },
};
</script>

标签:canvasState,index,canvas,fabric,js,stateIndex,vue2,isLoadCanvas
From: https://www.cnblogs.com/lwlblog/p/17406392.html

相关文章

  • JS和Document
    对象1.newvarobj=newObject();2.函数声明对象functionHuman(){};3.varobj={};大括号就是对象varobj={};obj.name='张三';obj.age=20;obj.say=function(){};4.varobj={};大括号内放入属性用:varobj={ name:'张三', age:20, sex:0, run:functi......
  • js深拷贝
    js深拷贝在JavaScript中,有多种方法可以实现对象的深拷贝,下面介绍几种常用的方式:手动遍历对象进行复制functiondeepCopy(obj){if(typeofobj!=='object'||obj===null){returnobj;}letnewObj=Array.isArray(obj)?[]:{};for(letkeyin......
  • threejs webgl性能优化
    threejswebgl性能优化WEBGL性能优化的方法有很多,以下是一些常见的方法:减少渲染次数:在WEBGL中,渲染次数越少,性能越好。因此,您可以通过减少渲染次数来提高性能。例如,使用批处理技术将多个对象合并为一个批处理对象进行渲染。降低纹理分辨率:纹理分辨率越高,渲染的时间就越长。......
  • JS逆向 -- 分析某站aid、cid、w_rid和sid的加密过程
    接上节课内容JS逆向--分析某站buvid3和_uuid的加密过程JS逆向--分析某站b_lsid值加密过程一、清除cookie信息,刷新网页,ctrl+f搜索sid,这样找到的数据是在url里或者响应信息里面,全局搜索找到的一般都是在js里面的数据,找到一个v2的数据包,里面有setcookie二、该数据包是get请求,分析......
  • jsp中实现文件上传下载的三种解决方案(推荐)
    ​ 我们平时经常做的是上传文件,上传文件夹与上传文件类似,但也有一些不同之处,这次做了上传文件夹就记录下以备后用。这次项目的需求:支持大文件的上传和续传,要求续传支持所有浏览器,包括ie6,ie7,ie8,ie9,Chrome,Firefox,360安全浏览器,并且刷新浏览器后仍然能够续传,重启浏览器(关闭......
  • SQLSERVER中JSON数组的拆分
    DECLARE@infoParamNVARCHAR(MAX);DECLARE@itemsNVARCHAR(MAX);SET@infoParam='{ "SCHOOL":"某某中学", "SCHOOLCODE":"1234", "USER":[{ "userid":"20XX001", "username......
  • JS / jQuery 刷新页面的方法
    <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title></title><!--引入jQuery--><scriptsrc="jq.js"></script></head><body>......
  • Newtonsoft.JSON 自定义JsonConverter
    引用:https://www.cjavapy.com/article/2513/https://www.cnblogs.com/weihanli/p/11080531.htmlhttps://www.cnblogs.com/Lulus/p/16968656.htmlhttps://www.cjavapy.com/article/2513/https://www.cnblogs.com/net-sky/p/16563025.htmlpublicclassDecimalConver......
  • js---校验身份证号码
    分享一个校验身份证号码的JS代码://校验身份证号码exportfunctionisIdCard(idCard){//15位和18位身份证号码的正则表达式varregIdCard=/^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((......
  • 用JFreeChart增强JSP报表的用户体验(2)
     三. 饼图在WebRoot目录下建立名为pie的子目录,用来存放本教程中饼图的实例jsp页面。下面让我们来看一个简单的三维饼图。首先在pie目录下建立一个名为sample1.jsp的页面。在JFreeChart中,与饼图绘制密切相关的类如下:1) PiePlot饼图绘制类,可以用来设置饼图的相关属性。例......