


Introduction to SVG 2D in HTML5 12—Introduction to SVG DOM and DOM operations_html5 tutorial skills
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:
<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:
element.setAttribute("x", "10");
element.setAttribute("y", "20");
element.setAttribute("width", "100%");
element.setAttribute("height", "2em");
And the SVG DOM way:
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:
在这个例子中,列举了常见的获取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

H5 improves web user experience with multimedia support, offline storage and performance optimization. 1) Multimedia support: H5 and elements simplify development and improve user experience. 2) Offline storage: WebStorage and IndexedDB allow offline use to improve the experience. 3) Performance optimization: WebWorkers and elements optimize performance to reduce bandwidth consumption.

HTML5 code consists of tags, elements and attributes: 1. The tag defines the content type and is surrounded by angle brackets, such as. 2. Elements are composed of start tags, contents and end tags, such as contents. 3. Attributes define key-value pairs in the start tag, enhance functions, such as. These are the basic units for building web structure.

HTML5 is a key technology for building modern web pages, providing many new elements and features. 1. HTML5 introduces semantic elements such as, , etc., which enhances web page structure and SEO. 2. Support multimedia elements and embed media without plug-ins. 3. Forms enhance new input types and verification properties, simplifying the verification process. 4. Offer offline and local storage functions to improve web page performance and user experience.

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.

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.

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

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


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver CS6
Visual web development tools