How To Create Svg Group In Javascript?

1.14 JavaScript Examples Engineering LibreTexts

SVG, or Scalable Vector Graphics, is a type of format used to create interactive and high-resolution graphics for web and mobile applications. SVG elements are written in XML, and can be embedded directly into HTML pages. One of the most common SVG elements is group, which is a collection of other elements that can be manipulated as one. In this tutorial, we will look at how to create an SVG group in JavaScript.

What is SVG Group?

An SVG group is a collection of other elements that can be manipulated as one. This allows you to move, rotate, or scale all the elements in the group as if they were a single element. Grouping elements together can also help to keep your SVG code organized and maintainable. For example, you can group elements that are related to each other, such as a set of shapes that make up a single image.

How to Create an SVG Group in JavaScript?

Creating an SVG group in JavaScript is relatively straightforward. The first step is to create a new SVG group element. This can be done by calling the createElementNS() method on the document object and passing in the SVG namespace as the first argument. The second argument should be set to “g”, which is the tag name for the group element.

Once the group element is created, you can append other SVG elements to it. This can be done by calling the appendChild() method on the group element and passing in the element you want to append. For example, if you wanted to append a circle element to the group, you would call the appendChild() method and pass in the circle element.

Finally, you can add the group element to the SVG canvas. This can be done by calling the appendChild() method on the SVG canvas element and passing in the group element as the argument.

Example of SVG Group in JavaScript

Let’s take a look at a simple example of how to create an SVG group in JavaScript. In this example, we’ll create a group of three circles that we can move and rotate as one. The first step is to create the group element.

const group = document.createElementNS("http://www.w3.org/2000/svg", "g");

Next, we’ll create three separate circles and append them to the group element.

const circle1 = document.createElementNS("http://www.w3.org/2000/svg", "circle");
 circle1.setAttribute("cx", "50");
 circle1.setAttribute("cy", "50");
 circle1.setAttribute("r", "40");
 group.appendChild(circle1);
 
 const circle2 = document.createElementNS("http://www.w3.org/2000/svg", "circle");
 circle2.setAttribute("cx", "150");
 circle2.setAttribute("cy", "50");
 circle2.setAttribute("r", "40");
 group.appendChild(circle2);
 
 const circle3 = document.createElementNS("http://www.w3.org/2000/svg", "circle");
 circle3.setAttribute("cx", "100");
 circle3.setAttribute("cy", "100");
 circle3.setAttribute("r", "40");
 group.appendChild(circle3);

Finally, we’ll add the group element to the SVG canvas.

svg.appendChild(group);

Moving and Rotating the Group

Now that we have our group of circles, we can move and rotate them as one. To do this, we can use the transform attribute on the group element to manipulate its position and rotation. For example, if we wanted to move the group to the right by 50 pixels, we would set the transform attribute to translateX(50).

group.setAttribute("transform", "translateX(50)");

Similarly, if we wanted to rotate the group by 45 degrees, we would set the transform attribute to rotate(45).

group.setAttribute("transform", "rotate(45)");

This is just a simple example of how to create an SVG group in JavaScript. There are many more features and options available, such as the ability to add events to the group element and animate the elements inside the group.

Conclusion

In this tutorial, we looked at how to create an SVG group in JavaScript. We discussed the basics of SVG groups and how to create them using the createElementNS() and appendChild() methods. We also looked at an example of how to create a group of circles and how to move and rotate the group. Finally, we discussed some of the other features and options available with SVG groups.