search
HomeWeb Front-endH5 TutorialHTML5 SVG 2D Introduction 7—Reuse and Reference of SVG Elements_html5 tutorial skills

We introduced a lot of graphic elements earlier. If many graphics themselves are the same, do we need to define a new one every time? Can we share some graphics? This is the focus of this section - the reuse of SVG elements.

Combined-g element
The g element is a container that combines a group of related graphic elements into a whole; in this way, we can operate on this whole. This element can usually be used in conjunction with the desc and title elements to provide structural information about the document. Well-structured documents are generally readable and render efficiently. Look at a small example:

Copy the code
The code is as follows:

version="1.1"width="5cm"height="5cm">
Twogroups,eachoftworectangles









fill="none"stroke="blue" stroke-width=".02cm"/>


Note a few points:
1.xmlns="http://www.w3.org/2000/svg" indicates that the default namespace of the entire svg element is svg. This can be omitted when there is no ambiguity. Since the svg document is an XML document, the relevant rules of the XML namespace are applicable here. For example, you can specify a namespace for svg display, provide an alias for the namespace, etc.
2.g elements can be nested.
3. The combined graphic elements can be given an id value just like a single element. In this way, when needed (such as animation and reuse of a group of elements), you only need to reference this id value.
4. Combining a group of graphic elements can uniformly set the related attributes of this group of elements (fill, stroke, transform, etc.). This is also a scenario where combination is used.

Template-symbol element
symbol element is used to define a graphic template (the template can contain many graphics). This template can be instantiated by the use element. The function of the template is very similar to that of the g element. They both provide a set of graphic objects, but there are some differences. The difference from the g element is:
1. The symbol element itself will not be rendered, only instances of the symbol template will be rendered.
2. The symbol element can have the attributes viewBox and preserveAspectRatio, which allow the symbol to scale the graphic element.

From a rendering perspective, elements similar to the symbol element are marker (defining arrows and labels) and pattern (defining colors) elements; these elements are not directly rendered; their usage is basically use element to instantiate. For this reason, the 'display' attribute is meaningless for symbols.
The following modified code shows how to use symbol:

Copy the code
The code is as follows:

xmlns:xlink="http://www.w3.org/1999/xlink"
version="1.1"width="5cm"height="5cm">
Twogroups,eachoftworectangles










fill="none"stroke="blue"stroke-width=".02cm"/>


define-defs element
SVG allows you to define a set of objects and then reuse this set of objects (note, not just graphic objects). The most common example is to define a gradient color and then assign it to the fill attribute in other graphics objects. Gradient colors are not rendered when defined, so objects of this type can be placed anywhere. Reuse often exists in graphics objects, and we don't want to render directly when defining, but want to render at the referenced place. This can be achieved using the defs element.

Generally, the recommended approach is to put referenced objects in defs elements whenever possible. These objects are usually: altGlyphDef, clipPath, cursor, filter, marker, mask, pattern, linearGradient, radialGradient, symbol and graphic objects, etc. Defining these objects in defs elements makes them easier to understand, thus improving accessibility.

In fact, the g element, symbol element, and defs element as container objects all provide reuse functions to varying degrees, but the characteristics of each element may be slightly different: for example, the g element is directly rendered, symbol and defs will not be rendered directly. The symbol contains the viewBox attribute and a new view window will be created.

Usually the id attribute is assigned to the elements defined in defs and used directly where used. Depending on the element, these definitions can be used in different places. For example, the following progressive color is used as an attribute:

Copy code
The code is as follows:

xmlns="http://www.w3.org/2000/svg"version=" 1.1">
LocalURIreferenceswithinancestor's'defs'element.





> ;
fill="url(#Gradient01)"/>


The definition of graphic related elements can be linked to the document using the use element. For example:

Copy code
The code is as follows:

xmlns="http://www.w3.org/2000/svg"xmlns:xlink="http://www.w3.org/ 1999/xlink">
ExampleUse01-Simplecaseof'use'ona'rect'



fill="none "stroke="blue"stroke-width=".2"/>

> ;

Please note the use of xlink namespace here. Although most viewers will display this correctly without it, for consistency the xlink namespace should be defined on the element.

Reference-use element
Any svg, symbol, g, single graphic element and use element can essentially be referenced by use elements as template objects (such as initialization). The graphics content referenced by use will be rendered at the specified location. Unlike the image element, the use element cannot reference the entire document. The
use element also has x, y, width and height attributes. These attributes can be omitted. If they are not omitted, the coordinates or length of the referenced graphic content will be mapped to the current user coordinate space. The function of the

use element is equivalent to deep copying the referenced object into an independent non-public DOM tree; the parent node of this tree is the use element. Although it is a non-public DOM node, it is still a DOM node in essence, so all attribute values, animations, events, CSS related settings, etc. of the referenced object will be copied and will still work, and these nodes will also inherit use Relevant attributes of the element and use ancestors (note that the referenced elements are deep copies. These copied elements have nothing to do with the original elements, so the attributes of the ancestor nodes of the referenced elements will not be inherited here). If these nodes themselves are related ( CSS) properties, and will also override inherited properties. These are consistent with ordinary DOM nodes, so be careful when using "visibility:hidden" on use elements, as it may not necessarily work. However, since this part of the nodes is not public, only the use element can be seen during DOM operations, so only the use element can be operated.

From a visual effect point of view, the use element is more like a placeholder. The visual effect after rendering is the same as rendering directly with the referenced object:
1.use element reference A symbol element
In this case, the visual effect is equivalent to:
(1) Replace the use element with the g element;
(2) Divide use by x, y, width, height, )Replace the referenced symbol element with an svg element. This svg element will explicitly use the width and height attributes of the use element (the use element without these attributes will be 100%);
(5) Change the graphic of the referenced symbol element The content is deep copied into the replaced svg.

2. The use element refers to an svg element
In this case, the visual effect is equivalent to: (1) Replace the use element with the g element;
(2 ) Move all the attributes of use except x, y, width, height, xlink:href to the g element;
(3) Change the x, y attributes of use into translate(x, y) and append to g The transform attribute of the element is last;
(4) Copy the referenced svg element including the content. This svg element will explicitly use the width and height attributes of the use element (the use element will use the original values ​​if it does not have these attributes);

3. Other situations
The visual effect in these cases is equivalent to: (1) Replace the use element with the g element;
(2) Divide use by x , y, width, height, xlink: All attributes other than href are moved to the g element;
(3) Change the x, y attributes of use to translate(x, y), and append them to the end of the transform attribute of the g element;
(4) Copy the reference element;

Look at the visual effect of the example below
:

Copy codeThe code is as follows:
xmlns="http:/ /www.w3.org/2000/svg"xmlns:xlink="http://www.w3.org/1999/xlink">
ExampleUse03-'use'witha'transform'attribute


> ;
fill="none"stroke="blue"stroke-width=".2"/ >
transform="translate(20,2.5)rotate(10)"/>



The appearance of the picture below is the same as the picture above
:

Copy codeCode As follows:
xmlns="http://www.w3.org/2000/svg"version= "1.1">
ExampleUse03-'use'witha'transform'attribute
fill="none"stroke="blue"stroke-width=".2"/>






Practical reference:
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/
Official documentation: http://www.w3.org/TR/SVG11/
Statement
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
H5 Code: Best Practices for Web DevelopersH5 Code: Best Practices for Web DevelopersApr 16, 2025 am 12:14 AM

Best practices for H5 code include: 1. Use correct DOCTYPE declarations and character encoding; 2. Use semantic tags; 3. Reduce HTTP requests; 4. Use asynchronous loading; 5. Optimize images. These practices can improve the efficiency, maintainability and user experience of web pages.

H5: The Evolution of Web Standards and TechnologiesH5: The Evolution of Web Standards and TechnologiesApr 15, 2025 am 12:12 AM

Web standards and technologies have evolved from HTML4, CSS2 and simple JavaScript to date and have undergone significant developments. 1) HTML5 introduces APIs such as Canvas and WebStorage, which enhances the complexity and interactivity of web applications. 2) CSS3 adds animation and transition functions to make the page more effective. 3) JavaScript improves development efficiency and code readability through modern syntax of Node.js and ES6, such as arrow functions and classes. These changes have promoted the development of performance optimization and best practices of web applications.

Is H5 a Shorthand for HTML5? Exploring the DetailsIs H5 a Shorthand for HTML5? Exploring the DetailsApr 14, 2025 am 12:05 AM

H5 is not just the abbreviation of HTML5, it represents a wider modern web development technology ecosystem: 1. H5 includes HTML5, CSS3, JavaScript and related APIs and technologies; 2. It provides a richer, interactive and smooth user experience, and can run seamlessly on multiple devices; 3. Using the H5 technology stack, you can create responsive web pages and complex interactive functions.

H5 and HTML5: Commonly Used Terms in Web DevelopmentH5 and HTML5: Commonly Used Terms in Web DevelopmentApr 13, 2025 am 12:01 AM

H5 and HTML5 refer to the same thing, namely HTML5. HTML5 is the fifth version of HTML, bringing new features such as semantic tags, multimedia support, canvas and graphics, offline storage and local storage, improving the expressiveness and interactivity of web pages.

What Does H5 Refer To? Exploring the ContextWhat Does H5 Refer To? Exploring the ContextApr 12, 2025 am 12:03 AM

H5referstoHTML5,apivotaltechnologyinwebdevelopment.1)HTML5introducesnewelementsandAPIsforrich,dynamicwebapplications.2)Itsupportsmultimediawithoutplugins,enhancinguserexperienceacrossdevices.3)SemanticelementsimprovecontentstructureandSEO.4)H5'srespo

H5: Tools, Frameworks, and Best PracticesH5: Tools, Frameworks, and Best PracticesApr 11, 2025 am 12:11 AM

The tools and frameworks that need to be mastered in H5 development include Vue.js, React and Webpack. 1.Vue.js is suitable for building user interfaces and supports component development. 2.React optimizes page rendering through virtual DOM, suitable for complex applications. 3.Webpack is used for module packaging and optimize resource loading.

The Legacy of HTML5: Understanding H5 in the PresentThe Legacy of HTML5: Understanding H5 in the PresentApr 10, 2025 am 09:28 AM

HTML5hassignificantlytransformedwebdevelopmentbyintroducingsemanticelements,enhancingmultimediasupport,andimprovingperformance.1)ItmadewebsitesmoreaccessibleandSEO-friendlywithsemanticelementslike,,and.2)HTML5introducednativeandtags,eliminatingthenee

H5 Code: Accessibility and Semantic HTMLH5 Code: Accessibility and Semantic HTMLApr 09, 2025 am 12:05 AM

H5 improves web page accessibility and SEO effects through semantic elements and ARIA attributes. 1. Use, etc. to organize the content structure and improve SEO. 2. ARIA attributes such as aria-label enhance accessibility, and assistive technology users can use web pages smoothly.

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool