我正在尝试研究如何在任意SVG path元素上绘制一条波浪线.路径由React组件生成.例如,我试图在这个问题中复制该行:

在SVG和/或JavaScript生成的路径中有一种简单的方法吗?
我已经考虑过使用spath命令连接一系列曲线,但是我需要沿曲线计算点数.我也考虑过某种位移滤波器,但我不确定从哪里开始.
在我看来,最简单的方法就是走这条路.然后,在每个步骤中,插入一个二次贝塞尔曲线,其控制点位于它们之间并垂直于曲线.然后,为下一步切换控制点所在的一侧.
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);Run Code Online (Sandbox Code Playgroud)
#follow {
fill: none;
stroke: grey;
stroke-width: 2;
}
#squiggle {
fill: none;
stroke: red;
stroke-width: 2;
}Run Code Online (Sandbox Code Playgroud)
<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>Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1902 次 |
| 最近记录: |