Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Analysis of SVG DOM Operation examples

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains the "SVG DOM operation case analysis", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "SVG DOM operation case analysis" bar!

DOM actions in HTML pages

DOM should be familiar to all of you. Here's a small example:

The copy code is as follows:

# svgContainer {

Width: 400px

Height: 400px

Background-color: # a0a0a0

}

Function CreateSVG () {

Var xmlns = "http://www.w3.org/2000/svg";

Var boxWidth = 300

Var boxHeight = 300

Var svgElem = document.createElementNS (xmlns, "svg")

SvgElem.setAttributeNS (null, "viewBox", "0" + boxWidth + "" + boxHeight)

SvgElem.setAttributeNS (null, "width", boxWidth)

SvgElem.setAttributeNS (null, "height", boxHeight)

SvgElem.style.display = "block"

Var g = document.createElementNS (xmlns, "g")

SvgElem.appendChild (g)

G.setAttributeNS (null, 'transform',' matrix)

/ / draw linear gradient

Var defs = document.createElementNS (xmlns, "defs")

Var grad = document.createElementNS (xmlns, "linearGradient")

Grad.setAttributeNS (null, "id", "gradient")

Grad.setAttributeNS (null, "x1", "0%")

Grad.setAttributeNS (null, "x2", "0%")

Grad.setAttributeNS (null, "Y1", "100%")

Grad.setAttributeNS (null, "Y2", "0%")

Var stopTop = document.createElementNS (xmlns, "stop")

StopTop.setAttributeNS (null, "offset", "0%")

StopTop.setAttributeNS (null, "stop-color", "# ff0000")

Grad.appendChild (stopTop)

Var stopBottom = document.createElementNS (xmlns, "stop")

StopBottom.setAttributeNS (null, "offset", "100%")

StopBottom.setAttributeNS (null, "stop-color", "# 0000ff")

Grad.appendChild (stopBottom)

Defs.appendChild (grad)

G.appendChild (defs)

/ / draw borders

Var coords = "M 0,0"

Coords + = "l 0300"

Coords + = "l 300,0"

Coords + = "l 0,-300"

Coords + = "l-300,0"

Var path = document.createElementNS (xmlns, "path")

Path.setAttributeNS (null, 'stroke', "# 000000")

Path.setAttributeNS (null, 'stroke-width', 10)

Path.setAttributeNS (null, 'stroke-linejoin', "round")

Path.setAttributeNS (null, 'dumped, coords)

Path.setAttributeNS (null, 'fill', "url (# gradient)")

Path.setAttributeNS (null, 'opacity', 1.0)

G.appendChild (path)

Var svgContainer = document.getElementById ("svgContainer")

SvgContainer.appendChild (svgElem)

}

Click on this text to show the number of child

Elements of the root element.

In this example, the common ways to get DOM objects are listed:

1. Get the object through methods such as document.getElementById or document.getElementByClassName

two。 Get the document object through document.documentElement or document.rootElement

3. Get the object that generated the event through the event parameter evt.target. The advantage of this approach is that you can get the object that generated the event without using id.

The rest of the scripts are basically the same as normal DOM.

Thank you for your reading, the above is the content of "SVG DOM operation example analysis", after the study of this article, I believe you have a deeper understanding of the problem of SVG DOM operation example analysis, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report