svg圆环缓冲动画

时间:2022-11-20 17:09:18

代码如下

svg圆环缓冲动画

 

 

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
  #hhn {
    width: 500px;
    height: 500px;
    position: relative;
    background: rgba(0,0,0,0.5);
  }
</style>
</head>
<body>
  <div id="hhn"></div>
</body>
<script src="../libs/d3.js"></script>
<script src="../libs/jquery.js"></script>
<script>
var chartCon = $('#hhn');

var domInfo = [{
    id : 'circle1',
    value : 32,
    left : '5%',
    top : '5%',
    text : '通信'
  },{
    id : 'circle2',
    value : 68,
    left : '35%',
    top : '5%',
    color : 'rgba(199, 164, 14, 0.5)',
    text : '物流'
  },{
    id : 'circle3',
    value : 32,
    left : '65%',
    top : '5%',
    text : '半导体'
  },{
    id : 'circle4',
    value : 32,
    left : '5%',
    top : '53%',
    text : '智能制造'
  },{
    id : 'circle5',
    value : 68,
    left : '35%',
    top : '53%',
    color : 'rgba(199, 164, 14, 0.5)',
    text : '网络信息'
  },{
    id : 'circle6',
    value : 32,
    left : '65%',
    top : '53%',
    text : '其他'
  }];

  var dom;
  $(domInfo).each(function(i,v){
    chartCon.append('<div id='+v.id+' style="position:absolute;left:'+v.left+';top:'+v.top+';width:150px;height:150px;"></div>');
    dom = document.getElementById(v.id);
    $('<span>').css({
      position: 'absolute',
      width: '100%',
      height: '20%',
      textAlign:'center',
      color: '#bbbcc0',
      fontSize: 20,
      top: '105%'
    }).text(v.text).appendTo($(dom));
      buildCircle(dom,v.value,v.color,v.id);
    });
    function buildCircle(el, value, vColor,vId) {

      if (!d3.arc) d3.arc = d3.svg.arc;
      if (!d3.pie) d3.pie = d3.layout.pie;

      var svg = d3.select(el).append('svg')//给每个div添加svg元素
        .attr('width', '100%')
        .attr('height', '100%')
        .attr('viewBox', '0 0 100 100');

      var defs = svg.append('defs');

      var id = 'color_' + Math.random();
      var linear = defs.append('linearGradient')
        .attr('x1', 0)
        .attr('y1', 0)
        .attr('x2', 0)
        .attr('y2', 1)
        .attr('id', id);

      linear.append('stop')
        .attr('stop-color', vColor || 'rgba(2,135,188,0.5)')
        .attr('offset', 0);

      linear.append('stop')
        .attr('stop-color', vColor || 'rgba(2,135,188,0.5)')
        .attr('offset', 1);

      var imgSrc = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAI8AAACPCAYAAADDY4iTAAABS2lUWHRYTUw6Y29tLmFkb2JlLnhtcAAAAAAAPD94cGFja2V0IGJlZ2luPSLvu78iIGlkPSJXNU0wTXBDZWhpSHpyZVN6TlRjemtjOWQiPz4KPHg6eG1wbWV0YSB4bWxuczp4PSJhZG9iZTpuczptZXRhLyIgeDp4bXB0az0iQWRvYmUgWE1QIENvcmUgNS42LWMxMzggNzkuMTU5ODI0LCAyMDE2LzA5LzE0LTAxOjA5OjAxICAgICAgICAiPgogPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4KICA8cmRmOkRlc2NyaXB0aW9uIHJkZjphYm91dD0iIi8+CiA8L3JkZjpSREY+CjwveDp4bXBtZXRhPgo8P3hwYWNrZXQgZW5kPSJyIj8+IEmuOgAADk1JREFUeJztnfmTVNUVxz/9GGZQGILOALIoSBAV3FkMoEASJGrAuMQ9akUrasqy8m+k8kOqkmi0TFxTMSqJUbHKNRpFIUHckBhAmKgjogwTUJjM3p0fTj+mbXs59/V9/Za+n6quYYb7Xt/u/vZdzjn3nEwul6PBGA20A235n98AxgGtwFigJd9uDDAKGAL683/rBQ7lHz3AfmAf0A38N9+2YWiKugMhcwTwTWAmMC3/mAh4ITzXMPA5sBv4BOgAPmJEeKkjk7KRpwU4CZgHnABMATIR9icLdAIfAP/K/xyMsD9WSYN4xgNnAWcCs4n3aDoAbAPeBN5FpsHEklTxtADzgbOBOYQzDYXNEPA+sBER0nC03TEnaeKZDiwHFiLrmbRwENgAvAbsjbgvapIgngyyhlkJnBxxX8ImB7wDvADsirgvVYmzeDLAG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EUcwxiK5qL5KQJGgtjkx7EQLkVsXhrC8PEnrSJpxAPCW+YhYxO0xBjnm0XQSH9yMjyCWLN3okYI1P5JqdZPKXwEAPkREaswxMQU4D/8N0kRyDTYhYp3AFidT7EiCV6PyNW6q78o2He0P8DTEweItrFFIYAAAAASUVORK5CYII=';

      svg.append('image')
        .attr('width', 100)
        .attr('height', 100)
        .attr('x', 0)
        .attr('y', 0)
        .attr('xlink:href', imgSrc);


      var data = [value, 100 - value];//圆环的数据

      var pie = d3.pie()
        .startAngle(0)
        .sort(function(d, i) {
          return d == value ? 1 : -1;
         })
        .endAngle(-Math.PI * 2);

      var arcData = pie(data);//生成圆环数据的数组对象

      var arc = d3.arc()
        .innerRadius(39)
        .outerRadius(50);
        //.cornerRadius(6);

      var group = svg.append('g')
        .attr('transform', 'translate(50, 50)');
        group.selectAll('path')
        .data([arcData[0]])
        .enter()
        .append('path')
        .attr('class','path' + vId)
        .style('fill', 'rgba(0,0,0,0)')
        .attr('d', arc);
       var i = 0;

      function move(){
        if(i < 100){
          requestAnimationFrame(move);
        }else {
          return;
        }
      $('.path' + vId).remove();//每次生成的时候要先删除原来的元素
      i+=1;
      data = [value * i / 100, 100 - value * i / 100];
      pie = d3.pie()
        .startAngle(0)
        .sort(function(d, k) {
          return d == value * i / 100 ? 1 : -1;
        })
        .endAngle(-Math.PI * 2);
      arcData = pie(data);
      group.selectAll('path')
        .data([arcData[0]])
        .enter()
        .append('path')
        .attr('class','path' + vId)
        .style('fill', 'url(#' + id + ')')
        .attr('d', arc);
      text.text(value * i / 100 + '%')
      }

      var text = group.append('text')
        .text(value + '%')
        .attr('class','cirtext')
        .attr('x', '0')
        .attr('y', '0')
        .attr('text-anchor', 'middle')
        .attr('dy', '0.5em')
        .style('fill', '#ffffff')
        .style('cursor', 'pointer')
        .style('font-size', 27);
       move();
      $('.cirtext').hover(function(){
          $(this).css('transform','scale(1.2)')
        },function(){
          $(this).css('transform','scale(1)')
        });
    }
</script>
</html>