Home Web Front-end H5 Tutorial SVG基础|SVG DEFS元素、SYMBOL元素和USE元素

SVG基础|SVG DEFS元素、SYMBOL元素和USE元素

May 17, 2016 am 09:07 AM
html

1.jpg

  SVG DEFS元素

  SVG的元素用于预定义一个元素使其能够在SVG图像中重复使用。例如你可以将一些图形制作为一个组,并用元素来定义它,然后你就可以在SVG图像中将它当做简单图形来重复使用。看下面的例子:

  1.   
  2.       
  3.          
  4.          
  5.       
  6.   
  7.    
复制代码

  在元素中定义的图形不会直接显示在SVG图像上。要显示它们需要使用元素来引入它们。如下面的代码所示:
  1.   
  2.   
  3.    
  4.         
  5.         
  6.    
  7.   

  8.   
  9.   
  10.       
复制代码

  要引用元素,必须在元素上设置一个ID,通过ID来引用它。元素通过xlink:href属性来引入元素。注意在ID前面要添加一个#。

  在元素中,通过x和y属性来指定重用图形的显示位置。注意在元素中的图形的定位点都是0,0,在使用元素来引用它的时候,它的定位点被转换为元素x和y属性指定的位置。

  下面是上面代码的返回结果:
2.jpg
  上面SVG图像中绿色的圆点并不是示例代码的一部分。它们是用来显示2个元素的x和y坐标的。

  哪些元素可以被定义为defs中的元素呢?

  你可以将下面的元素放入到元素中使用:

  •   任何图形元素,如:rect,line等
  •   g
  •   symbol

  SVG SYMBOL元素

  SVG 元素用于定义可重复使用的符号。嵌入在元素中的图形是不会被直接显示的,除非你使用元素来引用它。

  下面是一个使用元素的简单例子:

  1.    
  2.         
  3.    

  4.    
  5.    
复制代码

  元素需要一个ID号,以便以被元素引用。
3.jpg
  一个元素可以有preserveAspectRatio和viewBox属性。而元素不能拥有这些属性。因此相比于在元素中使用的方式来复用图形,使用元素也许是一个更好的选择。


  SVG USE元素

  SVG 元素可以在SVG图像中多次重用一个预定义的SVG图形,包括元素和元素。被重用的图形可以在定义的内部(图形将不可见直到使用use来引用它)或外部。

  下面是一个使用元素的例子:

  1.   
  2.    
  3.         
  4.         
  5.    
  6.   

  7.   
  8.   

  9.         
复制代码
  上面的例子显示的是在元素中定义的元素。

  要引用元素,必须给它一个ID号,通过ID号来引用它。

  下面是上面代码的返回结果:
4.jpg
  我们也可以引用不在元素中的图形。元素可以引用SCG图像中的任何元素,只要这个元素有一个唯一的ID号,例如:


  1.    
  2.         
  3.    

  4.    

  5.       
复制代码

  这个例子在元素中定义了一个元素。然后通过元素来引用这个元素。它返回的结果如下面所示:
5.jpg
  注意这里原始图形和复用的图形都会被显示。因为原始的图形并没有定义在元素中。所以它是可见的。

  你可以为引用的图形设置CSS样式。你可以在元素中使用style属性来为复用的图形设置它的样式。例如:


  1.   
  2.       
  3.   

  4.   
  5.   

  6.         
复制代码

  注意上面的代码中,原始的图形并没有设置样式,它将是默认的样式(黑色)。
6.jpg
  本文版权属于jQuery之家,转载请注明出处:http://www.htmleaf.com/ziliaoku/ ... g/201506132029.html

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)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 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)

Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.

See all articles