How to convert lines into a shape once drawing completed in React Diagram?
In React Diagram we can convert the drawn lines into shape by using the group feature. The drawn lines(polyline or Bezier connectors) can be converted into a shape. Initially add N number of polylines or Bezier curves to the diagram. The line’s source point and target point plays a vital role in converting the lines to shape. The following code sample explains how the lines’ source point and target point is converted into a shape’s path.
let node;
let offsetX;
let offsetY;
let path = 'M';
for (let i = 0; i <diagramInstance.selectedItems.connectors.length; i++) {
if (i === 0) {
if (diagramInstance.selectedItems.connectors[i].type === 'Bezier') {
path = path + String(diagramInstance.selectedItems.connectors[i].sourcePoint.x) + ',' +
String(diagramInstance.selectedItems.connectors[i].sourcePoint.y);
path = path + ' C' + String((diagramInstance.selectedItems.connectors[i].segments[0] ).point1.x) + ',' +
String((diagramInstance.selectedItems.connectors[i].segments[0] ).point1.y) + ' ';
path = path + String((diagramInstance.selectedItems.connectors[i].segments[0] ).point2.x) + ',' +
String((diagramInstance.selectedItems.connectors[i].segments[0] ).point2.y) + ' ';
path = path + String(diagramInstance.selectedItems.connectors[i].targetPoint.x) + ',' +
String(diagramInstance.selectedItems.connectors[i].targetPoint.y);
} else {
let points = (diagramInstance.selectedItems.connectors[i]).intermediatePoints;
path = path + String(points[0].x) + ',' + String(points[0].y);
for (let j = 1; j < points.length; j++) {
path = path + ' L' + String(points[j].x) + ',' + String(points[j].y);
}
}
} else {
if (diagramInstance.selectedItems.connectors[i].type === 'Bezier') {
path = path + ' C' + String((diagramInstance.selectedItems.connectors[i].segments[0] ).point1.x) + ',' +
String((diagramInstance.selectedItems.connectors[i].segments[0] ).point1.y) + ' ';
path = path + String((diagramInstance.selectedItems.connectors[i].segments[0] ).point2.x) + ',' +
String((diagramInstance.selectedItems.connectors[i].segments[0] ).point2.y) + ' ';
path = path + String(diagramInstance.selectedItems.connectors[i].targetPoint.x) + ',' +
String(diagramInstance.selectedItems.connectors[i].targetPoint.y);
} else {
let points = (diagramInstance.selectedItems.connectors[i]).intermediatePoints;
path = path + ' L' + String(points[0].x) + ',' + String(points[0].y);
for (let j = 1; j < points.length; j++) {
path = path + ' L' + String(points[j].x) + ',' + String(points[j].y);
}
}
}
}
Then a variable path is declared, which is used to store the path data of the lines drawn. This path data is converted into a shape. The path data draws an SVG shape. The commands used to draw the shape in the above code sample is explained as follows,
M = moveto - Set a new current point.
C = curveto - Draw a curve.
L = lineto - Draw a straight line.
diagramInstance.group();
for (let j = 0; j <diagramInstance.nodes.length; j++) {
if (diagramInstance.nodes[j].children) {
offsetX =diagramInstance.nodes[j].offsetX;
offsetY =diagramInstance.nodes[j].offsetY;
}
}
After getting path the diagram is grouped. The offsetX and offsetY of the node with the children are used to place the converted shape, then remove the drawn lines Finally, the lines drawn are added as a shape in the diagram.
diagramInstance.remove();
node = { id: 'shape', shape: { type: 'Path', data: path }, offsetY: offsetY, offsetX: offsetX };
diagramInstance.add(node);
diagramInstance.dataBind();
Refer the working sample for reference
Conclusion
I hope you enjoyed learning about how to convert lines into a shape once drawing completed in React Diagram.
You can refer to our React Diagram feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our React Diagram example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!