BringToFront和intersectsWithObject使用问题

时间:2015-11-15 12:14:38

标签: javascript fabricjs

我正在尝试使用Fabricjs绘制html页面,但我是javascript的新手。我有两个问题,第一个是当我点击它时我想在顶部带一个对象,但经过一些测试后,绘图顺序没有改变。 第二个连接到对象之间的交叉,我需要它来制作橡皮擦,当它与另一个对象相交时,它会删除它(现在只是尝试改变不透明度) 我想我做了一些语法错误。 ps当加载页面时,initF()函数运行一次

//canvas di default
var myCanvas;

//layer interposto fra myCanvas e le figure disegnate
var canvF;

//strutture dati per tenere traccia in modo distinti delle varie figure
var rettangoli;
var cerchi;

//flag
var drawing;
var circleOn;
var erase;
var gomma;

//generatore casuale di colore
function getRandomColor() {
    var letters = '0123456789ABCDEF'.split('');
    var rand_color = '#';
    for (var i = 0; i < 6; i++) {
        rand_color += letters[Math.floor(Math.random() * 16)];
    }
    return rand_color;
}

//inizializza il container e il canvas
function initF() {
    erase = false;
    drawing = false;
    circleOn = false;
    canvasContainer = document.createElement('div');
    document.body.appendChild(canvasContainer);
    canvasContainer.style.position = "absolute";
    canvasContainer.style.left = "0px";
    canvasContainer.style.top = "0px";
    canvasContainer.style.width = "100%";
    canvasContainer.style.height = "100%";
    canvasContainer.style.zIndex = "1000";
    superContainer = document.body;
    myCanvas = document.createElement('canvas');
    myCanvas.style.width = superContainer.scrollWidth + "px";
    myCanvas.style.height = superContainer.scrollHeight + "px";
    myCanvas.width = superContainer.scrollWidth;
    myCanvas.height = superContainer.scrollHeight;
    myCanvas.style.overflow = 'visible';
    myCanvas.style.position = 'absolute';
    canvasContainer.appendChild(myCanvas);
    canvF = new fabric.Canvas(myCanvas);
    rettangoli = new fabric.Canvas(myCanvas);
}

function drawRectF() {
    canvF.isDrawingMode = false;
    var rect = new fabric.Rect();
    rect.left = 100;
    rect.top = 100;
    var coloreRect = getRandomColor();
    rect.fill= coloreRect;
    rect.width = 80;
    rect.height = 80;
    rect.borderColor = 'red';
    rect.cornerColor = 'green';
    rect.cornerSize = 20;
    transparentCorners = false;
    rettangoli.add(rect);
    canvF.add(rect);

}

function animaF() {

        rettangoli.forEachObject(function (obj) {
                    obj.animate('top', myCanvas.height-obj.height, {
                        onChange: canvF.renderAll.bind(canvF),
                        duration: 2000,
                        easing: fabric.util.ease.easeOutBounce
                    });
        });

}

function drawCircleF() {
    canvF.isDrawingMode = false;
    var circle = new fabric.Circle({ radius: 30, fill: getRandomColor(), top: myCanvas.height / 2, left: myCanvas.width / 2 });
    circle.borderColor = 'red';
    circle.cornerColor = 'green';
    circle.cornerSize = 20;
    transparentCorners = false;
    canvF.add(circle);
    circle.on({'mousedown': canvF.bringToFront(circle)})
}

function textF() {
    var comicSansText = new fabric.Text("Ciao", { fontFamily: 'Comic Sans' });
    comicSansText.textBackgroundColor = red;
    comicSansText.left = 100;
    comicSansText.top = 100;
    comicSansText.width = 100;
    comicSansText.height = 100;
    canvF.add(comicSansText);
}

function EraseF() {
    erase = true;
    gomma = new fabric.Rect();
    rect.left = myCanvas.width/2;
    rect.top = 100;
    var coloreRect = getRandomColor();
    rect.fill = coloreRect;
    rect.width = 100;
    rect.height = 50;
    rect.borderColor = 'red';
    rect.cornerColor = 'transparent';
    rect.cornerSize = 10;
    transparentCorners = false;
    rect.hasBorders = false;
    perPixelTargetFind = true;
    canvF.add(rect);
    gomma.on({
        'moving': onChange,
        'scaling': onChange,
        'rotating': onChange,
    });
}

function onChange(options) {
    options.target.setCoords();
    canvF.forEachObject(function(obj) {
        if (obj === options.target) return;
        obj.setOpacity(options.target.intersectsWithObject(obj) ? 0.5 : 1);
    });
}



function drawLineF() {
    canvF.isDrawingMode = true;
    freeDrawingBrush.color = getRandomColor();
    freeDrawingBrush.width = 10;
    perPixelTargetFind = true;
}

1 个答案:

答案 0 :(得分:0)

在这里,您可以找到如何使用前景以及如何删除交叉点的示例。

如您所见,我们将canvas.bringToFron的逻辑放在每个对象选择事件中。

对于“橡皮擦”,逻辑略有不同。 对于每次移动或缩放,我们更新橡皮擦坐标(setCoords,这很重要),然后我们为画布上的每个对象循环,我们检查一些是否与橡皮擦的布线盒相交,然后,如果有正面匹配,我们删除对象。

<item name="android:textColorPrimary">@color/text_primary</item>
  var canvas = new fabric.Canvas('canvas');
  var onChange = function(options) {
if (!options.target.eraser) {
  return;
}
options.target.setCoords();
canvas.forEachObject(function(obj) {
  if (!obj.eraser && options.target.intersectsWithObject(obj)) {
    canvas.remove(obj);
  }
});
  }

$(document).ready(function() {
    canvas.add(new fabric.Rect({left: 50, width: 100, height: 50, fill: 'red'}));
    canvas.add(new fabric.Rect({left: 70, width: 100, height: 50, fill: 'blue'}));
    canvas.add(new fabric.Rect({left: 90, width: 100, height: 50, fill: 'green'}));
    var circle = new fabric.Circle({top: 100, left: 300, radius: 10, fill: 'purple', eraser: true});
    canvas.add(circle);
    canvas.on('object:selected', function(options) {
       canvas.bringToFront(options.target);
    });
    canvas.on('object:scaling', onChange);
    canvas.on('object:moving', onChange);

});