溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

SVG DOM操作實(shí)例分析

發(fā)布時(shí)間:2022-03-16 16:27:48 來(lái)源:億速云 閱讀:112 作者:iii 欄目:web開(kāi)發(fā)

這篇文章主要講解了“SVG DOM操作實(shí)例分析”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“SVG DOM操作實(shí)例分析”吧!

HTML頁(yè)面中的DOM操作

DOM大家應(yīng)該很熟悉了,這里先看一個(gè)小例子:

復(fù)制代碼 代碼如下:

<head>

<style>

#svgContainer {

width: 400px;

height: 400px;

background-color: #a0a0a0;

}

</style>

<script>

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 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(1,0,0,-1,0,300)');

// 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 0, 300";

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, 'd', coords);

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

path.setAttributeNS (null, 'opacity', 1.0);

g.appendChild (path);

var svgContainer = document.getElementById ("svgContainer");

svgContainer.appendChild (svgElem);

}

</script>

</head>

<body onload="CreateSVG ()">

<div id="svgContainer"></div>

</body>

發(fā)現(xiàn)了沒(méi),與普通的html元素的DOM操作完全一樣:

選擇元素:document.getElementById

創(chuàng)建元素:document.createElementNS

創(chuàng)建子元素的另外一種方式:element.createChildNS

添加元素:node.appendChild

設(shè)置元素的屬性:element.setAttributeNS/element.setAttribute

除了上面這幾個(gè)操作,下面的操作和屬性也很常見(jiàn):

獲取元素的屬性值: element.getAttributeNS/element.getAttribute

檢查元素是否存在某屬性:element.hasAttributeNS

移除元素的某屬性:element.removeAttributeNS

父元素、子元素和兄弟節(jié)點(diǎn):element.parentNode/element.firstChild/child.nextSibling

這些方法這里不再詳細(xì)介紹了;此外,DOM樹(shù)的節(jié)點(diǎn)結(jié)構(gòu),對(duì)象之間的繼承關(guān)系也都是差不多的,就不詳述了。需要的同學(xué)參看后面的DOM Core Object的文檔。

不過(guò),需要注意的是SVG本質(zhì)上是XML文檔,所以基本采用的DOM方法都是帶NS結(jié)尾的方式,來(lái)提供相關(guān)的namespace;如果創(chuàng)建元素時(shí)已經(jīng)提供了namespace,而且沒(méi)有多個(gè)namespace的問(wèn)題,那么設(shè)置相關(guān)屬性的時(shí)候,也可以選擇使用不帶NS的版本,比如直接使用element.setAttribute設(shè)置屬性值,但是總的來(lái)說(shuō),還是強(qiáng)烈推薦使用帶NS結(jié)尾的版本,因?yàn)檫@個(gè)版本總是工作正常的,即使是在多namespace的情況下。

SVG DOM

這個(gè)與標(biāo)準(zhǔn)的DOM有哪些不同,我也沒(méi)找到什么全面的資料,目前只知道對(duì)屬性的賦值方式是不同的。如果有了解這方面的同學(xué)還請(qǐng)吱一聲啊。

上面的例子中,我們使用element.setAttributeNS/element.setAttribute來(lái)給屬性賦值,在SVG DOM中,可以使用面向?qū)ο蟮姆绞?,通過(guò)訪問(wèn)點(diǎn)號(hào)來(lái)給對(duì)象的屬性賦值,比如下面是兩種方式的對(duì)比:

普通的DOM方式:

復(fù)制代碼 代碼如下:

element.setAttribute("x", "10");

element.setAttribute("y", "20");

element.setAttribute("width", "100%");

element.setAttribute("height", "2em");

而SVG DOM的方式:

復(fù)制代碼 代碼如下:

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腳本屬于傳統(tǒng)的腳本,其特征是通過(guò)構(gòu)建“值字符串”來(lái)設(shè)置各個(gè)項(xiàng)。SVG DOM腳本樣式的優(yōu)點(diǎn)是,你不必構(gòu)建“值字符串”,所以性能優(yōu)于DOM腳本。

嵌入SVG的腳本

如果要在SVG內(nèi)部添加腳本,就需要使用script元素,這個(gè)前面已經(jīng)講過(guò)了,除了這一點(diǎn),基本上與把腳本放到外面的HTML中是一樣的??匆粋€(gè)例子:

復(fù)制代碼 代碼如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head>

</head>

<body>

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="300" height="300">

<script type="text/ecmascript">

<![CDATA[

function showRectColor() {

alert(document.getElementById("myBlueRect").getAttributeNS(null,"fill"));

}

function showRectArea(evt) {

var width = parseFloat(evt.target.getAttributeNS(null,"width"));

var height = parseFloat(evt.target.getAttributeNS(null,"height"));

alert("The rectangle area is: " + (width * height));

}

function showRootChildrenNr() {

alert("Nr of Children: "+document.documentElement.childNodes.length);

}

]]>

</script>

<g id="firstGroup">

<rect id="myBlueRect" width="100" height="50" x="40" y="20" fill="blue" onclick="showRectArea(evt)"/>

<text x="40" y="100" onclick="showRectColor()">Click on this text to show rectangle color.</text>

<text x="40" y="130">Click on rectangle to show rectangle area.</text>

<text x="40" y="160" onclick="showRootChildrenNr()">Click on this text to show the number of child

<tspan x="40" dy="20">elements of the root element.</tspan></text>

</g>

</svg>

</body>

</html>

在這個(gè)例子中,列舉了常見(jiàn)的獲取DOM對(duì)象的方式:

1. 通過(guò)document.getElementById或者document.getElementByClassName之類的方法獲取對(duì)象;

2. 通過(guò)document.documentElement或者document.rootElement獲取document對(duì)象;

3. 通過(guò)事件參數(shù)evt.target獲取產(chǎn)生事件的對(duì)象。這種方式的優(yōu)點(diǎn)就是不使用id就可以獲取到產(chǎn)生事件的對(duì)象。

其余的腳本基本和普通的DOM是一樣的。

感謝各位的閱讀,以上就是“SVG DOM操作實(shí)例分析”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)SVG DOM操作實(shí)例分析這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI