Understanding SVGs
What is SVG?
SVG stands for Scalable Vector Graphics. It's an XML-based markup language for describing two-dimensional based vector graphics. SVG is essentially to graphics what HTML is to text. SVGs are scalable, meaning they maintain high quality at any size, and are interactive and scriptable, hence ideal for animations.
Why SVG for Animations?
Scalability: Vector-based, so they scale up or down without losing quality.
Performance: Generally smaller file sizes compared to bitmap images, which makes them faster to load.
Interactivity: Can be manipulated via CSS and JavaScript.
Accessibility: Text inside SVGs is selectable and searchable.
Creating a Simple SVG
SVGs can be created with graphic design software like Adobe Illustrator or even coded by hand. Here's an example of a simple SVG coded directly:
<svg width="20vh" height="20vh" xmlns="http://www.w3.org/2000/svg">
<circle cx="10vh" cy="10vh" r="8vh" stroke="cyan"
strokeWidth="0.2vh" fill="deeppink" />
</svg>
<svg>: This tag defines the SVG container that holds the SVG graphics. It acts as a canvas for vector graphics defined inside it.
width="20vh": Sets the width of the SVG canvas to 20 viewport heights. This means the SVG's width will be 20% of the viewport height, making it responsive and scaling based on the height of the viewport.
height="20vh": Similarly, sets the height of the SVG canvas to 20 viewport heights, ensuring that the SVG's height matches its width for a square aspect ratio.
xmlns="http://www.w3.org/2000/svg": Declares the XML namespace for SVG. This is necessary because SVG is an XML-based markup language, and this attribute ensures that the SVG markup is correctly interpreted according to the SVG specification.
<circle>: This tag defines a circle. The circle is one of the basic shapes in SVG
cx="10vh": The cx attribute sets the x-coordinate of the center of the circle. Here, it's set to 10 viewport heights, effectively centering the circle horizontally within the 20vh-wide SVG canvas.
cy="10vh": The cy attribute sets the y-coordinate of the center of the circle. Setting it to 10vh centers the circle vertically within the 20vh-tall SVG canvas.
r="8vh": The r attribute specifies the radius of the circle. The circle's radius is 8vh, making the circle's diameter 16vh. This size allows for a margin between the circle and the SVG container edges, given the container's 20vh dimensions.
stroke="cyan": This attribute sets the color of the circle's outline (stroke) to cyan.
strokeWidth="0.2vh": Defines the thickness of the stroke around the circle. The stroke width is set to 0.2vh, making it responsive to the viewport height like the other dimensions.
fill="deeppink": Determines the fill color of the circle, which is set to a deep pink color. This color fills the interior of the circle.
This SVG example creates a responsive, square-shaped SVG canvas that automatically scales its size based on the viewport's height. Inside this canvas, a circle is drawn, centered both horizontally and vertically, with a cyan stroke around it and filled with a deep pink color. The use of viewport units (vh) for all dimensions ensures that the circle's size and position scale dynamically with changes in the viewport size, maintaining its aspect ratio and positioning within the SVG container.