Articles in this section
Category / Section

How to convert lines into a shape once drawing completed?

2 mins read

This article explains how to convert lines into shapes once the drawing is completed. The diagram component provides us with numerous features. The drawn lines (polyline or Bezier connectors) can be converted into a shape. This document explains how to convert the drawn lines into a shape. Initially, add N number of polylines or Bezier curves to the diagram. The line’s source point and target point play a vital role in converting the lines into a shape. The following code sample explains how the lines’ source point and target point are converted into a shape’s path.

    let path: string = 'M';
    for (let i: number = 0; i < diagram.selectedItems.connectors.length; i++) {
        if (i === 0) {
            if (diagram.selectedItems.connectors[i].type === 'Bezier') {
                path = path + String(diagram.selectedItems.connectors[i].sourcePoint.x) + ',' +
                    String(diagram.selectedItems.connectors[i].sourcePoint.y);
                path = path + ' C' + String((diagram.selectedItems.connectors[i].segments[0] as BezierSegmentModel).point1.x) + ',' +
                    String((diagram.selectedItems.connectors[i].segments[0] as BezierSegmentModel).point1.y) + ' ';
                path = path + String((diagram.selectedItems.connectors[i].segments[0] as BezierSegmentModel).point2.x) + ',' +
                    String((diagram.selectedItems.connectors[i].segments[0] as BezierSegmentModel).point2.y) + ' ';
                path = path + String(diagram.selectedItems.connectors[i].targetPoint.x) + ',' +
                    String(diagram.selectedItems.connectors[i].targetPoint.y);
            } else {
                let points: PointModel[] = (diagram.selectedItems.connectors[i] as Connector).intermediatePoints;
                path = path + String(points[0].x) + ',' + String(points[0].y);
                for (let j: number = 1; j < points.length; j++) {
                    path = path + ' L' + String(points[j].x) + ',' + String(points[j].y);
                }
            }
        } else {
            if (diagram.selectedItems.connectors[i].type === 'Bezier') {
                path = path + ' C' + String((diagram.selectedItems.connectors[i].segments[0] as BezierSegmentModel).point1.x) + ',' +
                    String((diagram.selectedItems.connectors[i].segments[0] as BezierSegmentModel).point1.y) + ' ';
                path = path + String((diagram.selectedItems.connectors[i].segments[0] as BezierSegmentModel).point2.x) + ',' +
                    String((diagram.selectedItems.connectors[i].segments[0] as BezierSegmentModel).point2.y) + ' ';
                path = path + String(diagram.selectedItems.connectors[i].targetPoint.x) + ',' +
                    String(diagram.selectedItems.connectors[i].targetPoint.y);
            } else {
                let points: PointModel[] = (diagram.selectedItems.connectors[i] as Connector).intermediatePoints;
                path = path + ' L' + String(points[0].x) + ',' + String(points[0].y);
                for (let j: number = 1; j < points.length; j++) {
                    path = path + ' L' + String(points[j].x) + ',' + String(points[j].y);
                }
            }
        }
    }

Initially, 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 are explained as follows,


M = moveto - Set a new current point.

C = curveto - Draw a curve.

L = lineto - Draw a straight line.

diagram.group();
    for (let j: number = 0; j < diagram.nodes.length; j++) {
        if (diagram.nodes[j].children) {
            offsetX = diagram.nodes[j].offsetX;
            offsetY = diagram.nodes[j].offsetY;
        }
    }
    diagram.remove();
    node = {id: 'shape', shape: { type: 'Path', data: path }, offsetY: offsetY, offsetX: offsetX };
    diagram.add(node);

After getting the path, the diagram is grouped. The offsetX and offsetY of the node with the children are used to place the converted shape. Finally, the lines drawn are added as a shape in the diagram.

Sample

https://stackblitz.com/edit/typescript-sapyad?file=index.html,index.ts


Conclusion

We hope you enjoyed learning about how to convert lines into a shape once the drawing is completed.

You can refer to our JavaScript Diagram feature tour page to learn about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications.  You can also explore our JavaScript 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 forumsBoldDesk Support, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied