自动更新的画布未更新Toggled Image&其他物件

时间:2018-02-01 19:31:56

标签: fabricjs

我有这些自动更新的画布,并遇到以下问题;合并图像切换1)当我切换图像时,图像更新不会被反射,2)其他对象没有按照应有的方式显示(因为它们在第一个画布中)。 我的代码:

var canvas = [];
canvas.push(new fabric.Canvas('c0'));
for (i = 1; i <= 7; i++) {
  canvas.push(new fabric.StaticCanvas('sc' + i));
}

function dropText() {
  var text = new fabric.IText('Type here...', {
    fontSize: 27,
    top: 10,
    left: 10,
  });
  canvas[0].add(text);
}

var rect = new fabric.Rect({
  fill: '#ff0000',
  width: 100,
  height: 100,
  id: 1
});
var circle = new fabric.Circle({
  fill: '#ffff00',
  radius: 50,
  left: 150,
  top: 150,
  originX: 'center',
  originY: 'center',
  id: 2
});

canvas[0].on('object:added', onModified);
canvas[0].on('object:modified', onModified);
canvas[0].on('object:scaling', onModified);
canvas[0].on('object:moving', onModified);
canvas[0].add(rect, circle);

function onModified(option) {
  var ob = option.target;
  var index = canvas[0].getObjects().indexOf(ob);
  ob.clone(function(obj) {
    for (i = 1; i <= 7; i++) {
      canvas[i].insertAt(obj, index, true);
    }
  });
};

// Toggling Images
function replaceImage(oImgObj, imgUrl) {
  if (!isImageLoaded) return; //return if initial image not loaded
  var imgElem = oImgObj._element; //reference to actual image element
  imgElem.src = imgUrl; //set image source
  imgElem.onload = () => canvas[0].renderAll(); //render on image load
}

// Default (Blank)
fabric.Image.fromURL('https://i.imgur.com/SamdNdX.png', function(img) {
  isImageLoaded = true;
  oImg = img.set({
    selectable: false,
    evented: false,
  });
  canvas[0].add(oImg).renderAll();
  canvas[0].sendToBack(oImg);
});

$('#update').click(function() {
  var json = JSON.stringify(canvas[0]);
  for (i = 1; i <= 7; i++) {
    canvas[i].loadFromJSON(json);
  }
});

$('#save').click(function() {
  html2canvas($('#imagesave'), {
    onrendered: function(canvas) {
      var a = document.createElement('a');
      // toDataURL defaults to png, so we need to request a jpeg, then convert for file download.
      a.href = canvas.toDataURL("image/jpeg").replace("image/jpeg", "image/octet-stream");
      a.download = 'myfile.jpg';
      a.click();
    }
  });
});
body {
  margin: 0px;
  padding: 0px;
}

canvas {
  border: 1px solid #f00;
  margin: 0px;
  display: block;
}

td {
  margin: 0;
  padding: 0;
  border: 0;
  outline: 0;
  vertical-align: baseline;
}

#imagesave {
  background-color: white;
  height: 637.5px;
  width: 825px;
  padding-left: 75px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.7.22/fabric.js"></script>
<button onclick="dropText();" class="dropdown-item">Add Text</button><button id="save">Save</button>
<button onclick="replaceImage(oImg, 'https://i.imgur.com/SamdNdX.png')">Blank</button>
<button onclick="replaceImage(oImg, 'https://i.imgur.com/TIINd6E.png')">Hands Pic</button>


<div id="imagesave">

  <table>
    <tr>
      <td>
        <canvas id="c0" width="187.5" height="318.75"></canvas>
      </td>
      <td>
        <canvas id="sc1" width="187.5" height="318.75"></canvas>
      </td>
      <td>
        <canvas id="sc2" width="187.5" height="318.75"></canvas>
      </td>
      <td>
        <canvas id="sc3" width="187.5" height="318.75"></canvas>
      </td>
    </tr>
    <tr>
      <td>
        <canvas id="sc4" width="187.5" height="318.75"></canvas>
      </td>
      <td>
        <canvas id="sc5" width="187.5" height="318.75"></canvas>
      </td>
      <td>
        <canvas id="sc6" width="187.5" height="318.75"></canvas>
      </td>
      <td>
        <canvas id="sc7" width="187.5" height="318.75"></canvas>
      </td>
    </tr>
  </table>

</div>

我做错了什么?

1 个答案:

答案 0 :(得分:1)

使用setSrc()设置/更改图像来源。在setSrc和sendToBack里面,它不是触发对象:modified将canvas [0] json设置为其他画布。

master
var canvas = [],
  image;
canvas.push(new fabric.Canvas('c0'));
for (i = 1; i <= 7; i++) {
  canvas.push(new fabric.StaticCanvas('sc' + i));
}

function dropText() {
  var text = new fabric.IText('Type here...', {
    fontSize: 27,
    top: 10,
    left: 10,
  });
  canvas[0].add(text);
}

var rect = new fabric.Rect({
  fill: '#ff0000',
  width: 100,
  height: 100,
  id: 1
});
var circle = new fabric.Circle({
  fill: '#ffff00',
  radius: 50,
  left: 150,
  top: 150,
  originX: 'center',
  originY: 'center',
  id: 2
});

canvas[0].on('object:added', onModified);
canvas[0].on('object:modified', onModified);
canvas[0].on('object:scaling', onModified);
canvas[0].on('object:moving', onModified);
canvas[0].add(rect, circle);

function onModified(option) {
  var ob = option.target;
  var index = canvas[0].getObjects().indexOf(ob);
  ob.clone(function(obj) {
    for (i = 1; i <= 7; i++) {
      canvas[i].insertAt(obj, index, true);
    }
  });
};

$('#update').click(function() {
 updateCanvas();
});

function updateCanvas(){
  var json = JSON.stringify(canvas[0]);
  for (i = 1; i <= 7; i++) {
    canvas[i].loadFromJSON(json);
  }
}
// Toggling Images
function replaceImage(imgUrl) {
  if (!isImageLoaded) return; //return if initial image not loaded
  image.setSrc(imgUrl, function() {
   canvas[0].renderAll();
   updateCanvas();
 });
}

// Default (Blank)
fabric.Image.fromURL('https://i.imgur.com/SamdNdX.png', function(img) {
  isImageLoaded = true;
  image = img.set({
    selectable: false,
    evented: false,
  });
  canvas[0].add(image);
  canvas[0].sendToBack(image);
  updateCanvas();
});

$('#save').click(function() {
  html2canvas($('#imagesave'), {
    onrendered: function(canvas) {
      var a = document.createElement('a');
      // toDataURL defaults to png, so we need to request a jpeg, then convert for file download.
      a.href = canvas.toDataURL("image/jpeg").replace("image/jpeg", "image/octet-stream");
      a.download = 'myfile.jpg';
      a.click();
    }
  });
});
body {
  margin: 0px;
  padding: 0px;
}

canvas {
  border: 1px solid #f00;
  margin: 0px;
  display: block;
}

td {
  margin: 0;
  padding: 0;
  border: 0;
  outline: 0;
  vertical-align: baseline;
}

#imagesave {
  background-color: white;
  height: 637.5px;
  width: 825px;
  padding-left: 75px;
}