可以覆盖fabricjs中的边界框选择区域 - controls选项

时间:2016-12-19 07:56:19

标签: javascript canvas html5-canvas custom-controls fabricjs

在这里,我们正在使用fabric.js创建设计工具。我们在fabric.js中为canvas对象创建自定义选择区域。我在fabric.js中读取了源代码,它为边界框生成了方框,但我希望更改为自定义。选择区域外观。有人可以告诉我吗?

HERE DANCING DOTS SELECTION AREA

我们想要自定义选择区域外观。

HERE FABRIC.JS DEFAULT SCRIPT

我们已尝试将此代码 context.setLineDash() 用于选择区域。

if (fabric.StaticCanvas.supports('setLineDash') === true) {
    ctx.setLineDash([4, 4]);
}

参考Github的代码。但是我的工作区域没有工作正常。

此处我们在结构功能

中添加了 Borderdasharray 属性创建
fabric.Object.prototype.set({  
        borderColor: 'green',  
        cornerColor: 'purple',  
        cornerSize: 33,
        transparentCorners: false,padding:4,
        borderDashArray:[50,25]          
    });

但我们需要在fabric.js。

中为该选区创建动画舞点/移动点

我们如何在fabric.js中创建自定义选择区域?

2 个答案:

答案 0 :(得分:4)

对于动画" borderDashArray"

文档:https://developer.mozilla.org/fr/docs/Web/API/CanvasRenderingContext2D/lineDashOffset

演示:https://jsfiddle.net/bp4u8tsr/

var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var offset = 0;

function draw() {
  ctx.clearRect(0,0, canvas.width, canvas.height);
  ctx.setLineDash([4, 2]);
  ctx.lineDashOffset = -offset;
  ctx.strokeRect(10,10, 100, 100);
}

function march() {
  offset++;
  if (offset > 16) {
    offset = 0;
  }
  draw();
  setTimeout(march, 20);
}

march();

在fabricjs中:object_interactivity.mixin.js并修改drawBorders函数,就像这个演示一样

更新:

fabric.Object.prototype.set({  
        borderColor: 'green',  
        cornerColor: 'purple',  
        cornerSize: 33,
        transparentCorners: false,padding:14,
        borderDashArray:[50,25]          
    });
// initialize fabric canvas and assign to global windows object for debug
var canvas = window._canvas = new fabric.Canvas('c',{width: 500, height: 500});
textbox = new fabric.Textbox('Hello, World!', {
  width: 200,
  height: 200,
  top: 80,
  left: 80,
  fontSize: 50,
  textAlign: 'center',
});
canvas.add(textbox);
textbox2 = new fabric.Textbox('Hello, World!', {
  width: 200,
  height: 200,
  top: 160,
  left: 160,
  fontSize: 50,
  textAlign: 'center',
});
canvas.add(textbox2);
canvas.renderAll();
canvas.setActiveObject(textbox);
var offset = 0;

  (function animate() {
    offset++;
    if (offset > 75) {
      offset = 0;
    }  
    canvas.contextContainer.lineDashOffset = -offset;
    canvas.renderAll();
    fabric.util.requestAnimFrame(animate);
  })();

  canvas.on('after:render', function() {
    canvas.contextContainer.strokeStyle = 'green';
        canvas.contextContainer.setLineDash([50,25]);
    canvas.forEachObject(function(obj) {
      var bound = obj.getBoundingRect();            
      canvas.contextContainer.strokeRect(
        bound.left + 0.5,
        bound.top + 0.5,
        bound.width,
        bound.height
      );
    })
  });

演示:https://jsfiddle.net/Da7SP/398/

答案 1 :(得分:3)

自定义:

  borderDashArray: Dash stroke of borders
  cornerDashArray: Dash stroke of corners
  cornerStrokeColor: If corners are filled, this property controls the color of the stroke
  cornerStyle: standrd 'rect' or 'circle'
  selectionBackgroundColor: add an opaque or transparent layer to the selected object.

使用此:

fabric.Object.prototype.set({
    transparentCorners: false,
    borderDashArray: ......