Paper.js套索选择工具

时间:2016-11-20 16:28:03

标签: javascript vector-graphics paperjs

是否有人为Adobe Illustart中的paperjs构建了一个徒手或套索选择工具?

我试图使用intersects(item),但这似乎只有在我的选择路径直接击中任何其他路径时才有效,而不是如果选择中有项目,即使我在{{之前收集它1}}。

onMouseUp看起来很有希望,但它只能检查是否有东西在矩形内而不是自由手道。

以下是一些例子:

isInside(rect)

2 个答案:

答案 0 :(得分:1)

布尔运算对我有用。它们不是几何测试,而是创建必须删除的额外项目,但它看起来是我能得到的最佳解决方案。 isEmpty()测试结果形状是否包含减法后的任何段。

var red = Path.Circle(new Point(180, 100), 20);
red.fillColor = "red";
red.name = "red";

var green = Path.Rectangle(new Point(150, 180), new Size(50, 50));
green.fillColor = "green";
green.name = "green";

var yellow = Path.Circle(new Point(90, 100), 20);
yellow.fillColor = "yellow";
yellow.name = "yellow";

var purple = Path.Rectangle(new Point(160, 190), new Size(30, 30));
purple.fillColor = "purple";
purple.name = "purple";

var selection = new Path([
    new Point(50, 50),
    new Point(50, 250),
    new Point(250, 250),
    new Point(250, 150),
    new Point(150, 150)
]);
selection.closed = true;
selection.strokeColor = "blue";
selection.fillColor = new Color(0, 0, 50, 0.2);


function isInside(_selection, _item) {

    var result = _item.subtract(_selection);
    var insideSelection = result.isEmpty();
    result.remove();

    return insideSelection;
}

function test(_item) {
    console.log(_item.name, isInside(selection, _item) ? " inside" : " outside");
}

test(red); // red  outside
test(green); // green  inside
test(yellow); // yellow  outside
test(purple); // purple  inside

答案 1 :(得分:0)

您可以使用jsclipper来交叉多边形。

This is the original Clipper libraryits documentation

PointInPolygon()功能可能会对您有所帮助。