Path: blob/main/files/en-us/web/svg/tutorial/basic_shapes/index.md
6552 views
------{{ PreviousNext("Web/SVG/Tutorial/Positions", "Web/SVG/Tutorial/Paths") }}
There are several basic shapes used for most SVG drawing. The purpose of these shapes is fairly obvious from their names. Some of the parameters that determine their position and size are given, but an element reference would probably contain more accurate and complete descriptions along with other properties that won't be covered in here. However, since they're used in most SVG documents, it's necessary to give them some sort of introduction.
To insert a shape, you create an element in the document. Different elements correspond to different shapes and take different parameters to describe the size and position of those shapes. Some are slightly redundant in that they can be created by other shapes, but they're all there for your convenience and to keep your SVG documents as short and as readable as possible. All the basic shapes are shown in the following image.

The code to generate that image looks something like this:
Note: The
stroke,stroke-width, andfillattributes are explained later in the tutorial.
Rectangle
The {{SVGElement("rect")}} element draws a rectangle on the screen. There are six basic attributes that control the position and shape of the rectangles on screen. The one on the right has its rx and ry parameters set, giving it rounded corners. If they're not set, they default to 0.
x: The x position of the top left corner of the rectangle.
y: The y position of the top left corner of the rectangle.
width: The width of the rectangle.
height: The height of the rectangle.
rx: The x radius of the corners of the rectangle.
ry: The y radius of the corners of the rectangle.
Circle
The {{SVGElement("circle")}} element draws a circle on the screen. It takes three basic parameters to determine the shape and size of the element.
r: The radius of the circle.
cx: The x position of the center of the circle.
cy: The y position of the center of the circle.
Ellipse
An {{SVGElement("ellipse")}} is a more general form of the {{SVGElement("circle")}} element, where you can scale the x and y radius (commonly referred to as the semimajor and semiminor axes in maths) of the circle separately.
rx: The x radius of the ellipse.
ry: The y radius of the ellipse.
cx: The x position of the center of the ellipse.
cy: The y position of the center of the ellipse.
Line
The {{SVGElement("line")}} element takes the positions of two points as parameters and draws a straight line between them.
x1: The x position of point 1.
y1: The y position of point 1.
x2: The x position of point 2.
y2: The y position of point 2.
Polyline
A {{SVGElement("polyline")}} is a group of connected straight lines. Since the list of points can get quite long, all the points are included in one attribute:
points: A list of points. Each number must be separated by a space, comma, EOL, or a line feed character with additional whitespace permitted. Each point must contain two numbers: an x coordinate and a y coordinate. So, the list
(0,0),(1,1), and(2,2)could be written as0, 0 1, 1 2, 2.
Polygon
A {{SVGElement("polygon")}} is similar to a {{SVGElement("polyline")}}, in that it is composed of straight line segments connecting a list of points. For polygons though, the path automatically connects the last point with the first, creating a closed shape.
Note: A rectangle is a type of polygon, so a polygon can be used to create a
<rect/>element that does not have rounded corners.
points: A list of points, each number separated by a space, comma, EOL, or a line feed character with additional whitespace permitted. Each point must contain two numbers: an x coordinate and a y coordinate. So, the list
(0,0),(1,1), and(2,2)could be written as0, 0 1, 1 2, 2. The drawing then closes the path, so a final straight line would be drawn from(2,2)to(0,0).
Path
A {{SVGElement("path")}} is the most general shape that can be used in SVG. Using a path element, you can draw rectangles (with or without rounded corners), circles, ellipses, polylines, and polygons. Basically any of the other types of shapes, bezier curves, quadratic curves, and many more.
For this reason, the next section in this tutorial will be focused on paths. But for now, note that there is a single parameter used to control its shape.
d: A list of points and other information about how to draw the path. See the Paths section for more information.
{{ PreviousNext("Web/SVG/Tutorial/Positions", "Web/SVG/Tutorial/Paths") }}