将FabricJS画布对象组的克隆添加到另一个画布时,它们将被隐藏

时间:2018-11-03 02:38:44

标签: javascript svg canvas fabricjs

我尝试将一组对象从画布导出到svg。为此,我将组克隆到另一个画布中(该组的高度/宽度相同)。但是问题是该组隐藏在原始画布中,我不知道为什么。

代码如下:

    this.__canvas = new fabric.Canvas('meCanvas', {
        preserveObjectStacking: true,
        height: 300,
        width: 200,
        backgroundColor: '#1F1F1F',
        canvasKey:'azpoazpoaz'
    });
    let newID = (new Date()).getTime().toString().substr(5);
    let rect = new fabric.Rect({
        fill: 'red',
        width: 48,
        height: 32,
        left: 100,
        top: 100,
        originX: 'center',
        originY: 'center',
        fontWeight: 'normal',
        myid: newID
    });

    let newID1 = (new Date()).getTime().toString().substr(5);
    let text = new fabric.IText('Text', {
        fontFamily: 'Times',
        fontSize: 18,
        fill: 'white',
        left: 100,
        top: 100,
        originX: 'center',
        originY: 'center',
        fontWeight: 'normal',
        myid: newID1,
        objecttype: 'text'
    });

    this.__canvas.add(rect);
    this.__canvas.add(text);
    this.__canvas.renderAll();

    $('#generate').click((e)=>{

        let obj = this.__canvas.getActiveObject();
        if(!obj) return;

        let obj1 = $.extend(true, {}, obj);
        this.tempCanvas = new fabric.Canvas('tempCanvas', {
            canvasKey:'efsdfsd',
            preserveObjectStacking: true,
            height: obj1.getScaledHeight(),
            width: obj1.getScaledWidth()
        });
        obj1.left= 0;
        obj1.top=0;
        this.tempCanvas.add(obj1);
        let mySVG = this.tempCanvas.toSVG();
        //console.log(this.tempCanvas.toSVG());
    });
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/2.4.3/fabric.js"></script>
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>

    <div style='display: inline-block'>
        <div>
            <canvas id='meCanvas' ref='meFabric'/>
        </div>
        <div>
            <button id='generate'>Generate the SVG</button>
        </div>
        <div style='display: inline-block'>
            <div id="rect"></div>
            <div id="recttext"></div>
        </div>
    </div>

如果同时选择文本和红色矩形,然后单击“生成SVG”按钮,然后再次单击画布,则该组将消失。而且我不知道为什么。

请如何解决?

1 个答案:

答案 0 :(得分:1)

要复制对象,请使用obj.clone()而不是$.extend

示例

this.__canvas = new fabric.Canvas('meCanvas', {
  preserveObjectStacking: true,
  height: 300,
  width: 200,
  backgroundColor: '#1F1F1F',
  canvasKey: 'azpoazpoaz'
});
let newID = (new Date()).getTime().toString().substr(5);
let rect = new fabric.Rect({
  fill: 'red',
  width: 48,
  height: 32,
  left: 100,
  top: 100,
  originX: 'center',
  originY: 'center',
  fontWeight: 'normal',
  myid: newID
});

let newID1 = (new Date()).getTime().toString().substr(5);
let text = new fabric.IText('Text', {
  fontFamily: 'Times',
  fontSize: 18,
  fill: 'white',
  left: 100,
  top: 100,
  originX: 'center',
  originY: 'center',
  fontWeight: 'normal',
  myid: newID1,
  objecttype: 'text'
});

this.__canvas.add(rect);
this.__canvas.add(text);
this.__canvas.renderAll();

$('#generate').click((e) => {

  let obj = this.__canvas.getActiveObject();
  if (!obj) return;
  obj.clone(function(clonedObj) {
    let obj1 = clonedObj;
    this.tempCanvas = new fabric.Canvas('tempCanvas', {
      canvasKey: 'efsdfsd',
      preserveObjectStacking: true,
      height: obj.getScaledHeight(),
      width: obj.getScaledWidth()
    });
    obj1.left = 0;
    obj1.top = 0;
    this.tempCanvas.add(obj1);
    let mySVG = this.tempCanvas.toSVG();
  })


  //console.log(this.tempCanvas.toSVG());
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/2.4.3/fabric.js"></script>
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>

<div style='display: inline-block'>
    <div>
        <canvas id='meCanvas' ref='meFabric'/>
    </div>
    <div>
        <button id='generate'>Generate the SVG</button>
    </div>
    <div style='display: inline-block'>
        <div id="rect"></div>
        <div id="recttext"></div>
    </div>
</div>