Skip to main content

Archive

Show more

Paths and Curves in SVG

Paths and Curves in SVG

In SVG (Scalable Vector Graphics), paths are used to draw complex shapes and curves by defining a series of points and commands. Paths can contain straight lines, curves, arcs, and other shapes, allowing for the creation of intricate and custom graphics. Here's how to work with paths and curves in SVG:


1. Path Element

The <path> element in SVG is used to define a path by specifying a series of commands and coordinates. Each command represents a drawing operation such as moving to a point, drawing a line, or drawing a curve.

Example:

<svg width="200" height="200">
  <path d="M10 80 C 40 10, 65 10, 95 80 S 150 150, 180 80" fill="none" stroke="black" stroke-width="3" />
</svg>

In this example, the path commands M, C, and S are used to create a cubic Bézier curve.


2. Straight Lines

To draw straight lines in SVG, use the L command followed by the coordinates of the line's end point.

Example:

<svg width="200" height="200">
  <path d="M10 10 L 100 100" fill="none" stroke="black" stroke-width="2" />
</svg>

This example draws a straight line from (10,10) to (100,100).


3. Curves

SVG supports various types of curves, including quadratic Bézier curves (Q command) and cubic Bézier curves (C command), which allow for smooth and curved paths.

Example:

<svg width="200" height="200">
  <path d="M10 80 Q 52.5 10, 95 80 T 180 80" fill="none" stroke="black" stroke-width="3" />
</svg>

This example draws a quadratic Bézier curve (Q) from (10,80) to (95,80), followed by a smooth curve (T) to (180,80).


4. Arcs

Arcs in SVG are defined using the A command, which allows you to draw elliptical arcs based on radius, rotation, and other parameters.

Example:

<svg width="200" height="200">
  <path d="M10 80 A 45 45, 0, 0, 0, 180 80" fill="none" stroke="black" stroke-width="3" />
</svg>

This example draws an elliptical arc from (10,80) to (180,80) with a radius of 45.


5. Conclusion

SVG paths and curves provide a powerful mechanism for creating complex and custom graphics in web development. By using path commands like M, L, Q, C, and A, you can draw a wide range of shapes and curves to enhance the visual appeal of your SVG graphics.

Comments