Home Web Front-end H5 Tutorial Introduction to SVG 2D in HTML5 12—Introduction to SVG DOM and DOM operations_html5 tutorial skills

Introduction to SVG 2D in HTML5 12—Introduction to SVG DOM and DOM operations_html5 tutorial skills

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

Using scripts can easily complete various complex tasks, and it is also a mainstream way to complete animation and interaction. Since SVG is an element of html, it supports ordinary DOM operations. And since SVG is essentially an xml document, there is also a special DOM operation, which is mostly called SVG DOM. Of course, since IE currently does not support SVG, developing SVG pages based on IE requires different methods. Everyone is actually familiar with this part of knowledge, so let’s just take a brief look at it.

DOM operations in HTML pages
Everyone should be familiar with DOM. Here is a small example:

Copy code
The code is as follows:



<script> <br>function CreateSVG () { <br>var xmlns = "http://www.w3.org/2000/svg"; <br>var boxWidth = 300; <br>var boxHeight = 300; <br>var svgElem = document.createElementNS (xmlns, "svg"); <br>svgElem.setAttributeNS (null, "viewBox", "0 0 " boxWidth " " boxHeight); <br>svgElem.setAttributeNS (null, "width", boxWidth); <br>svgElem.setAttributeNS (null, "height" , boxHeight); <br>svgElem.style.display = "block"; <br>var g = document.createElementNS (xmlns, "g"); <br>svgElem.appendChild (g); <br>g.setAttributeNS (null, 'transform', 'matrix(1,0,0,-1,0,300)'); <br>// draw linear gradient <br>var defs = document.createElementNS (xmlns, "defs"); <br>var grad = document.createElementNS (xmlns, "linearGradient"); <br>grad.setAttributeNS (null, "id", "gradient"); <br>grad.setAttributeNS (null, "x1", "0% "); <br>grad.setAttributeNS (null, "x2", "0%"); <br>grad.setAttributeNS (null, "y1", "100%"); <br>grad.setAttributeNS (null, "y2", "0%"); <br>var stopTop = document.createElementNS (xmlns, "stop"); <br>stopTop.setAttributeNS (null, "offset", "0%"); <br>stopTop .setAttributeNS (null, "stop-color", "#ff0000"); <br>grad.appendChild (stopTop); <br>var stopBottom = document.createElementNS (xmlns, "stop"); <br>stopBottom.setAttributeNS (null, "offset", "100%"); <br>stopBottom.setAttributeNS (null, "stop-color", "#0000ff"); <br>grad.appendChild (stopBottom); <br>defs.appendChild (grad); <br>g.appendChild (defs); <br>// draw borders <br>var coords = "M 0, 0"; <br>coords = " l 0, 300"; <br>coords = " l 300, 0"; <br>coords = " l 0, -300"; <br>coords = " l -300, 0"; <br>var path = document.createElementNS (xmlns, "path") ; <br>path.setAttributeNS (null, 'stroke', "#000000"); <br>path.setAttributeNS (null, 'stroke-width', 10); <br>path.setAttributeNS (null, 'stroke- linejoin', "round"); <br>path.setAttributeNS (null, 'd', coords); <br>path.setAttributeNS (null, 'fill', "url(#gradient)"); <br>path .setAttributeNS (null, 'opacity', 1.0); <br>g.appendChild (path); <br>var svgContainer = document.getElementById ("svgContainer"); <br>svgContainer.appendChild (svgElem); <br> } <br></script>





Have you noticed, the DOM operation of ordinary html elements is exactly the same:
Select element: document.getElementById
Create element: document.createElementNS
Another way to create child elements: element. createChildNS
Add an element: node.appendChild
Set the element's attributes: element.setAttributeNS/element.setAttribute
In addition to the above operations, the following operations and attributes are also common:
Get the element's attributes Attribute value: element.getAttributeNS/element.getAttribute
Check whether an attribute exists on the element: element.hasAttributeNS
Remove an attribute of the element: element.removeAttributeNS
Parent element, child element and sibling node: element. parentNode/element.firstChild/child.nextSibling
These methods will not be introduced in detail here; in addition, the node structure of the DOM tree and the inheritance relationship between objects are also similar, so they will not be described in detail. Students who need it can refer to the documentation of DOM Core Object later.
However, it should be noted that SVG is essentially an XML document, so the basic DOM methods used are all ending with NS to provide related namespaces; if a namespace has been provided when creating an element, and there are not multiple namespaces problem, then when setting related attributes, you can also choose to use the version without NS, such as directly using element.setAttribute to set the attribute value, but in general, it is still strongly recommended to use the version with NS ending, because this version always It works fine, even in the case of multiple namespaces.
SVG DOM
How is this different from the standard DOM? I haven’t found any comprehensive information. Currently, I only know that the methods of assigning attributes are different. If there are any students who know about this, please let me know.
In the above example, we use element.setAttributeNS/element.setAttribute to assign values ​​to attributes. In SVG DOM, you can use the object-oriented method to assign values ​​to the attributes of objects by accessing dot numbers. For example, the following are two Comparison of methods:
Common DOM method:

Copy the code
The code is as follows:

element.setAttribute("x", "10");
element.setAttribute("y", "20");
element.setAttribute("width", "100%");
element.setAttribute("height", "2em");

And the SVG DOM way:

Copy code
The code is as follows:

element.x.baseVal.value = 10;
element.y.baseVal.value = 20;
element.width .baseVal.newValueSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_PERCENTAGE, 100);
element.height.baseVal.newValueSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_EMS, 10);

DOM script is a traditional script, which is characterized by passing Construct "value strings" to set individual items. The advantage of the SVG DOM script style is that you don't have to build a "value string", so the performance is better than DOM script.

Script embedded in SVG
If you want to add a script inside SVG, you need to use the script element. This has been mentioned before. Apart from this, it is basically the same as putting the script in It's the same into the external HTML. Look at an example:

Copy the code
The code is as follows:










Click on this text to show rectangle color.
Click on rectangle to show rectangle area.
Click on this text to show the number of child
elements of the root element.






在这个例子中,列举了常见的获取DOM对象的方式
1. 通过document.getElementById或者document.getElementByClassName之类的方法获取对象;
2. 通过document.documentElement或者document.rootElement获取document对象;
3. 通过事件参数evt.target获取产生事件的对象。这种方式的优点就是不使用id就可以获取到产生事件的对象。
其余的脚本基本和普通的DOM是一样的。

实用参考:
脚本索引:http://msdn.microsoft.com/zh-cn/library/ff971910(v=vs.85).aspx
开发中心:https://developer.mozilla.org/en/SVG
热门参考:http://www.chinasvg.com/
官方文档:http://www.w3.org/TR/SVG11/
DOM Core Object API:http://reference.sitepoint.com/javascript/Document
SVG DOM常用属性和方法:http://riso.iteye.com/blog/393454, http://riso.iteye.com/blog/393459
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)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1243
24
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!

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!

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.

What does vue dom mean? What does vue dom mean? Dec 20, 2022 pm 08:41 PM

DOM is a document object model and an interface for HTML programming. Elements in the page are manipulated through DOM. The DOM is an in-memory object representation of an HTML document, and it provides a way to interact with web pages using JavaScript. The DOM is a hierarchy (or tree) of nodes with the document node as the root.

What is the reason why ref binding to dom or component fails in vue3 and how to solve it What is the reason why ref binding to dom or component fails in vue3 and how to solve it May 12, 2023 pm 01:28 PM

Vue3ref binding DOM or component failure reason analysis scenario description In Vue3, it is often used to use ref to bind components or DOM elements. Many times, ref is clearly used to bind related components, but ref binding often fails. Examples of ref binding failure situations The vast majority of cases where ref binding fails is that when the ref is bound to the component, the component has not yet been rendered, so the binding fails. Or the component is not rendered at the beginning and the ref is not bound. When the component starts to render, the ref also starts to be bound, but the binding between ref and the component is not completed. At this time, problems will occur when using component-related methods. The component bound to ref uses v-if, or its parent component uses v-if to cause the page to

What are the ways to obtain DOM nodes in Vue3 What are the ways to obtain DOM nodes in Vue3 May 11, 2023 pm 04:55 PM

1. Native js gets the DOM node: document.querySelector (selector) document.getElementById (id selector) document.getElementsByClassName (class selector).... 2. Get the instance object of the current component in vue2: because each vue Each component instance contains a $refs object, which stores references to the corresponding DOM elements or components. So by default, the component's $refs point to an empty object. You can first add ref="name" to the component, and then pass this.$refs.

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

What are the dom and bom objects? What are the dom and bom objects? Nov 13, 2023 am 10:52 AM

There are 5 DOM objects including "document", "element", "Node", "Event" and "Window"; 2. "window", "navigator", "location" and "history" and "screen" and other 5 BOM objects.

See all articles