用SVG生成圆形按钮

时间:2016-10-07 15:47:25

标签: javascript svg

我正在生成SVG路径以用作按钮来表示一个月内的天数。我的发电机工作正常。我喂它28,它产生适当数量的路径。唯一的问题是我希望我的路径从90度开始,这样我的第一个生成路径(按钮)将位于顶部而不是右侧。

我当前的生成器工作here

这是一个视觉效果:

enter image description here

HTML

<div class="column column-three">
      <div class="dial-label">DAY</div>
      <div class="full-circle" id="day"></div>
</div>

JS生成器代码

var textCircleRadius = 35
createDial('#day',31,'28');

function createDial(selection,numOfButtons,circleText){
    var svg = d3.select(selection)
        .append('svg')
        .attr('viewBox', '0 0 110 110');

        svg.selectAll('path')
          .data(createDialPath(numOfButtons))
          .enter()
          .append('path')
          .attr('d', function(d){
            return d;
          })
          .attr('class','dial-button')
          .attr('id', function(d,i){ return 'd' + i;})
          .on('click',function(event){
            console.log(this);
          });

        svg.append('circle')
          .attr('cx',55)
          .attr('cy',55)
          .attr('r',textCircleRadius)
          .attr('class','mid-circle');

        svg.append('text')
            .attr('x','50%')
            .attr('y','50%')
            .attr('class', 'mid-circle-text')
            .attr('id','mid-circle-text')
            .attr('dy', '.3em')
            .html(circleText);

      return svg;
    }

我尝试使用一些数学运算来使其生成不同,并尝试重新组织我创建的路径数组以更改生成顺序,但无法找出可以为任何数字动态工作的解决方案路径。

1 个答案:

答案 0 :(得分:4)

我可能会误解,但只需从createDialPath()中删除90度即可。

afD = i*degrees - degrees - 90;
atD = i*degrees - 90;

请在此处查看以下更改:

&#13;
&#13;
var textCircleRadius = 35
createDial('#day', 31, '28');

function createDial(selection, numOfButtons, circleText) {
  var svg = d3.select(selection)
    .append('svg')
    .attr('viewBox', '0 0 110 110');

  svg.selectAll('path')
    .data(createDialPath(numOfButtons))
    .enter()
    .append('path')
    .attr('d', function(d) {
      return d;
    })
    .attr('class', 'dial-button')
    .attr('id', function(d, i) {
      return 'd' + i;
    })
    .on('click', function(event) {
      console.log(this);
    });

  svg.append('circle')
    .attr('cx', 55)
    .attr('cy', 55)
    .attr('r', textCircleRadius)
    .attr('class', 'mid-circle');

  svg.append('text')
    .attr('x', '50%')
    .attr('y', '50%')
    .attr('class', 'mid-circle-text')
    .attr('id', 'mid-circle-text')
    .attr('dy', '.3em')
    .html(circleText);

  return svg;
}

function createDialPath(numOfSegments) {
  // defaults
  var degrees = 360 / numOfSegments,
    text = 35,
    r = 50,
    xC = 55,
    yC = 55,
    pathString = [];

  // dynamic values
  var afD,
    atD,
    afR,
    atR,
    xF,
    yF,
    xT,
    yT,
    d;
  for (var i = 1; i <= numOfSegments; i++) {
    afD = i * degrees - degrees - 90;
    atD = i * degrees - 90;
    afR = afD * (Math.PI / 180);
    atR = atD * (Math.PI / 180);
    xF = ((r * Math.cos(afR)) + xC).toFixed(2);
    yF = ((r * Math.sin(afR)) + yC).toFixed(2);
    xT = ((r * Math.cos(atR)) + xC).toFixed(2);
    yT = ((r * Math.sin(atR)) + yC).toFixed(2);
    d = ['M', xC, ',', yC, ' ', 'L', xF, ',', yF, ' ', 'A', r, ',', r, ' 0 0,1 ', xT, ',', yT, 'z'].join('');
    pathString.push(d);
  }
  return pathString;
}
&#13;
.full-circle {
  /*background-color: #c06;*/
  height: 100px;
  -moz-border-radius: 50px;
  -webkit-border-radius: 50px;
  width: 100px;
  display: block;
  position: relative;
  margin: 6px auto;
}
.dial-label {
  text-align: center;
  color: #42BBAA;
  font-weight: 100;
}
span.dial-selection-label {
  color: #777777;
  text-transform: uppercase;
  margin-top: 30px;
  display: block;
  font-size: 22px;
  font-weight: 100;
  text-align: center;
}
path {
  stroke: white;
  stroke-width: 2px;
}
circle {
  fill: yellowgreen;
  stroke: black;
}
.dial-button {
  fill: #E7E6E6;
}
.dial-button.active-month {
  fill: #78AE06;
}
.dial-button.active-season {
  fill: #EEA124;
}
.mid-circle {
  fill: #FFFFFF;
  stroke: white;
  stroke-width: 2px;
}
.mid-circle-text {
  text-anchor: middle;
  stroke: #7A7A7A;
  font-size: 19px;
  /*stroke-width: .2px;*/
  font-weight: 100;
}
&#13;
<script src="https://d3js.org/d3.v4.min.js"></script>
<div class="column column-three">
  <div class="dial-label">DAY</div>
  <div class="full-circle" id="day"></div>
</div>
&#13;
&#13;
&#13;