SVG学习——6.实现圆与中心圆连接

11 篇文章 0 订阅

跟着视频做的,对方数据是车子。我随便改了一下,要贴合最近的心情。
效果图如下:
在这里插入图片描述
代码如下:

<!DOCTYPE html>
<html>
<head>
<title></title>
<style>
#div1 {width:780px; height:400px; background:url(img/bg.jpg) no-repeat;  margin:20px auto; overflow:hidden; }
body {background:black; }
</style>
<script>

<!--svg中创建标签的方法-->
window.onload=function(){
  var svgNS='http://www.w3.org/2000/svg';   //命名空间
  var oParent=document.getElementById('div1');   //获取父节点 才能添加到页面中
  var centerX=oParent.offsetWidth/2;   //中心点横坐标
  var centerY=oParent.offsetHeight/2;   //中心点纵坐标

//数据与操作分离
  var data={
    centerNode: {text:'心情如何'},
    otherNode: [
      { x:100, y:100 , text:'不开心'},
      { x:200, y:50 , text:'一般般'},
      { x:600, y:150 , text:'糟透了'},
      { x:650, y:300 , text:'垂头丧气'},
    ]
  };

  function createTag(tag, objAttr){       //封装一个创建标签的函数 这里取名为createTag
      var oTag=document.createElementNS(svgNS, tag);
      for(var attr in objAttr){
         oTag.setAttribute(attr, objAttr[attr]);    //设置属性
      }
      return oTag;
  }
  
//中间圆
  function addTag(){
     var oSvg=createTag('svg', {'xmlns':svgNS, 'width':'100%', 'height':'100%' });
     
     for(var i=0;i<data.otherNode.length;i++){
        addOtherTag(data.otherNode[i], oSvg);
     }

     var oG=createTag('g', {'style':'cursor:pointer'});
     var oCircle=createTag('circle', {'cx':centerX, 'cy':centerY, 'r':'60'  ,'fill':'white', 'stroke':'red', 'stroke-width':'1'});
     var oText=createTag('text', {'x':centerX, 'y':centerY+8, 'font-size':'20', 'text-anchor':'middle' });
     oText.innerHTML=data.centerNode.text;

     oG.appendChild(oCircle);  //添加到oG
     oG.appendChild(oText);  //添加到oG
     oSvg.appendChild(oG);  //添加到oSvg
     oParent.appendChild(oSvg);  //添加到oParent
  }
  //调用一下
  addTag();

//其他圆和线
  function addOtherTag(otherAttr, oSvg){
   //创建线
    var oG=createTag('g', {'style':'cursor:pointer'});
    var oLine1=createTag('line', {'x1':otherAttr.x, 'y1':otherAttr.y, 'x2':centerX, 'y2':centerY, 'stroke':'#ccc'});
    var oLine2=createTag('line', {'x1':otherAttr.x, 'y1':otherAttr.y, 'x2':centerX, 'y2':centerY, 'stroke':'transparent', 'stroke-width':'10'});
    var oRect=createTag('rect', {'x':(centerX+otherAttr.x)/2-10, 'y':(centerY+otherAttr.y)/2-10, 'fill':'#999', 'width':'20', 'height':'20', 'rx':'5'});
    var oText=createTag('text', {'x':(centerX+otherAttr.x)/2, 'y':(centerY+otherAttr.y)/2+8, 'fill':'white', 'font-size':'20', 'text-anchor':'middle' });
    oText.innerHTML='?';  //添加文字

    oG.appendChild(oLine1);  //添加到oG
    oG.appendChild(oLine2);  //添加到oG
    oG.appendChild(oRect);  //添加到oG
    oG.appendChild(oText);  //添加到oG
    oSvg.appendChild(oG);  //oG添加到oSvg
  
   //创建圆 
    var oG=createTag('g', {'style':'cursor:pointer'});
    var oCircle=createTag('circle', {'cx':otherAttr.x, 'cy':otherAttr.y, 'r':'40'  ,'fill':'white', 'stroke':'red', 'stroke-width':'1'});
    var oText=createTag('text', {'x':otherAttr.x, 'y':otherAttr.y+8, 'font-size':'20', 'text-anchor':'middle' });
    oText.innerHTML=otherAttr.text; //文字是传过来的otherAttr的text

    oG.appendChild(oCircle);  //添加到oG
    oG.appendChild(oText);  //添加到oG
    oSvg.appendChild(oG);  //添加到oSvg



  }


}  
</script>
</head>
<body>
<!--直接写svg的方法-->
<div id="div1">

</div>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值