DEV Community

Cover image for SVG essentials. Basic shapes and path
Julia Shlykova
Julia Shlykova

Posted on

SVG essentials. Basic shapes and path

In the previous article, we have seen the flower consisted of a circle at center and paths for petals. However, there are more shapes that can be drawn with SVG.

Table of contents

  1. Presentational attributes
  2. Circle
  3. Ellipse
  4. Rectangle
  5. Line
  6. Polyline
  7. Polygon
  8. Path

Presentational attributes

Before diving into shapes let's talk a bit about the style defined in the SVG element:

  • fill - background of the shape;
  • stroke - color of the shape border;
  • stroke-width - size of the shape border.

These presentation attributes are always overridden by styles applied with CSS rules;

Circle

Let's draw one more circle:

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
 <circle cx="50" cy="50" r="50" fill="green" />
</svg>
Enter fullscreen mode Exit fullscreen mode
  • cx and cy set the center of a circle;
  • r stands for radius.

green circle

Since the boundary of the SVG is 100x100 and the circle radius is 50, the circle will be at the center of SVG and its border at the SVG border.

Ellipse

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
 <ellipse cx="50" cy="50" rx="50" ry="25" fill="orange" />
</svg>
Enter fullscreen mode Exit fullscreen mode
  • cx and cy define the center of an ellipse;
  • rx sets the x-axis radius;
  • ry sets the y-axis radius;

orange ellipse

Rectangle

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
 <rect width="100" height="100" x="0" y="0" rx="50" ry="25" fill="lightblue" />
</svg>
Enter fullscreen mode Exit fullscreen mode
  • x and y set left top corner of the rectangle;
  • width and height define the width and height of the rectangle;
  • rx and ry make the rectangle rounded: if only either rx or ry is set, the other one takes the same value.

blue rounded rectangle

Line

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
 <line x1="25" y1="25" x2="75" y2="75" stroke="black" stroke-width="10" />
</svg>
Enter fullscreen mode Exit fullscreen mode
  • x1 and y1 are coordinates for the start of the line;
  • x2 and y2 are coordinates for the end of the line.

black line

Polyline

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
 <polyline
 fill="pink"
 stroke="black"
 stroke-width="10"
 points="10,10 50,80 90,10"
 />
</svg>
Enter fullscreen mode Exit fullscreen mode
  • points are a sequence of coordinates that connect straight line segments.

polyline

This element defines open shapes.

Polygon

In contrast to polyline, polygon always closes a shape.

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
  <polygon
   fill="lime"
   stroke="black"
   stroke-width="10"
   points="10,10 50,80 90,10"
 />
</svg>
Enter fullscreen mode Exit fullscreen mode
  • points are sequence of coordinates that connect straight line segments.

polygon

We can see the same set of points as in polyline, but polygon automatically closes the path.

Path

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
  <path
   fill="yellow"
   stroke="black"
   stroke-width="10"
   d="M10,10 V90 L50,50 L90,90 V10 Z"
 />
</svg>
Enter fullscreen mode Exit fullscreen mode
  • Mx,y stands for "Move To" and defines the starting point of the drawing with coordinates x y;
  • Lx,y stands for "Line To" and draws a line to the point with x y coordinates;
  • Hx draws a horizontal line;
  • Vy draws a vertical line;
  • Z closes a path.

path basic

We can certainly draw the same shape using either polyline or polygon. So, what's the difference between path and those shapes? Path also allows us to draw curves!

Cubic Bézier curve

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
  <path 
    fill="rosybrown" 
    stroke="black" 
    stroke-width="10" 
    d="M10,10 V90 C10,50 90,50 90,90 V10 Z" />
</svg>
Enter fullscreen mode Exit fullscreen mode
  • Cx1,y1 x2,y2 x,y stands for "Curve To" and draws a curve from the current point to the ending point (x,y), using (x1,y1) and (x2,y2) as control points.

Cubic Bézier curve basic

Cubic Bézier curve with labels

Empty circles indicate starting and ending points, while filled ones indicate control points.

Smooth continuation

We can create smooth shapes by relying on the current point:

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
  <path 
    fill="teal" 
    stroke="black" 
    stroke-width="10" 
    d="M10,50 C10,20 50,20 50,50 S90,80 90,50" />
</svg>
Enter fullscreen mode Exit fullscreen mode
  • Sx2,y2 x,y stands for "Smooth Curve To" and draws the curve by setting the reflection of the second control point on the previous command (either C or S) relative to the current point as (x1,y1) control point. It's better to see the example:

smooth Cubic Bézier

smooth Cubic Bézier with labels

The control point (50,80) (blue circle) was created under the hood, we haven't written it explicitely, it's the reflection of (50,20) point on the point (50,50) and represents the first control point for the second curve.

Quadratic Bézier curve

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
  <path 
    fill="coral" 
    stroke="black" 
    stroke-width="10" 
    d="M10,10 V80 Q50,40 90,80 V10 Z"
 />
</svg>
Enter fullscreen mode Exit fullscreen mode
  • Qx1,y1 x,y stands for "Quadratic Curve To" and draws a quadratic curve from the current point to the ending point (x,y), using (x1,y1) as a control point.

Quadratic Bézier curve

Quadratic Bézier curve with labels

Smooth continuation

Just like a cubic curve has a special command for smooth continuation, a quadratic curve has T command:

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
  <path 
    fill="cornflowerblue" 
    stroke="black" 
    stroke-width="10" 
    d="M10,50 Q30,10 50,50 T90,50"
 />
</svg>
Enter fullscreen mode Exit fullscreen mode
  • Tx,y draws a quadratic curve from the current point to the ending point (x,y), and the control point is the reflection of the control point on the previous command relative to the current point. Let's look at the example:

Smooth Quadratic Bézier curve

Smooth Quadratic Bézier curve with labels

The blue point was automatically calculated based on the previous control point and the current point.

Relative values

The path has an important feature: we can use lowercase to set relative values:

d="M10,10 v80 l40,-40 l40,40 v-80 Z"
Enter fullscreen mode Exit fullscreen mode

path basic

It's the same shape we've built using d="M10,10 V90 L50,50 L90,90 V10 Z" path.

Top comments (0)