Home Web Front-end H5 Tutorial HTML5 SVG 2D Introduction 7—Reuse and Reference of SVG Elements_html5 tutorial skills

HTML5 SVG 2D Introduction 7—Reuse and Reference of SVG Elements_html5 tutorial skills

May 16, 2016 pm 03:49 PM
2d svg Quote Reuse

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< /desc>


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 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

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)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

How to use block quotes in Apple Notes How to use block quotes in Apple Notes Oct 12, 2023 pm 11:49 PM

In iOS 17 and macOS Sonoma, Apple has added new formatting options for Apple Notes, including block quotes and a new Monostyle style. Here's how to use them. With additional formatting options in Apple Notes, you can now add block quotes to your notes. The block quote format makes it easy to visually offset sections of writing using the quote bar to the left of the text. Just tap/click the "Aa" format button and select the block quote option before typing or when you are on the line you want to convert to a block quote. This option applies to all text types, style options, and lists, including checklists. In the same Format menu you can find the new Single Style option. This is a revision of the previous "equal-width"

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!

C++ compilation error: undefined reference, how to solve it? C++ compilation error: undefined reference, how to solve it? Aug 21, 2023 pm 08:52 PM

C++ is a popular programming language, but during use, the compilation error "undefined reference" often occurs, which brings a lot of trouble to program development. This article will discuss the solution to the "undefined reference" error from both the cause and the solution. 1. Cause of error When the C++ compiler compiles a source file, it will be divided into two stages: the compilation stage and the link stage. The compilation phase converts the source code in the source files into assembly code, while the linking phase combines different source files into an executable file.

What are the benefits of C++ functions returning reference types? What are the benefits of C++ functions returning reference types? Apr 20, 2024 pm 09:12 PM

The benefits of functions returning reference types in C++ include: Performance improvements: Passing by reference avoids object copying, thus saving memory and time. Direct modification: The caller can directly modify the returned reference object without reassigning it. Code simplicity: Passing by reference simplifies the code and requires no additional assignment operations.

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.

How to use C++ reference and pointer parameter passing? How to use C++ reference and pointer parameter passing? Apr 12, 2024 pm 10:21 PM

References and pointers in C++ are both methods of passing function parameters, but there are differences. A reference is an alias for a variable. Modifying the reference will modify the original variable, while the pointer stores the address of the variable. Modifying the pointer value will not modify the original variable. When choosing to use a reference or a pointer, you need to consider factors such as whether the original variable needs to be modified, whether a null value needs to be passed, and performance considerations.

C++ syntax error: When a function returns a pointer or reference, it cannot return a local variable or temporary object. What should I do? C++ syntax error: When a function returns a pointer or reference, it cannot return a local variable or temporary object. What should I do? Aug 22, 2023 am 09:22 AM

C++ is an object-oriented programming language, and its flexibility and power often provide programmers with great help. However, precisely because of its flexibility, it is difficult to avoid various small errors when programming. One of the most common mistakes is that when a function returns a pointer or reference, it cannot return a local variable or temporary object. So how to deal with this problem? This article will introduce the relevant content in detail. The cause of the problem is that in the C++ language, local variables and temporary objects are dynamically allocated during the running of the function. When the function ends, these local variables and temporary

In-depth analysis of pointers and references in C++ to optimize memory usage In-depth analysis of pointers and references in C++ to optimize memory usage Jun 02, 2024 pm 07:50 PM

By using pointers and references, memory usage in C++ can be optimized: Pointers: store addresses of other variables and can point to different variables, saving memory, but may generate wild pointers. Reference: Aliased to another variable, always points to the same variable, does not generate wild pointers, and is suitable for function parameters. Optimizing memory usage can improve code efficiency and performance by avoiding unnecessary copies, reducing memory allocations, and saving space.

See all articles