使用fabric.js在画布中撤消和重做命令

时间:2013-09-25 07:08:38

标签: javascript jquery canvas fabricjs

我想在我的小网站中使用undo和redo命令,我正在使用fabric.js,任何人都知道如何做到这一点,以下是我的代码。点击撤消&重做按钮我试图撤消&重做画布的对象这是js小提琴链接

Here is fiddle link

$(document).ready(function(){
 var canvas = new fabric.Canvas('c');
   var colorSet="red";
   $("#svg3").click(function(){
     fabric.loadSVGFromURL('http://upload.wikimedia.org/wikipedia/en/c/cd/-Islandshreyfingin.svg', function (objects, options) { 
          var shape = fabric.util.groupSVGElements(objects, options);
     shape.set({
            left: canvas.width/2,
            top: canvas.height/2,
            scaleY: canvas.height / shape.width,
            scaleX: canvas.width / shape.width
        });
       if (shape.isSameColor && shape.isSameColor() || !shape.paths) {
            shape.setFill(colorSet);
        } else if (shape.paths) {
            for (var i = 0; i < shape.paths.length; i++) {
                shape.paths[i].setFill(colorSet);
            }
        }

        canvas.add(shape);
        canvas.renderAll();
      });
     });


$("#undo").click(function(){

yourJSONString = JSON.stringify(canvas);
});
$("#redo").click(function(){
canvas.clear();
//alert(yourJSONString);
canvas.loadFromJSON(yourJSONString);
canvas.renderAll();
// alert(svgobj);
});
    });

2 个答案:

答案 0 :(得分:0)

var mods = 0;
//Undo Functionality
$scope.undo = function() 
{
    if (mods < $scope.state.length) {
    $rootScope.canvas.clear().renderAll();
    $rootScope.canvas.loadFromJSON($scope.state[$scope.state.length-1 - mods - 1]);         
    $rootScope.canvas.renderAll();        
    mods += 1;
    }
   }

  //Redo Functionality
    $scope.redo = function() 
    {
        if (mods > 0) {
        $rootScope.canvas.clear().renderAll();
        $rootScope.canvas.loadFromJSON($scope.state[$scope.state.length-1 - mods + 
     1]);
        $rootScope.canvas.renderAll();
        mods -= 1;
        }



}

这很好用吧!

答案 1 :(得分:-1)

你可以使用javascript和jquery编写自己的undo和redo函数,如下所示:

// --------------------------------undo+redu--------------------------
var vall=20;
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!=20)
    {
        yourJSONString[k] = JSON.stringify(canvas);
        k++;
        //$('#btn').show();
    }        
    j = k;
    var activeObject = canvas.getActiveObject();
});


$("#undo").click(function(){

  // counts the no of objects on canvas
  var objCount = canvas.getObjects().length;
  console.log("Undo"+objCount);

      if(k-1 >=0)
    {
        canvas.clear();
        canvas.loadFromJSON(yourJSONString[k-1]);
        k--;
        l++;
    }else

     {
         canvas.clear();

           k--;
           l++;
     }          
    canvas.renderAll();   

});

$("#redo").click(function(){
     // counts the no of objects on canvas
  var objCount = canvas.getObjects().length;
  console.log("redo"+objCount);

    if(l > 1)
    {
      if (objCount = 0) {
        canvas.clear();
        canvas.loadFromJSON(yourJSONString[k-1]);
        k++;
        l--;
        canvas.renderAll();

      }
      else{

        canvas.clear();
        canvas.loadFromJSON(yourJSONString[k+1]);
        k++;
        l--;
        canvas.renderAll();
      }

    }

});

此代码将对您在画布上进行的每个更改执行撤消和重做。它将在每次更改时保存画布状态,然后在需要时执行撤消和重做。