HTML5 SVG
HTML5 SVG
UNIT-3
What is SVG in HTML5?
The full form of SVG is Scalable Vector Graphics
HTML5 SVG is an alternative to Canvas.
SVG helps in creating 2 Dimensional vector graphics for the website.
SVG is mostly useful for vector type diagrams like Pie charts, Two-dimensional graphs in an X,Y coordinate
system etc.
It depicts the image or its related object in XML format which is like a special text format.
Vector Graphics are made using line, points, and arc by using some mathematical formula. It is also a W3C
recommendation.
Along with SVG, the other file formats which use Vector Graphics are PDF and EPS.
On the other hand, Images like PNG, GIF and JPEG uses Raster Graphics. Raster Graphic is also called as
Bitmap Image, and they use pixels(set of dots), unlike Vector Graphics.
Vector Graphics Raster Graphics
Consists of XML format using line, points & arc. Consists of Pixels format like dots.
If you Zoom in the image, the quality of the image is not lost The quality of the image is lost after
after scaling. zooming.
Famous tools based on Vector Graphics are Adobe Famous tools based on Raster Graphics
Illustrator & CorelDRAW. is Adobe Photoshop.
•If you scale the SVG image, it will not lose its quality at any resolution
•SVG images can be created and edited with any text editor.
<body>
<h2>HTML5 SVG - LINE</h2>
<svg height="250" width="600">
<line x1="10" y1="10" x2="400" y2="400"
style="stroke:rgb(0,0,255);stroke-
width:3" />
</svg>
</body>
</html>
SVG Text
To draw HTML5 SVG containing Texts, you should use
the <text> element inside your SVG.
Set these attributes –
•x – The x-coordinate of the starting point of the text
•y – The y-coordinate of the starting point of the text
•Setting the width and height of the SVG is optional but often a
good idea
Syntax:
<svg>
<text x=”x-pos” y=”y-pos”> Text… </text>
</svg>
<!DOCTYPE html>
<html>
<HEAD>
<title> HTML5 SVG - TEXT</TITLE>
<style>
svg{
border:1px solid blue;
}
</style>
</head>
<body>
<h2> SVG - text </h2>
<svg height="250" width="400">
<text x="50" y="50" fill="red" font-
size="30px" font-family="Cavolini Condensed">HTML5 SVG
TEXT</text>
</svg>
</body>
</html>
<html>
<head>
<title>svg text</title>
<style>
</style>
</head>
<body>
<svg width="500" height="200">
<text x="30" y="30" fill="blue" font-size="25px"> G F G C
THIRTHAHALLI</body>
</text>
</svg>
<br>
<svg width="300" height="300">
<text x="30" y="30" fill="red" font-size="25px" transform="rotate(60)">
G F G C THIRTHAHALLI</body>
</text>
</svg>
<svg width="500" height="200">
<text x="30" y="30" fill="blue" font-size="25px"> G F G C
THIRTHAHALLI</body>
<tspan x="40" y="50" fill="pink"> bca</tspan>
<tspan x="40" y="70" fill="pink"> bsc</tspan>
</text>
</svg>
</body></html>
SVG Circle
To draw a circle inside HTML SVG, we should use the <circle> element inside our SVG.
Inside this element, we need to set these attributes –
•cx – The x-coordinate of the center of the circle
•cy – The y-coordinate of the center of the circle
•r – The radius of the circle
In the below example, the radius of the circle is 30% relative to the SVG so if you change the width and
height values of SVG, the area of the circle will also change.
To fix the radius irrespective of the SVG width and height values, you can use a fixed value of radius
like r:”120″.
Syntax:
<svg>
<circle cx=”x-center-val” cy=”y-center-val” r=”radius-val”/>
</svg>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<svg width="200" height="200">
<circle cx="99" cy="99" r="40" fill="red" stroke="green"
stroke-width="5"/>
</svg>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
#cir:hover
{
fill:green;
}
</style>
<body>
<svg width="200" height="200">
<circle id="cir" cx="99" cy="99" r="40" fill="red" />
</svg>
</body>
</html>
HTML5 SVG Rectangle
To draw a Rectangle inside SVG, you should use the <rect> element inside your
SVG.
Set these attributes –
•x – The x-coordinate of the top left corner
•y – The y-coordinate of the top left corner
•width – The width of the rectangle
•height – The height of the rectangle
In the below example, the <fill> is optional. This sets the color of the rectangle as
darkred.
Syntax:
<svg>
<rect x=”x-top-left” y=”y-top-left” width=”w-val” height=”h-val”/>
</svg>
<!DOCTYPE html>
<html>
<body>
<h> SVG-RECTANGLE<h><br>
<svg width="200" height="100">
<rect width="200" height="100" stroke="yellow" stroke-
width="4" fill="red" />
</svg>
</body>
</html>