【问题标题】:Lasso tool in html5 canvas - replacing the clipTo function with clipPathhtml5 画布中的套索工具 - 用 clipPath 替换 clipTo 函数
【发布时间】:2018-12-31 01:09:36
【问题描述】:

下面显示的代码来自this post。使用fabric.js/1.4.0 时它工作正常,但是当我将fabric.js 更新到2.4.3 时,它无法运行。问题是clipTo 函数已被一个名为clipPath 的新函数取代。有谁知道如何更新此代码以使其与 clipPath 一起使用?我已经浏览了 clipPath 的文档,并且已经在 Google 和 Stackoverflow 上搜索了将近 2 天,但我仍然迷路了。

var canvas = document.getElementById('c');
var ctx = canvas.getContext('2d');
var img = document.createElement('IMG');

img.onload = function() {
    var OwnCanv = new fabric.Canvas('c', {
        isDrawingMode: true
    });

    var imgInstance = new fabric.Image(img, {
        left: 0,
        top: 0,
    });
    OwnCanv.add(imgInstance);

    OwnCanv.freeDrawingBrush.color = "purple"
    OwnCanv.freeDrawingBrush.width = 4

    OwnCanv.on('path:created', function(options) {
        var path = options.path;
        OwnCanv.isDrawingMode = false;
        OwnCanv.remove(imgInstance);
        OwnCanv.remove(path);
        OwnCanv.clipTo = function(ctx) {
            path.render(ctx);
        };
        OwnCanv.add(imgInstance);
    });
}

img.src = "http://upload.wikimedia.org/wikipedia/commons/3/33/Jbassette4.jpg?uselang=fi";
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.1/jquery.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.4.0/fabric.min.js"></script>

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

【问题讨论】:

    标签: fabricjs


    【解决方案1】:

    好的,所以创建后的路径已经被缓存了。 您需要做的是使用“set”方法将填充设置为不为空的颜色。 然后将路径设置为画布的 clipPath 或 Object 本身。

    如果你想只为物体设置它,你需要重新计算它的正确位置。

    var canvas = document.getElementById('c');
    var ctx = canvas.getContext('2d');
    var img = document.createElement('IMG');
    
    img.onload = function() {
        var OwnCanv = new fabric.Canvas('c', {
            isDrawingMode: true
        });
    
        var imgInstance = new fabric.Image(img, {
            left: 0,
            top: 0,
        });
        OwnCanv.add(imgInstance);
        OwnCanv.controlsAboveOverlay = true;
        OwnCanv.freeDrawingBrush.color = "purple"
        OwnCanv.freeDrawingBrush.width = 4
    
        OwnCanv.on('path:created', function(options) {
            var path = options.path;
            path.set('fill', 'black');
            console.log(path)
            OwnCanv.isDrawingMode = false;
            OwnCanv.remove(path);
            OwnCanv.clipPath = path;
        });
    }
    
    img.src = "http://upload.wikimedia.org/wikipedia/commons/3/33/Jbassette4.jpg?uselang=fi";
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.1/jquery.min.js"></script>
    <script src="http://cdnjs.cloudflare.com/ajax/libs/fabric.js/2.4.6/fabric.min.js"></script>
    
    <canvas id="c" width="500" height="500"></canvas>

    【讨论】:

      【解决方案2】:

      您需要在路径上的false 上有objectCaching。 在这里查看:https://jsfiddle.net/jw6rLx5f/2/

      var canvas = document.getElementById('c');
      var ctx = canvas.getContext('2d');
      var img = document.createElement('IMG');
      
      img.onload = function() {
          var OwnCanv = new fabric.Canvas('c', {
              isDrawingMode: true
          });
      
          var imgInstance = new fabric.Image(img, {
              left: 0,
              top: 0,
          });
          OwnCanv.add(imgInstance);
      
          OwnCanv.freeDrawingBrush.color = "purple"
          OwnCanv.freeDrawingBrush.width = 4
      
          OwnCanv.on('path:created', function(options) {
              var path = options.path;
              path.objectCaching= false;
             
              OwnCanv.isDrawingMode = false;
              OwnCanv.remove(imgInstance);
              OwnCanv.remove(path);
              OwnCanv.clipTo = function(ctx) {
              	 ctx.save();
                  path.render(ctx);
                  ctx.restore();
              };
            
              OwnCanv.add(imgInstance);
          });
      }
      
      img.src = "http://upload.wikimedia.org/wikipedia/commons/3/33/Jbassette4.jpg?uselang=fi";
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/2.4.6/fabric.js"></script>
      <canvas id="c" width="500" height="500"></canvas>

      【讨论】:

        猜你喜欢
        • 2016-05-15
        • 2012-03-13
        • 2020-01-08
        • 2013-09-01
        • 2016-05-17
        • 2019-03-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多