使用svg作为NODE中的模式背景不起作用

时间:2016-08-14 12:21:53

标签: fabricjs

var src=__dirname + '/test.svg';
fabric.PathGroup.fromObject({paths:src,toBeParsed:true},function(pg) {
    var patternSourceCanvas = fabric.createCanvasForNode(pg.width, pg.height);
    patternSourceCanvas.add(pg);
    patternSourceCanvas.renderAll();
    var pcanvas= patternSourceCanvas.getElement();

    //uncomment these comments,you will get something
    /*var  pctx = pcanvas.getContext('2d')
    pctx.beginPath();
    pctx.rect(0,0,10,10);
    pctx.fillStyle='red';
    pctx.fill();
    pctx.beginPath();
    pctx.rect(10,10,10,10);
    pctx.fillStyle='blue';
    pctx.fill();
    */
    var pattern = new fabric.Pattern({
        source: pcanvas,
        repeat: 'repeat'
    });
    canvas.setBackgroundColor(pattern, function () { canvas.renderAll();
       fs.writeFile(__dirname + '/text333.png', canvas.nodeCanvas.toBuffer());
    });
})

已经加载并放入模式画布的svg没有渲染,所以我在后台没有任何东西 是一个错误还是什么?

1 个答案:

答案 0 :(得分:0)

var src=__dirname + '/test.svg';
fabric.PathGroup.fromObject({paths:src,toBeParsed:true},function(pg) {
    //changed
    var patternSourceCanvas = new fabric.StaticCanvas(null,{width:pg.width,height:pg.height});
    patternSourceCanvas.add(pg);
    patternSourceCanvas.renderAll();
    var pcanvas= patternSourceCanvas.getElement();


    var pattern = new fabric.Pattern({
        source: pcanvas,
        repeat: 'repeat'
    });
    canvas.setBackgroundColor(pattern, function () { canvas.renderAll();
       fs.writeFile(__dirname + '/text333.png', canvas.nodeCanvas.toBuffer());
    });
})

好吧,我自己修理