欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-09-08 11:32:46
前面的话 本文将详细介绍svg基本操作api,并使用这些api制作实例效果 基础api 在javascript中,可以使用一些基本的api来对svg进行操作 【ns...

前面的话

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

基础api

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

【ns地址】

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

有两个常用的ns地址

var svg_ns = "http://www.w3.org/2000/svg";
var xlink_ns = http://www.w3.org/1999/xlink;

【创建图形】

document.createelementns(svg_ns,tagname);

【添加图形】

element.appendchild(childelement)

【设置/获取属性】

element.setattribute(name,value);
element.getattribute(name);

【设置xlink】

<a>、<textpath>等标签需要设置xlink属性

element.setattributens(xlink_ns,'xlink:href',value);

封装函数

将创建标签及添加属性的操作封闭成一个函数,方便复用

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;
}

下面通过该函数,创建一个圆形

<script>
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;
} 
var osvg = createtag('svg',{'version':'1.1','xmls':'http://www.w3.org/2000/svg',height:'70'});
var ocircle = createtag('circle',{'cx':'25','cy':'25','r':20,'fill':'pink'});
osvg.appendchild(ocircle);
document.body.appendchild(osvg);
</script>

实例

下面通过svg基本操作api,创建一个可交互的svg实例

<style>
#box{
 height: 300px;
 width: 300px;
 background: hsl(20,40%,90%);
 background-image:linear-gradient(90deg,#fb3 11px,transparent 0),
 linear-gradient(90deg,#ab4 23px,transparent 0),
 linear-gradient(90deg,#655 41px,transparent 0);
 background-size: 41px 100%,61px 100%,83px 100%; 
} 
</style>
<div id="box"></div>
<script>
var osvg = createtag('svg',{'version':'1.1','xmls':'http://www.w3.org/2000/svg',height:'100%',width:'100%'});
var obox = document.getelementbyid('box');
var w = parseint(getcomputedstyle(obox).width);
var h = parseint(getcomputedstyle(obox).height);
var appearance = {
 'dis':h/3,
 'r0':h/8,'r':h/10,
 'x0':w/2,'y0':h/2,
 'fontsize':h/20,
 'bg0':'hsla(0,0%,100%,.6)','bg':'hsla(0,0%,100%,.3)',
 'color0':'hsl(210,13%,30%)','color':'hsl(210,13%,30%)',
 'bordercolor0':'rgba(0,0,0,0.3)','bordercolor':'rgba(0,0,0,0.3)',
 'strokwidth0':3,'strokwidth':2,
 'linecolor':'rgba(0,0,0,0.3)','linewidth':1,'linedashed':'5,5'
}
var data = {
 text:'前端开发',
 children:[
  {text:'html',url:'http://www.cnblogs.com/xiaohuochai/p/5203223.html'},
  {text:'css',url:'http://www.cnblogs.com/xiaohuochai/p/5249139.html'},
  {text:'js',url:'http://www.cnblogs.com/xiaohuochai/p/5613593.html'},
  {text:'es6',url:'http://www.cnblogs.com/xiaohuochai/p/7233392.html'},
  {text:'http',url:'http://www.cnblogs.com/xiaohuochai/p/6392010.html'},
  {text:'nodejs',url:'http://www.cnblogs.com/xiaohuochai/p/6940560.html'},
  {text:'前端工具',url:'http://www.cnblogs.com/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的实例讲解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。