Undo-Redo feature in Fabric.js

前端 未结 9 739
梦谈多话
梦谈多话 2020-12-04 13:28

Is there any built-in support for for undo/redo in Fabric.js? Can you please guide me on how you used this cancel and repeat in [http://printio.ru/][1]

9条回答
  •  渐次进展
    2020-12-04 14:10

    i developed a small script for you,hope it will help you .see this demo Fiddle although redo is not perfect you have to click minimum two time at undo button then redo work .you can easily solve this problem with giving simple conditions in redo code. //Html

    
      

    //script

      var canvas = new fabric.Canvas('c');
        var text = new fabric.Text('Sample', {
       fontFamily: 'Hoefler Text',
        left: 50,
       top: 30,
        //textAlign: 'center',
        fill: 'navy',
    
    });
    canvas.add(text);
    var vall=10;    
    var l=0;
    var flag=0;
    var k=1;
    var yourJSONString = new Array();
    canvas.observe('object:selected', function(e) {
        //yourJSONString = JSON.stringify(canvas);
        if(k!=10)
    {   
    yourJSONString[k] = JSON.stringify(canvas);
    k++;
    }
    j = k;
        var activeObject = canvas.getActiveObject();
        });
    $("#undo").click(function(){
         if(k-1!=0)
         {
         canvas.clear();
         canvas.loadFromJSON(yourJSONString[k-1]);
         k--;
         l++;
         } 
      canvas.renderAll();   
     });
    
    $("#redo").click(function(){
        if(l > 1)
         {  
          canvas.clear();
          canvas.loadFromJSON(yourJSONString[k+1]);
           k++;
           l--;
          canvas.renderAll();   
         }
     });
    
    $("#clear").click(function(){
        canvas.clear();
      });
     $("#addtext").click(function(){
    var text = new fabric.Text('Sample', {
       fontFamily: 'Hoefler Text',
        left: 100,
        top: 100,
        //textAlign: 'center',
        fill: 'navy',
    
    });
    canvas.add(text);
    }); 
    

提交回复
热议问题