Home Web Front-end H5 Tutorial HTML5 SVG 2D Introduction 1—SVG (Scalable Vector Graphics) Overview_html5 Tutorial Tips

HTML5 SVG 2D Introduction 1—SVG (Scalable Vector Graphics) Overview_html5 Tutorial Tips

May 16, 2016 pm 03:50 PM
2d svg

Bitmaps and Vector Graphics
Previously, graphics displayed in browsers, such as jpeg, gif, etc., were all bitmaps, and these image formats were based on raster. In a raster image, the image file defines the color value of each pixel in the image. The browser needs to read these values ​​and act accordingly. The reproduction ability of this kind of image is relatively strong, but it will appear insufficient in some situations. For example, when a browser displays an image at different sizes, jagged edges often result, and the browser has to interpolate or guess values ​​for pixels that do not exist in the original image; this results in image distortion. Additionally, animating bitmaps is, at best, limited to producing "flip book" type animations, where individual images are displayed in rapid succession.

Vector graphics overcome some of these difficulties by specifying the instructions needed to determine the value of each pixel, rather than specifying the values ​​themselves. For example, instead of providing pixel values ​​for a one-inch diameter circle, vector graphics tell the browser to create a one-inch diameter circle and let the browser (or plug-in) do the rest. This removes many of the limitations of raster graphics; with vector graphics, the browser only knows that it must draw a circle. If an image needs to be displayed at three times its normal size, the browser simply draws the circle at the correct size without having to perform the usual interpolation of raster images. Similarly, the browser receives instructions that can be more easily tied to external information sources such as applications and databases. To animate an image, the browser simply receives instructions on how to manipulate properties such as radius or color. .
In the HTML system, the most commonly used technology for drawing vector graphics is SVG and the newly added canvas element of HTML5. Both technologies support drawing vector and raster images.

SVG Overview
Scalable Vector Graphics (SVG for short) is a language that uses XML to describe two-dimensional graphics (SVG strictly follows XML syntax). SVG allows three types of graphic objects: vector graphic shapes (such as paths composed of straight lines and curves), images, and text. Graphic objects (including text) can be grouped, styled, transformed, and combined into previously rendered objects. The SVG feature set includes nested transforms, clipping paths, alpha masks, and template objects.

SVG drawings are interactive and dynamic. For example, you can use scripts to define and trigger animations. This is very powerful compared to Flash. Flash is a binary file, which is difficult to create and modify dynamically. SVG is a text file, and dynamic manipulation is quite easy. Moreover, SVG directly provides relevant elements to complete animation, which is very convenient to operate.

SVG is compatible with other web standards and directly supports the Document Object Model DOM. This is also a very powerful point compared with the canvas in HTML5 (note here that SVG also uses a similar canvas internally to display SVG graphics. Later you will find that many features are somewhat similar to the canvas of HTML5; in the article If it is not explicitly stated that it is the canvas of SVG, it refers to the canvas element in HTML5). Therefore, many advanced applications of SVG can be easily implemented using scripts. Moreover, SVG graphic elements basically support standard events in the DOM. A number of event handlers (such as "onmouseover" and "onclick") can be assigned to any SVG graphic object. Although the rendering speed of SVG is not as fast as that of canvas elements, the advantage is that DOM operations are very flexible. This advantage can completely make up for the disadvantage in speed.

SVG can be said to be both a protocol and a language; it is both a standard element of HTML and an image format.
SVG is not something in HTML5, but it is considered one of the popular web technologies, so it will be included in this topic for the time being.

Comparison of SVG and other image formats
Compared with other image formats, SVG has many advantages (many advantages come from the advantages of vector graphics):
• SVG The file is pure XML and can be read and modified by many tools (such as Notepad).
• SVG is smaller and more compressible than JPEG and GIF images.
• SVG is scalable, can be enlarged without losing image quality, and can be printed with high quality at any resolution.
• Text in SVG images is optional and searchable (great for making maps).
• SVG can run with Java technology.
• SVG is an open standard.

Comparison of SVG and Flash
SVG’s main competitor is Flash. Compared with Flash, the biggest advantage of SVG is that it is compatible with other standards (such as XSL and DOM) and is easy to operate, while Flash is a proprietary technology that is not open source. In other aspects such as storage format and dynamically generated graphics, SVG also has great advantages.

How SVG is presented
The browser that supports HTML5 and SVG is not the focus of the discussion here. Basically, it is enough to install the latest Chrome or FireFox browser (IE users please Just install IE9. As for versions before IE9, you need to install the SVG plug-in, which will be skipped here). For browsers that directly support SVG, SVG mainly uses two presentation methods on both sides.

Inline into HTML
SVG is a standard HTML element, which can be written directly into HTML. See the following example:

Copy code
The code is as follows:









width="200px" height="200px">
fill= "none" stroke="black"/>
style="stroke: black; fill: red;"/>


The main reason for considering the compatibility issues is to consider these parts; these parts basically don’t need to be written in HTML5 (it’s up to you whether to write them or not).

Stand-alone SVG file

Stand-alone SVG refers to the vector graphics file format provided by using the svg file extension. Embed this svg file in your browser and it's ready to use. 1. Independent SVG file/page, the defined template is basically as follows:



Copy code
The code is as follows:
< ;/svg>


Save such a text file as a file with an svg extension, such as sun.svg. Such a file can be opened directly with a browser or embedded as a reference. in other pages.
2.HTML references external SVG files.
Just use object or img elements to embed SVG graphics, such as the following small example:




Copy code
The code is as follows:

My First SVG Page


width="300px" height="300px">

Your browser does not support SVG - please upgrade to a modern browser.


< ;/object>
svg not supported!




In fact, SVG can also be placed in other XML documents, or it can be formatted and verified using XML-related technologies like other XML documents. This is not the point and is omitted here.

The rendering order of SVG

SVG is rendered strictly in accordance with the order of defined elements, which is different from HTML that relies on z-index values ​​to control layering. In SVG, elements written in the front are rendered first, and elements written in the back are rendered last. The later-rendered element will cover the previous element. Although sometimes it does not appear to be covered due to the effect of transparency, SVG is indeed rendered in strict order. Note: SVG is defined in XML, so it is case-sensitive, which is different from HTML.

Practical reference
:
Official documentation: http://www.w3.org/TR/SVG11/
Script index: http://msdn.microsoft.com/zh-cn/library/ff971910(v=vs.85).aspx
Development Center: https://developer.mozilla.org/en/ SVG
Popular reference:http://www.chinasvg.com/
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Let's talk about how to use SVG to achieve image mosaic effect Let's talk about how to use SVG to achieve image mosaic effect Sep 01, 2022 am 11:05 AM

How to use SVG to achieve image mosaic effect without using Javascript? The following article will give you a detailed understanding, I hope it will be helpful to you!

How to convert svg to jpg format How to convert svg to jpg format Nov 24, 2023 am 09:50 AM

svg can be converted to jpg format by using image processing software, using online conversion tools, and using the Python image processing library. Detailed introduction: 1. Image processing software includes Adobe Illustrator, Inkscape and GIMP; 2. Online conversion tools include CloudConvert, Zamzar, Online Convert, etc.; 3. Python image processing library, etc.

An in-depth analysis of how to use svg icons in vue3+vite An in-depth analysis of how to use svg icons in vue3+vite Apr 28, 2022 am 10:48 AM

svg images are widely used in projects. The following article will introduce how to use svg icons in vue3 + vite. I hope it will be helpful to everyone!

Produced by Peking University: The latest SOTA with texture quality and multi-view consistency, achieving 3D conversion of one image in 2 minutes Produced by Peking University: The latest SOTA with texture quality and multi-view consistency, achieving 3D conversion of one image in 2 minutes Jan 10, 2024 pm 11:09 PM

It only takes two minutes to convert pictures into 3D! It is also the kind with high texture quality and high consistency in multiple viewing angles. No matter what species it is, the single-view image when input is still like this: Two minutes later, the 3D version is completed: △ Upper, Repaint123 (NeRF); Lower, Repaint123 (GS) The new method is called Repaint123. The core idea is to combine 2D The powerful image generation capabilities of the diffusion model are combined with the texture alignment capabilities of the redraw strategy to generate high-quality, consistent images across multiple views. In addition, this study also introduces a visibility-aware adaptive repaint intensity method for overlapping regions. Repaint123 solves the problems of previous methods such as large multi-view deviation, texture degradation, and slow generation in one fell swoop. Current item

VUE3 introductory tutorial: Use Vue.js plug-in to play with SVG VUE3 introductory tutorial: Use Vue.js plug-in to play with SVG Jun 16, 2023 am 09:48 AM

With the continuous development of modern Web front-end development, more and more technologies are widely used in actual development. Among them, Vue.js is currently one of the most popular JavaScript frameworks. It is based on the MVVM model and provides a rich API and component library, making it easier to develop responsive, reusable, and efficient web applications. The latest version of Vue.js3 has better performance and richer features than the old version, which has attracted widespread attention and research. This article will introduce to you a

Detailed explanation of using SVG to add logo to favicon Detailed explanation of using SVG to add logo to favicon Sep 07, 2022 am 10:30 AM

How to add logo to favicon using SVG? The following article will introduce to you how to use SVG to generate favicon with logo. I hope it will be helpful to you!

How to use svg method in vue3+vite2 How to use svg method in vue3+vite2 May 11, 2023 pm 05:55 PM

1. Install vite-plugin-svg-icons. You also need to install fast-glob related dependencies. Otherwise, when vite runs npmrundev, it will report the Cannotfindmodule'fast-glob' error npmifast-glob@3.x-Dnpmivite-plugin-svg. -icons@2.x-D 2. Create a new component index.vueimport{computed}from'vue';cons under src/components/svgIcon

Draw SVG files on HTML5 canvas Draw SVG files on HTML5 canvas Sep 15, 2023 pm 03:09 PM

To draw HTMLImageElements on a canvas element, use the drawImage() method. This method defines an Image variable using src="mySVG.svg" and uses drawImage when loading. varmyImg=newImage();myImg.onload=function(){ ctx.drawImage(myImg,0,0);}img.src="http://www.example.com/files/sample.svg";

See all articles