Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

SVG Basic Syntax

SVG Basic Syntax

An SVG document consists of one or more SVG elements, which define the content and attributes of the graphics.



  height="200"    

  xmlns="http://www.w3.org/2000/svg">   

  



Basic Syntax of SVG:

Drawing Basic Shapes:

SVG provides a series of graphic elements to draw shapes of various forms, such as rectangles, circles, straight lines, polygons, etc.

**Rectangles:** Use the element to draw rectangles, allowing you to specify attributes such as position, size, and rounded corners.





**Circles:** Use the element to draw circles, allowing you to specify the center coordinates and radius.





**Ellipses:** Use the element to draw ellipses, allowing you to specify the center coordinates and the radii of the major and minor axes.





**Lines:** Use the element to draw straight lines, requiring start and end point coordinates.





**Polygons:** Use the element to draw polygons, requiring the coordinates of multiple vertices.





**Polylines:** Use the element to draw polylines, requiring the coordinates of multiple points.





**Paths:** Use the element to draw paths; various shapes can be drawn by specifying a series of path commands.





Gradients and Fills:

Text and Fonts:

Animation and Interaction:

SVG Element Attributes:

SVG elements can have various attributes used to specify characteristics of the graphics such as position, size, color, etc.



  cy="100"       

  r="50"         

  fill="red"     

  stroke="black" 

  stroke-width="2" 

/>

Nesting and Grouping:

SVG elements can be nested and grouped to better organize and manage graphic elements.

   

  

  

  




Simple SVG Example

SVG files are recommended to use .svg (all lowercase) as the extension for such files.

1. The following is a simple SVG file example that creates an SVG image containing a circle:

test.svg file

 

This code describes an SVG canvas with a width of 200 units and a height of 200 units. A circle is drawn on the canvas with center coordinates at (100, 100), a radius of 80 units, and a blue fill color.

Preview:

2. Another simple SVG graphic example:

test.svg file

  baseProfile="full"

  width="300" height="200"

  xmlns="http://www.w3.org/2000/svg">

 

 

 

RUNOOB SVG TEST

Try it »

The display result is as follows:

RUNOOB SVG TEST

SVG Code Analysis:

  • : This is the start tag of the SVG document, specifying the SVG version, basic configuration (full), and the width and height of the canvas. The xmlns attribute defines the XML namespace of the SVG document.

  • : This is a rectangle element, using thetag. Its width and height are both set to the canvas's width and height (100%); the stroke attribute specifies the rectangle's border color as red, the stroke-width attribute sets the border width to 4 units, and the fill attribute specifies the rectangle's fill color as yellow.

  • : This is a circle element, using thetag. Its center coordinates (cx, cy) are (150, 100), the radius r is 80 units, and the fill attribute specifies the circle's fill color as green.

  • RUNOOB SVG TEST: This is a text element, using thetag. Its starting coordinates (x, y) are (150, 115), the font-size attribute sets the font size to 16 units, the text-anchor attribute sets the horizontal alignment of the text to centered, and the fill attribute specifies the text color as white. The text content is "RUNOOB SVG TEST".

  • : This is the end tag of the SVG document, marking the end of the SVG document.