javascript - d3.js 形状动画的技巧

标签 javascript d3.js

您可以看到我在 this 中的工作示例

我怎样才能让它不那么生涩?

代码的工作原理如下:

我有一个通过 requestAnimationFrame 调用自身的方法

  animateCircle(state, direction) {
    this.drawSineGraph(state, direction);

    requestAnimationFrame(this.animateCircle.bind(this, state, direction));
  }

这又调用了一个drawSineGraph函数:

  drawSineGraph(state, direction) {
    d3.select('.sine-curve').remove();

    const increase = 54 / 1000;

    state.sineIncrease = state.sineIncrease || 0;

    state.sineIncrease += increase;

    const sineData = d3.range(0, state.sineIncrease)
            .map(x => x * 10 / 85)
            .map((x) => {
              return {x: x, y: Math.sin(x)};
            });

    state.nextCoord = {x: state.xScale(_.last(sineData).x), y: state.yScale(Math.sin(_.last(sineData).y) + 1)};

    const sine = d3.svg.line()
            .interpolate('monotone')
            .x( (d) => {return state.xScale(d.x);})
            .y( (d) => {return state.yScale(d.y + 1);});

    state.xAxisGroup.append('path')
      .datum(sineData)
      .attr('class', 'sine-curve')
      .attr('d', sine);
  }

它增加了一个计数器并将正弦波绘制到该点,但效果非常不稳定。

如何在正弦波扩展时实现平滑运动?

最佳答案

看看生成代码,您一遍又一遍地重新创建每个曲线段。重叠超过 100 条曲线。您必须在上一个段的末尾添加一个段。问题在于:

 animateCircle(state, direction) {
    this.drawSineGraph(state, direction); // <--recreate the curve from the origin

    requestAnimationFrame(this.animateCircle.bind(this, state, direction)); 
  }

关于javascript - d3.js 形状动画的技巧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37688645/

相关文章:

javascript - D3.js:如何在版本 4 中将分布线添加到直方图中

javascript - d3 js 运行不正常

javascript - 如何在点上应用渐变蒙版

javascript - 将 DOM 元素附加到 D3

javascript - 显示 data-id 到 <select> Bootstrap 模式

javascript - 减少 jQuery 彩色动画的加载时间

javascript 在新选项卡中打开所有 html 链接?

javascript - Google map 插件显示为灰色框(MVC 环境)

javascript - 从空格键中的模板事件监听器访问 _id #each 循环

javascript - 使用边界矩形获取 d3 元素的高度/宽度时如何考虑比例