SVG contains the following elements to create basic shapes: RECT, CIRCLE, ELLIPSE, LINE, POLYLINE, POLYGON. We also have the PATH element with which we could create any shape including the previous ones already named. We will see how to create the different basic shapes with SVG and JavaScript. We have to take into account that, to create the shapes in JavaScript, we must use DOM ‘namespace-aware’ methods.
Rectangle
SVG
1 |
<rect x="5" y="5" width="50" height="50" fill="skyblue" stroke="black" stroke-width="2"/> |
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
; html-script: false ] <![CDATA[ var svgns = "http://www.w3.org/2000/svg"; function crearForma() { var forma = document.createElementNS(svgns, "rect"); forma.setAttributeNS(null, "x", 5); forma.setAttributeNS(null, "y", 5); forma.setAttributeNS(null, "width", 50); forma.setAttributeNS(null, "height", 50); forma.setAttributeNS(null, "fill", "skyblue"); forma.setAttributeNS(null, "stroke", "black"); forma.setAttributeNS(null, "stroke-width", "2"); svgDocument.documentElement.appendChild(forma); } ]]> |
Rounded rectangle
The rx and rv attributes are the radius of the ellipses’ axes, used to round the corners of the rectangle.
SVG
1 |
<rect x="5" y="5" width="50" height="50" rx="10" ry="10" fill="skyblue" stroke="black" stroke-width="2"/> |
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
; html-script: false ] <![CDATA[ var svgns = "http://www.w3.org/2000/svg"; function makeShape() { var shape = document.createElementNS(svgns, "rect"); shape.setAttributeNS(null, "x", 5); shape.setAttributeNS(null, "y", 5); shape.setAttributeNS(null, "rx", 10); shape.setAttributeNS(null, "ry", 10); shape.setAttributeNS(null, "width", 50); shape.setAttributeNS(null, "height", 50); shape.setAttributeNS(null, "fill", "skyblue"); shape.setAttributeNS(null, "stroke", "black"); shape.setAttributeNS(null, "stroke-width", "2"); svgDocument.documentElement.appendChild(shape); } ]]> |
Circle
Based on the center of the circle which defines its position, depending on the cx and cy attributes, and on the radius of the circle defined by the r attribute.
SVG
1 |
<circle cx="30" cy="30" r="25" fill="skyblue" stroke="black" stroke-width="2"/> |
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
; html-script: false ] <![CDATA[ var svgns = "http://www.w3.org/2000/svg"; function crearForma() { var forma = document.createElementNS(svgns, "circle"); forma.setAttributeNS(null, "cx", 30); forma.setAttributeNS(null, "cy", 30); forma.setAttributeNS(null, "r", 25); forma.setAttributeNS(null, "fill", "skyblue"); forma.setAttributeNS(null, "stroke", "black"); forma.setAttributeNS(null, "stroke-width", "2"); document.documentElement.appendChild(forma); } ]]> |
Ellipse
It is similar to the circle but with two radius, rx and ry.
SVG
1 |
<ellipse cx="55" cy="30" rx="50" ry="25" fill="skyblue" stroke="black" stroke-width="2"/> |
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
; html-script: false ] <![CDATA[ var svgns = "http://www.w3.org/2000/svg"; function crearForma() { var forma = document.createElementNS(svgns, "ellipse"); forma.setAttributeNS(null, "cx", 55); forma.setAttributeNS(null, "cy", 30); forma.setAttributeNS(null, "rx", 50); forma.setAttributeNS(null, "ry", 25); forma.setAttributeNS(null, "fill", "skyblue"); forma.setAttributeNS(null, "stroke", "black"); forma.setAttributeNS(null, "stroke-width", "2"); svgDocument.documentElement.appendChild(forma); } ]]> |
Line
Segment with a starting point and a final one.
SVG
1 |
<line x1="5" y1="5" x2="50" y2="50" stroke="black" stroke-width="2"/> |
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
; html-script: false ] <![CDATA[ var svgns = "http://www.w3.org/2000/svg"; function crearForma() { var forma = document.createElementNS(svgns, "line"); forma.setAttributeNS(null, "x1", 5); forma.setAttributeNS(null, "y1", 5); forma.setAttributeNS(null, "x2", 50); forma.setAttributeNS(null, "y2", 50); forma.setAttributeNS(null, "stroke", "black"); forma.setAttributeNS(null, "stroke-width", "2"); svgDocument.documentElement.appendChild(forma); } ]]> |
Polyline
It is a group of connected straight lines, which generally define open shapes.
SVG
1 |
<polyline points="5,5 45,45 5,45 45,5" fill="none" stroke="black" stroke-width="2"/> |
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 |
; html-script: false ] <![CDATA[ var svgns = "http://www.w3.org/2000/svg"; function crearForma() { forma = document.createElementNS(svgns, "polyline"); forma.setAttributeNS(null, "points", "5,5 45,45 5,45 45,5"); forma.setAttributeNS(null, "fill", "none"); forma.setAttributeNS(null, "stroke", "black"); forma.setAttributeNS(null, "stroke-width", "2"); svgDocument.documentElement.appendChild(forma); } ]]> |
Polygon
It is a group of connected straight lines, which define a close shape.
SVG
1 |
<polygon points="19,54 5,30 19,5 48,5 62,30 48,54" fill="skyblue" stroke="black" stroke-width="2"/> |
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 |
; html-script: false ] <![CDATA[ var svgns = "http://www.w3.org/2000/svg"; function crearForma() { var forma = document.createElementNS(svgns, "polygon"); forma.setAttributeNS(null, "points", "19,54 5,30 19,5 48,5 62,30 48,54"); forma.setAttributeNS(null, "fill", "skyblue"); forma.setAttributeNS(null, "stroke", "black"); forma.setAttributeNS(null, "stroke-width", "2"); svgDocument.documentElement.appendChild(forma); } ]]> |
We can also create shapes with programs as Illustrator or Inkscape. If we create these basic shapes from these programs, we will see that Illustrator uses the SVG basic shapes meanwhile Inkscape only uses RECT, but it convert the rest of shapes into PATH.