javascript - 在 SVG 中绘制 "squiggly line"

标签 javascript svg

我正在尝试研究如何在任意 SVG path 元素上绘制波浪线。该路径由 React 组件生成。例如,我正在尝试复制 in this question 行:

Squiggly Line Example

在 SVG 和/或 JavaScript 生成的路径中是否有一种简单的方法可以做到这一点?

我考虑过使用 s 路径命令连接一系列曲线,但随后我需要计算沿曲线的点。我也考虑过某种位移过滤器,但我不确定从哪里开始。

最佳答案

在我看来,最简单的方法就是沿着这条路走下去。然后,在每一步中,插入一个二次贝塞尔曲线,控制点位于它们之间的一半并垂直于曲线。然后在下一步中切换控制点所在的一侧。

function makeSquiggle(squigglePathId, followPathId, squiggleStep, squiggleAmplitude)
{
  var followPath = document.getElementById(followPathId);
  var pathLen = followPath.getTotalLength();

  // Adjust step so that there are a whole number of steps along the path
  var numSteps = Math.round(pathLen / squiggleStep);

  var pos = followPath.getPointAtLength(0);
  var newPath = "M" + [pos.x, pos.y].join(',');
  var side = -1;
  for (var i=1; i<=numSteps; i++)
  {
    var last = pos;
    var pos = followPath.getPointAtLength(i * pathLen / numSteps);

    // Find a point halfway between last and pos. Then find the point that is
    // perpendicular to that line segment, and is squiggleAmplitude away from
    // it on the side of the line designated by 'side' (-1 or +1).
    // This point will be the control point of the quadratic curve forming the
    // squiggle step.
    
    // The vector from the last point to this one
    var vector = {x: (pos.x - last.x),
                  y: (pos.y - last.y)};
    // The length of this vector
    var vectorLen = Math.sqrt(vector.x * vector.x + vector.y * vector.y);
    // The point halfwasy between last point and tis one
    var half = {x: (last.x + vector.x/2),
                y: (last.y + vector.y/2)};
    // The vector that is perpendicular to 'vector'
    var perpVector = {x: -(squiggleAmplitude * vector.y / vectorLen),
                      y: (squiggleAmplitude * vector.x / vectorLen)};
    // No calculate the control point position
    var controlPoint = {x: (half.x + perpVector.x * side),
                        y: (half.y + perpVector.y * side)};
    newPath += ("Q" + [controlPoint.x, controlPoint.y, pos.x, pos.y].join(','));
    // Switch the side (for next step)
    side = -side;
  }
  var squigglePath = document.getElementById(squigglePathId);
  squigglePath.setAttribute("d", newPath);
}


makeSquiggle("squiggle", "follow", 25, 20);
#follow {
  fill: none;
  stroke: grey;
  stroke-width: 2;
}

#squiggle {
  fill: none;
  stroke: red;
  stroke-width: 2;
}
<svg width="500" height="400">
  <path id="follow" d="M 50,300 C 100,100 300,0, 350,250 L 450,200"/>
  <path id="squiggle" d="M0,0"/>
</svg>

关于javascript - 在 SVG 中绘制 "squiggly line",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42441472/

相关文章:

javascript - AngularJS 在页面上显示 svg 文件

javascript - 流播放器 + RTMP

javascript - 如何在 JavaScript 文件中使用 Django 变量?

javascript - 激活引导折叠时切换内联 V 形向下

javascript - slider 无法正常工作,并且出现 TypeError : jQuery(. ..).easyResponsiveTabs 不是函数

javascript - 仅将输入字段中每个单词的第一个字母自动大写

css - 为 SVG 渐变条设置动画,使其看起来像填充了停止色

javascript - svg - 获取重叠形状区域的路径坐标

javascript - 将元素 (SVG) 传递给网络 worker

html - 是否可以在 SVG 中创建发光效果