SVG Transformation

The SVG transformation can be defined as to mutate the single SVG shape element or the set of SVG elements. The SVG transformation upholds Rotate, Translate, Scale, and Skew (Shear). SVG also contains an attribute called “Transform,” which supports the transformation.

An SVG transformation may consist of the following functions. They are-

Rotate

The rotate holds the three options, such as angle, the angle of rotation, cx that is the center of rotation for the x-axis,and cy is the center of rotation for the y-axis. If you don’t declare the cx and cy, then by default, it takes the existing origin of the coordinate system. For Example– rotate (90)

Translate

The translate contains two options, i.e., tx, the translation along with the x-axis, and ty is the translation along with the y-axis. For Example- translate (60, 60).

Scale

The scale function consists of two options- sx and sy. The sx defines the scaling factor along with the x-axis, and sy determines the scaling factor along with the y-axis. If you don’t declare the sy, then it will take the value of sx by default. It is optional to use. For Example- scale (30).

Skew

This function of transformation is also known as a Shear function. It only has a single option- skew-angle, is an angle along with the x-axis for the skewX andthe angle along with the y-axis for the skewY. For Example- skewX (30).

Example

Here, we have an example of SVG rectangle with translate, shown in below code-

Output

After the successful execution of the above code, you will get the following result:

You can declare the multiple transformation for the single SVG element with the help of space used for separation. If you declare more than one value, then the transformation can be applied to the values in a sequential order (One by one). It is shown in the below code-

Output

After the execution of the above example, you will get the following output: You can also use the transformation for the set of shapes elements as shown in below code-

Output

After the execution of the above example, you will get the following output:

Here, we have another example to elaborate the scaling and rotation with the help of transformation. It is shown in the below steps-

Step 1: First you need to create an SVG graphics with 400 width and 400 height.

Step 2: Now, you are required to create a set of SVG.

Step 3: Now, draw a rectangle with 80 length and 60 height and fill this shape with black color as shown in below code:

Step 4: Now, you need to draw a circle of 20 radius and fill it with the yellow color. It is given in below code:

Step 5: Now, add the transform attribute and also attach the translate and rotate function. It is shown in below code:

Step 6: After executing the previous steps, you need to create a html file named svg_transform.html and accommodate all the above given SVG codes shown below:

Output

After the execution of the above example, you will get the following output:

### Transformation with D3.js

You can create an SVG image or graphic with the help of D3.js by following the below given steps:

Step 1: First, create a “svgcontainer” to store the SVG image as shown in below code:

Step 2: In this step, you are required to draw a SVG image or graphic as explained in below code:

Step 3: Now, you need an SVG group element and declare all the attributes of translate and rotate. It is defined in the below code:

Step 4: In this step, you will create an SVG shape (rectangle) and affix it within the SVG element group as determined below:

Step 5: Now, you are required to draw anther shape (circle) and affix it within the SVG group element as shown in below code:

Example

Here, we have discussed an example to elaborate the above concept:

Output

After the successful execution of the above example code, you will get the following output:

Transform Library

D3.js library helps you to manage the transformation without creating the transform of the attributes randomly. The transform library facilitates you to manage all types of transformations. Transform library contains some methods such as scale(), rotate(), translate(), transform(), etc. You can also attach the D3-transform in your html webpage with the help of following given code: