用纹理/图案填充图像

时间:2019-06-28 11:29:13

标签: design-patterns fabricjs

我尝试将其“翻译”成FabricJS代码,但是我做错了。是否可以在FabricJS中使用globalCompositeOperation?

Fill image with texture/pattern

我做错了什么?

(function() {
  var canvas = this.__canvas = new fabric.Canvas('c');
  fabric.Object.prototype.transparentCorners = false;

  var padding = 0;

  fabric.Image.fromURL('https://i.imgur.com/8WqH9v4.png', function(img) {

    img.scaleToWidth(100);
    var patternSourceCanvas = new fabric.StaticCanvas();
    patternSourceCanvas.add(img);
    patternSourceCanvas.renderAll();
    var pattern = new fabric.Pattern({
      source: function() {
        patternSourceCanvas.setDimensions({
          width: img.getScaledWidth() + padding,
          height: img.getScaledHeight() + padding
        });
        patternSourceCanvas.renderAll();
        return patternSourceCanvas.getElement();
      },
      repeat: 'no-repeat'
    });


    fabric.Image.fromURL('https://i.imgur.com/8WqH9v4.png', function(img) {
       fill: pattern
    });

  });
})();

0 个答案:

没有答案