После того, как я нарисую полигон, я хотел бы изменить непрозрачность холста из полигона, который я только что нарисовал. Я думаю, что это может быть связано с отсечением, но я не уверен. Полигон и линии внутри должны иметь непрозрачность 100%, но все остальное (фон и линии вне полигона должны иметь непрозрачность 50%)
prototypefabric.polygon = {
drawPolygon: function () {
canvas.on('mouse:down', function (options) {
if (options.target && options.target.id == pointArray[0].id) {
prototypefabric.polygon.generatePolygon(pointArray);
}
if (polygonMode) {
prototypefabric.polygon.addPoint(options);
}
});
canvas.on('mouse:up', function (options) {
});
canvas.on('mouse:move', function (options) {
if (activeLine && activeLine.class == "line") {
var pointer = canvas.getPointer(options.e);
activeLine.set({x2: pointer.x, y2: pointer.y});
var points = activeShape.get("points");
points[pointArray.length] = {
x: pointer.x,
y: pointer.y
};
activeShape.set({
points: points
});
canvas.renderAll();
}
canvas.renderAll();
});
polygonMode = true;
pointArray = new Array();
lineArray = new Array();
activeLine;
},
addPoint: function (options) {
var random = Math.floor(Math.random() * (max - min + 1)) + min;
var id = new Date().getTime() + random;
var circle = new fabric.Circle({
radius: 5,
fill: '#ffffff',
stroke: '#333333',
strokeWidth: 0.5,
left: (options.e.layerX / canvas.getZoom()),
top: (options.e.layerY / canvas.getZoom()),
selectable: false,
hasBorders: false,
hasControls: false,
originX: 'center',
originY: 'center',
id: id
});
if (pointArray.length == 0) {
circle.set({
fill: '#BB2026'
})
}
var points = [(options.e.layerX / canvas.getZoom()), (options.e.layerY / canvas.getZoom()), (options.e.layerX / canvas.getZoom()), (options.e.layerY / canvas.getZoom())];
line = new fabric.Line(points, {
strokeWidth: 2,
fill: '#999999',
stroke: 'green',
class: 'line',
originX: 'center',
originY: 'center',
selectable: false,
hasBorders: false,
hasControls: false,
evented: false
});
if (activeShape) {
var pos = canvas.getPointer(options.e);
var points = activeShape.get("points");
points.push({
x: pos.x,
y: pos.y
});
var polygon = new fabric.Polygon(points, {
stroke: '#333333',
strokeWidth: 1,
fill: '#cccccc',
opacity: 0.3,
selectable: false,
hasBorders: false,
hasControls: false,
evented: false,
});
canvas.remove(activeShape);
canvas.add(polygon);
activeShape = polygon;
canvas.renderAll();
console.log(points);
} else {
var polyPoint = [{x: (options.e.layerX / canvas.getZoom()), y: (options.e.layerY / canvas.getZoom())}];
var polygon = new fabric.Polygon(polyPoint, {
stroke: '#333333',
strokeWidth: 1,
fill: '#cccccc',
opacity: 0.3,
selectable: false,
hasBorders: false,
hasControls: false,
evented: false,
});
activeShape = polygon;
canvas.add(polygon);
}
activeLine = line;
pointArray.push(circle);
lineArray.push(line);
canvas.add(line);
canvas.add(circle);
canvas.selection = false;
},
generatePolygon: function (pointArray) {
var points = new Array();
$.each(pointArray, function (index, point) {
points.push({
x: point.left,
y: point.top,
});
canvas.remove(point);
});
$.each(lineArray, function (index, line) {
canvas.remove(line);
});
canvas.remove(activeShape).remove(activeLine);
var polygon = new fabric.Polygon(points, {
stroke: '#333333',
strokeWidth: 0.5,
fill: 'rgb(255, 255, 255, 0)',
hasBorders: false,
hasControls: false,
id: 'plotOutline'
});
canvas.add(polygon);
var overlayRect = new fabric.Rect({
width: canvas.get("width"),
height: canvas.get("height"),
selectable: false,
fill: "rgb(255, 255, 255, 0.5)"
});
var clippingPolygon = new fabric.Polygon(polygon.points, {
left: -(overlayRect.get("width") / 2) - polygon.get("left") * -1,
top: -(overlayRect.get("height") / 2) - polygon.get("top") * -1
});
var clipGroup = new fabric.Group([clippingPolygon], {
inverted: true
});
overlayRect.clipPath = clipGroup;
canvas.add(overlayRect);
console.log(JSON.stringify(polygon.points) + 'points');
if (polygon.id == 'plotOutline') {
$('#saveStepOne').prop('disabled', false);
}
activeLine = null;
activeShape = null;
polygonMode = false;
canvas.selection = true;
}
};
рисование полигона