使用SVG基本操作API的实例讲解

时间:2021-05-25

前面的话

本文将详细介绍SVG基本操作API,并使用这些API制作实例效果

基础API

在javascript中,可以使用一些基本的API来对SVG进行操作

【NS地址】

因为SVG定义在其自身的命令空间下,而不是HTML的命名空间下,可以作为单独的XML文件存在。所以需要使用自身的NS地址

有两个常用的NS地址

var SVG_NS = "http:///xiaohuochai/p/6666415.html'}, ], }addChildenTags();addCenterTag();oBox.appendChild(oSvg);function createTag(tag,objAttr){ var oTag = document.createElementNS('http://www.w3.org/2000/svg',tag); for(var attr in objAttr){ if(attr == 'xlink:href'){ oTag.setAttributeNS("http://www.w3.org/1999/xlink",attr,objAttr[attr]); }else{ oTag.setAttribute(attr,objAttr[attr]); } } return oTag;} function addCenterTag(){ var oG = createTag('g',{'style':'cursor:default'}); var oCircle = createTag('circle',{'cx':appearance.x0,'cy':appearance.y0,'r':appearance.r0,'fill':appearance.bg0,'stroke':appearance.borderColor0,'stroke-width':appearance.strokWidth0}); var oText = createTag('text',{'x':appearance.x0,'y':appearance.y0,'font-size':appearance.fontSize,'text-anchor':'middle','dominant-baseline':'middle','stroke-width':appearance.strokWidth0}); oText.innerHTML = data.text; oG.appendChild(oCircle); oG.appendChild(oText); oSvg.appendChild(oG);}function addChildenTags(){ var children = data.children; var length = children.length; var deg = (360/length)*(2*Math.PI)/360; for(var i = 0; i < children.length;i++){ var cos = Math.cos(deg*i - 90); var sin = Math.sin(deg*i - 90); var x = appearance.x0 + appearance.dis*cos; var y = appearance.y0 + appearance.dis*sin; var oA = createTag('a',{'xlink:href':children[i].url,'target':'_black'}); var oG = createTag('g',{'style':'cursor:pointer'}); oG.index = i; var oLine = createTag('line',{'x1':x-appearance.r*cos,y1:y-appearance.r*sin,x2:appearance.x0+appearance.r0*cos,y2:appearance.y0+appearance.r0*sin,'stroke':appearance.lineColor,'stroke-width':appearance.lineWidth,'stroke-dasharray':appearance.lineDashed,'style':'transition:.5s'}); var oLineHelper = createTag('line',{'x1':x-appearance.r*cos,y1:y-appearance.r*sin,x2:appearance.x0+appearance.r0*cos,y2:appearance.y0+appearance.r0*sin,'stroke':'transparent','stroke-width':10}); var oCircle = createTag('circle',{'cx':x,'cy':y,'r':appearance.r,'fill':appearance.bg,'stroke':appearance.bordercolor,'stroke-width':appearance.strokWidth,'class':'frostedglass'}); var oText = createTag('text',{'x':x,'y':y,'font-size':appearance.fontSize,'text-anchor':'middle','dominant-baseline':'middle','stroke-width':appearance.strokWidth,fill:appearance.color}); oText.innerHTML = children[i].text; oG.appendChild(oLine); oG.appendChild(oLineHelper); oG.appendChild(oCircle); oG.appendChild(oText); oA.appendChild(oG); oSvg.appendChild(oA); oG.onmouseenter = function(){ elasticMove(this,appearance.r*1.2); var line = this.children[0]; line.removeAttribute('stroke-dasharray'); line.setAttribute('stroke-width',appearance.lineWidth*3); line.setAttribute('x1',appearance.x0+(appearance.dis - appearance.r0)*Math.cos(deg*this.index - 90)); line.setAttribute('y1',appearance.y0+(appearance.dis - appearance.r0)*Math.sin(deg*this.index - 90)); } oG.onmouseleave = function(){ elasticMove(this,appearance.r); var line = this.children[0]; line.setAttribute('stroke-width',appearance.lineWidth); line.setAttribute('stroke-dasharray',appearance.lineDashed); line.setAttribute('x1',appearance.x0+(appearance.dis - appearance.r)*Math.cos(deg*this.index - 90)); line.setAttribute('y1',appearance.y0+(appearance.dis - appearance.r)*Math.sin(deg*this.index - 90)); } }}function elasticMove(obj,str){ var circle = obj.getElementsByTagName('circle')[0]; var r0 = circle.getAttribute('r'); var r = Number(str); //声明步长值 var step = 0; //声明弹性距离 var len = r - r0; //声明弹性系数 var k = 0.7; //声明损耗系数 var z= 0.7; //声明当前值 var cur = r0; clearInterval(circle.timer); circle.timer = setInterval(function(){ //获取当前值cur cur = circle.getAttribute('r'); //更新弹性距离 len = r - cur; //弹力影响 step += len*k; //阻力影响 step = step*z; //赋值 circle.setAttribute('r',Number(cur) + step); //当元素的步长值接近于0,并且弹性距离接近于0时,停止定时器 if(Math.round(step) == 0 && Math.round(len) == 0){ circle.setAttribute('r',r); clearInterval(circle.timer); } },30);}</script>

以上这篇使用SVG基本操作API的实例讲解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章