在页面上保存/加载JSON

时间:2018-03-01 15:38:40

标签: javascript fabricjs

我想为单个画布创建一个saver / loader,like on the Fabric.js example website(请参阅JSON选项卡)。我可以通过以下方式获取并加载到画布上,我的JSON(kudos Durga):



var canvas = new fabric.Canvas('canvas', {
  backgroundColor: 'white',
  centeredScaling: true,
  isDrawingMode: true
});

var canvas1 = new fabric.Canvas('canvas1');

function loadOnCanvas(){
 var json = canvas.toJSON();
 console.log(json)
 canvas1.loadFromJSON(json,canvas1.renderAll.bind(canvas1));
}

body {
}

canvas {
  border-radius: 2px;
  margin: 5px;
  border:1px solid #000000;
}

<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.7.22/fabric.min.js"></script>
<button onclick='loadOnCanvas()'>Load</button>
<canvas id="canvas" height="400" width="400"></canvas><br>
<canvas id="canvas1" height="400" width="400"></canvas>
&#13;
&#13;
&#13;

我如何在页面上获取JSON,以便我可以像示例一样保存/加载它?

1 个答案:

答案 0 :(得分:0)

对于遇到这篇文章的人,我最终得到了a solution (here's the JSFiddle)

&#13;
&#13;
$(function() {
  var canvas = new fabric.Canvas('c', {
    /* isDrawingMode: true */
  });

  $('#text').on('click', addtext);

  function addtext() {
    var text = new fabric.IText('Some Text!', {
      left: 10,
      top: 10
    });
    canvas.add(text);
  }

  // From Computer
  document.getElementById('imgLoader').onchange = function handleImage(e) {
    var reader = new FileReader();
    reader.onload = function(event) {
      console.log('fdsf');
      var imgObj = new Image();
      imgObj.src = event.target.result;
      imgObj.onload = function() {
        // start fabricJS stuff

        var image = new fabric.Image(imgObj);
        image.set({
          left: 0,
          top: 0,
          angle: 20,
          padding: 10,
          cornersize: 10
        });
        //image.scale(getRandomNum(0.1, 0.25)).setCoords();
        image.scale(0.2);
        canvas.add(image);

        // end fabricJS stuff
      }

    }
    reader.readAsDataURL(e.target.files[0]);
  }

  //

  var myImg = 'https://i.imgur.com/q2oGjQ9.jpg';

  $('#addImage').on('click', addImg);

  function addImg() {
    fabric.Image.fromURL(myImg, function(oImg) {
      oImg.scale(0.2);
      canvas.add(oImg);
    });
  }

  $("#canvas2json").click(function() {
    var json = canvas.toJSON();
    $("#myTextArea").text(JSON.stringify(json));
  });
  $("#loadJson2Canvas").click(function() {
    canvas.loadFromJSON(
      $("#myTextArea").val(),
      canvas.renderAll.bind(canvas));

  });
});
&#13;
#myTextArea {
  width: 200px;
  height: 200px;
}

canvas {
  border: 1px solid black
}
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.7.22/fabric.min.js"></script>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>

<button id="text">Add Text</button>

<input type="button" id="addImage" value="Add Web IMG">
<input type="file" id="imgLoader">

<br/><br/>

<canvas id='c' width=500 height=500></canvas>

<br/>

<button id='canvas2json'>canvas2json</button>
<button id='loadJson2Canvas'>loadJson2Canvas</button>

<br/><br/>

<textarea id='myTextArea' onfocus="this.select();" onmouseup="return false;"></textarea>
&#13;
&#13;
&#13;