Fill a percentage of an SVG and animate the fill

前端 未结 4 1557
暗喜
暗喜 2021-01-05 05:28

Currently, I am working on a project that is comparing state data with data from another country. One data point is percentage of protected land and I want to fill the a per

4条回答
  •  余生分开走
    2021-01-05 06:03

    ProgressBar looks promising and easy to use: https://kimmobrunfeldt.github.io/progressbar.js/

    Here's a nice Fiddle example: https://jsfiddle.net/kimmobrunfeldt/72tkyn40/

    Javascript:

    // progressbar.js@1.0.0 version is used
    // Docs: http://progressbarjs.readthedocs.org/en/1.0.0/
    
    var bar = new ProgressBar.Circle(container, {
      color: '#aaa',
      // This has to be the same size as the maximum width to
      // prevent clipping
      strokeWidth: 4,
      trailWidth: 1,
      easing: 'easeInOut',
      duration: 1400,
      text: {
        autoStyleContainer: false
      },
      from: { color: '#aaa', width: 1 },
      to: { color: '#333', width: 4 },
      // Set default step function for all animate calls
      step: function(state, circle) {
        circle.path.setAttribute('stroke', state.color);
        circle.path.setAttribute('stroke-width', state.width);
    
        var value = Math.round(circle.value() * 100);
        if (value === 0) {
          circle.setText('');
        } else {
          circle.setText(value);
        }
    
      }
    });
    bar.text.style.fontFamily = '"Raleway", Helvetica, sans-serif';
    bar.text.style.fontSize = '2rem';
    
    bar.animate(1.0);  // Number from 0.0 to 1.0
    

提交回复
热议问题