d3.js - 用单行重新创建矩形波动画

标签 d3.js svg

我有一个由许多 5px 宽的矩形组成的动画。

https://codepen.io/guanzo/pen/rmGqNP

您可以看到它表现出类似波浪的行为。这告诉我必须有一种方法用一条曲线而不是数百个矩形来表示这种运动。最有可能使用像 <path> 这样的 svg 元素或<polyline> ?我想我需要使用一些修改后的正弦波函数,但我无法理解数学。

编辑: 设法得到了polyline通过cubicInOut 缓动上下移动。

https://codepen.io/guanzo/pen/vmeoXw

仍然不知道如何对类似波浪的行为进行编程。

编辑: 我尝试使用 path和贝塞尔曲线过渡。关闭,但没有雪茄。

https://codepen.io/guanzo/pen/XRevME?editors=1010

最佳答案

对整条线应用线性变换,而不是对线的控制点进行动画处理,会更容易。这是一个简单的例子:

<svg width="100%" viewBox="0 0 100 40">
  <rect x="0" y="0" width="100" height="40" fill="#fa0" stroke="none"/>
  <path d="M0 0V10H0C20 10 50 30 80 30H120C150 30 150 10 180 10 H220C250 10 250 30 280 30H320C350 30 350 10 380 10H400V-10Z" fill="#c00" stroke="none">
    <animateTransform attributeName="transform" attributeType="XML" type="translate" from="-300 0" to="-100 0" dur="5s" repeatCount="indefinite"/>
  </path>
</svg>

关于d3.js - 用单行重新创建矩形波动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43811085/

相关文章:

javascript - mousedown 内的 mousemove 停止鼠标弹起

javascript - 拖动 D3 节点时防止单击操作

python - 从 SVG 输入生成 PDF

html - 如何在angular4中使用来自node_modules的svg图标集包?

svg - 使用 'month-year' 日期值创建带时间线的 D3.js 散点图

javascript - Webpack2 - 如何内联 SVG 文件

javascript - D3.js折线图方向错误

Javascript:加载csv文件并将其打印到页面

javascript - 用D3绘制三个相互交织的圆圈

javascript - 将图像附加到现有表头