SVG element in HTML5
SVG is used to draw 2D graphics and graphics applications on the website, mostly useful for vector style diagrams. So what content does SVG have, how it is used, what is its difference and what is Canvas?
In the previous lesson, TipsMake joined you in learning about the Canvas element in HTML5 used to draw graphics for the website. Today we would like to introduce one more element with a similar task, also used to draw 2D graphics and graphics applications on the website, which is SVG element. So what content does SVG have, how it is used, what is its difference and what is Canvas? Let's find out with Quantrimang through this article.
What is SVG?
SVG stands for Scalable Vector Graphics , which is an image format (similar to JPG, PNG, and so on).
SVG is used to draw 2D graphics and graphics applications on the website, mostly useful for vector-style diagrams such as Pie charts, two-dimensional graphs in X and Y coordinates.
SVG is the official standard of the W3C world web organization.
Advantages
- SVG images can be enlarged but not broken.
- SVG images can be printed in high quality at any resolution.
- SVG images can be created and edited with javascript.
- SVG images can be searched, indexed, compressed, .
- SVG images can move using built-in animation components.
- SVG images may contain links to any other document.
- SVG is mostly supported on all browsers.
Element in HTML
Element in HTML is a container for drawing graphics.
SVG has several methods to draw such as lines, box shapes, circles, text and graphic images .
Draw SVG circle
To draw a circle, we use the card
Draw circles in SVG HTML5
stroke="lightcoral" stroke-width="4" fill="moccasin"
The internal properties are:
- cx: center position, calculated from the left edge of SVG.
- cy: mind position, calculated from the upper edge.
- r: radius.
- fill: define the fill color.
- stroke: border.
Result:
Draw the SVG ellipse
To draw an ellipse we use the card .
Draw ellipse in SVG HTML5
stroke="darkslateblue" stroke-width="4" fill="lightblue"
The same parameters when drawing a circle, add two attributes:
- rx: radius from center to left edge.
- ry: radius from center to upper edge.
Result:
Draw SVG rectangles
You use tags to draw rectangles.
Draw rectangles in SVG HTML5
style="fill:lightcoral;stroke-width:10;stroke:pink"
Result:
Draw SVG rectangular corners
Use cards to draw.
Draw angled rectangles in SVG HTML5
style="fill:red;stroke:black;stroke-width:5;opacity:0.5"
Result:
Draw the SVG line
Used to draw straight lines:
Draw straight lines in SVG HTML5
Result:
Draw folding lines of SVG
Used to draw kinks, in which the points attribute in four points shows the x and y coordinates of the points to be joined together:
Draw kinks in SVG HTML5
points="10,10 80,10 80,80 150,80 150,150 220,150 220,220"
fill="pink" stroke="palevioletred" stroke-width="5"
Result:
Draw stars in SVG
We use tags , in which points determine the x and y coordinates of vertices.
used to draw a closed shape includes straight lines. The end point will connect to the starting point.
Draw stars in SVG HTML5
style="fill:lemonchiffon;stroke:purple;stroke-width:5;fill-rule:evenodd;"
In addition, it is also used to draw other polygons.
Draw complex SVG shapes
Used to draw complex shapes, the path to draw is defined in attribute d = "way - drawing command"
The commands used in attribute d are:
- M: move the drawing point to the x and y coordinates (M xy).
- L: draw from the current point to the point x, y (L xy).
- H: Draw a horizontal line from the current point to the point where x (H x) is coordinates.
- V: Draw a vertical line to the point with the y coordinate (y y).
- C: draw cubic-bezier curve from current point x0, y0 to point x, y. the beginning of the tangent curve to the straight line x0, y0, x1, y1, the second point of the tangent curve to the wall x, y, x2, y2 C x1 y1, x2 y2, x, y.
- S: draw a smooth curve from the current point x0, y0 to the point x, y in which the first point tangent to the line x0, y0, x2, y2 S x2 y2, x y.
- Q: Draw cubic-bezier curves from the current point x0, y0 to point x, y. The beginning of the tangent curve to the straight line x0, y0, x1, y1 is the second point of the tangent curve to the wall x, y, x1, y1 C x1 y1, x y.
- T: draw cubic-bezier curves, from current point to point x, y (T xy).
- A: draw the arc.
- Z: close the drawing line.
Draw complex SVG shapes
style="stroke:crimson;stroke-width:3; fill:lightpink"
Result:
Insert text into SVG
To add text to SVG start at coordinates (x, y) then use .
There are attributes: font-family, font-style, font-weight, font-variant, font-size, font-size, font-size, adjust, kerning, letter-spacing, word-spacing, text-decoration.
Insert text into SVG
style="fill:purple;stroke:indigo;stroke-width:5;"
Draw SVG shapes with gradient colors
You can use tags or to define a SVG Gradient.
style="stop-color:#ff9999;stop-opacity:1"
style="stop-color:#9933ff;stop-opacity:1"
Differences between SVG and Canvas
The comparison table below shows some important differences between Canvas and SVG:
CANVAS SVG
- Canvas draws 2D graphics with JavaScript.
- Canvas is displayed as pixels standing side by side.
- In canvas, when an image has been drawn, it will not be remembered by the browser. If the position of the image wants to change, the whole image will need to be re-drawn including the objects around the graphic object.
- Depends on the resolution
- No support in handling events
- The ability to create images for text is not good
- You can save the image after drawing it as .png or .jpg
- Very suitable for graphics games
- SVG describes 2D graphics in XML
- SVG is built on XML so all components are available in SVG DOM. You can use javascript to handle the above events for SVG elements.
- In SVG, each drawn image is stored as an object. If the properties of an SVG object change, the browser will automatically return the result.
- Not dependent on resolution
- Support in handling events
- Suitable for wide image building applications (Google Maps)
- Build images slowly if graphics are complex (Everything using multiple DOMs is slow)
- Not suitable for game applications
Okay, so we've finished learning how to use HTML5 graphics drawing canvas and. Hopefully the lesson will be useful for you. Let's wait for the next lesson with TipsMake.
Wish you can design a perfect website!
You should read it
- Canvas element in HTML5
- Corel Draw - Lesson 5: Draw a circle, an ellipse
- How to draw a straight line, draw an arrow in Excel
- Corel Draw: What is Corel DRAW - Introduction
- Reference Canvas in HTML
- How to draw straight lines, curves in Photoshop
- How to draw a line chart in Excel
- How to draw anime cats in detail step by step
- Corel Draw 12: Draw the ducklings swimming
- Draw nested circles
- How to draw anime hair covering one eye
- How to Draw Basic Shapes Using Open Office Draw