svg绘制圆弧(一)

转载地址:https://www.oschina.net/code/snippet_170216_54737

绘制圆弧案例:

<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <title>SVG Draw Arc</title>
  <style media="screen">
    .svgs{display: flex; flex-flow:row wrap; justify-content: space-around; width: 620px; margin: 0 auto;}
    .svgs svg{width:300px; height:200px; background: #F2F2F2; margin-bottom: 10px;}
  </style>
</head>

<body>
  <div class="svgs">
  <svg version="1.1" xmlns="http://www.w3.org/2000/svg">
    <path d="M 150 0 L 150 200 M 0 100 L 300 100" stroke="#CCC" stroke-width="1" />
    <path id="path1" d="" stroke="#000" fill="green" fill-opacity="1" />
  </svg>
  <svg version="1.1" xmlns="http://www.w3.org/2000/svg">
    <path d="M 150 0 L 150 200 M 0 100 L 300 100" stroke="#CCC" stroke-width="1" />
    <path id="path2" d="" stroke="#000" fill="green" fill-opacity="1" />
  </svg>
  <svg version="1.1" xmlns="http://www.w3.org/2000/svg">
    <path d="M 150 0 L 150 200 M 0 100 L 300 100" stroke="#CCC" stroke-width="1" />
    <path id="path3" d="" stroke="#000" fill="green" fill-opacity="1" />
  </svg>
  <svg version="1.1" xmlns="http://www.w3.org/2000/svg">
    <path d="M 150 0 L 150 200 M 0 100 L 300 100" stroke="#CCC" stroke-width="1" />
    <path id="path4" d="" stroke="#000" fill="green" fill-opacity="1" />
  </svg>
</div>
</body>
<script type="text/javascript">
  function changeArcDeg(path, d){
    path.setAttribute("d", d);
  }
  function drawArcByRadiusDeg(startX, startY, r, deg, clockwise) {
    var cw = typeof clockwise !== 'undefined' ? clockwise : 1;
    var x = startX - r + r*Math.cos(deg*Math.PI/180);
    var y = startY + (1===cw ? 1 : -1)*r*Math.sin(deg*Math.PI/180);
    var bigOrSmall = deg > 180 ? 1 : 0;
    var line = " L" + (startX - r) + " " + startY + " L"+startX + " " + startY + "Z";
    return "M " + startX +" "+ startY + " A "+ r +" " + r + " 0 " + bigOrSmall + " " +cw+ " " + x + " " + y + line;
  }
  function run(pid, deg, clockwise){
    var path = document.getElementById(pid);
    var x = 200, y = 100, r = 50, add = 0===deg ? true : false;
    setInterval(function(){
      if(deg>360) deg -= 360;
      if(deg<0) deg += 360;
      path.setAttribute("d", drawArcByRadiusDeg(x, y, r, add ? deg++ : deg--, clockwise)) ;
    }, 10);
  }
  window.onload = function(){
    run('path1', 0, 1);
    run('path2', 0, 0);
    run('path3', 360, 0);
    run('path4', 360, 1);
  };
</script>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值