HTML SVG Graphics

      Comments Off on HTML SVG Graphics
Spread the love

HTML SVG Graphics

SVG stands for Scalable Vector Graphics. SVG graphics can be created with simple text editors and they are scalable without losing quality. SVG is an XML-based format for vector graphics.

The <svg> tag is used to define an SVG graphic. The following example shows how to create an SVG graphic:

 
<!DOCTYPE html>
<html>
<body>

<svg width="100" height="100">
  <circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />
</svg>

</body>
</html>

In the above example, the <svg> element creates the SVG container. The width and height attributes define the size of the SVG container. The <circle> element defines a circle with a center point (cx, cy) and a radius (r).

SVG Circle

The <circle> element is used to create a circle. The cx and cy attributes define the x and y coordinates of the center of the circle. The r attribute defines the radius of the circle. The following example shows how to create a circle:

php
<!DOCTYPE html>
<html>
<body>

<svg width="100" height="100">
  <circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />
</svg>

</body>
</html>

SVG Rectangle

The <rect> element is used to create a rectangle. The x and y attributes define the upper-left corner of the rectangle. The width and height attributes define the size of the rectangle. The following example shows how to create a rectangle:

 
<!DOCTYPE html>
<html>
<body>

<svg width="200" height="200">
  <rect x="50" y="50" width="100" height="100" stroke="blue" stroke-width="2" fill="white" />
</svg>

</body>
</html>

SVG Star

To create a star, we can use the <polygon> element. The following example shows how to create a star:

 
<!DOCTYPE html>
<html>
<body>

<svg width="200" height="200">
  <polygon points="100,10 40,198 190,78 10,78 160,198" style="fill:lime;stroke:purple;stroke-width:1" />
</svg>

</body>
</html>

In the above example, the <polygon> element defines a polygonal shape. The points attribute defines the x and y coordinates of the points that make up the polygon. The style attribute defines the fill color, stroke color, and stroke width of the polygon.

SVG and Canvas are two different graphics technologies used in web development. SVG stands for Scalable Vector Graphics, which is a vector graphics format that uses XML to define 2D graphics. Canvas, on the other hand, is a bitmap-based graphics technology that allows for dynamic and interactive graphics rendering.

SVG is best suited for creating static images or icons, while Canvas is best suited for creating dynamic, interactive graphics or animations. SVG images are scalable without losing quality, making them ideal for high-resolution displays. SVG is also supported by all modern web browsers.

Canvas, on the other hand, is ideal for creating complex animations and graphical effects. It allows developers to draw complex shapes and images in real-time, making it perfect for creating interactive games or visualizations. Canvas images are resolution dependent, which means that they can become pixelated when resized or zoomed in.

In terms of syntax, SVG uses XML tags and attributes to define graphics elements, while Canvas uses JavaScript commands to draw and manipulate graphics. SVG is generally easier to work with for designers and developers who are familiar with HTML, while Canvas requires knowledge of JavaScript and its APIs.

Overall, both SVG and Canvas have their own strengths and weaknesses, and choosing one over the other depends on the specific requirements of the project. If you need static, scalable graphics, SVG is the way to go. If you need dynamic, interactive graphics, Canvas is the way to go.