Re:Draw SVG rope using JavaScript
by xg15 on Saturday December 31, 2022 @12:27PM
(#34198541)
I wonder, could you turn this into a sort if generalized version using coordinate space transformations? Sort of like this:
- design a rope pattern as you like (or any other pattern) as if it were perfectly straight - e.g. design your pattern as if the X axis were the path and the Y axis were the normals.
- now take your actual (non-straight) path, divide it into sections and calculate the normals just as described in the post. Don't do any skewing.
For now, I assume the path has a fixed length, so the length of your pattern must be the same as the length of the path.
- divide the pattern into the same number of sections as the path has.
- now you have a 1:1 correspondence between sections on the path and sections on the pattern. Each section begins and ends with a normal perpendicular to the length of the path or pattern. The only difference is that the pattern's sections are rectangular while the path's sections are trapezoids.
- ...well, not quite: The "x axis" of a path section is the curve of the path itself, so in theory, it can perform arbitrary bends within the section. However, you can take the two points where the normals intersect the path and draw a straight line between them to get an actual trapezoid. If your straight line differs too much from the path curve, you have to increase the number of sections and make the individual sections smaller.
- now you have n trapezoidal sections on one side and n rectangular sections on the other side. You can map the coordinate systems between them with a linear transformation.
- finally, you can take the vectors that make up your rope pattern and transformation them into the coordinate systems of the path, section by section. Now your previously straight pattern follows the path!
Caveats:
1) This assumes your path has a fixed length and your pattern is defined over the entire length. It would probably be more practical to have a repeating pattern or a function that can generate a pattern given the desired path length.
2) Each section is transformed individually, so if in the pattern, you have a straight line that crosses a section boundary, the line will have a visible kink after transformation. A way to mitigate this would be to use multiple overlapping sets of sections and interpolate the transformation between them.
- design a rope pattern as you like (or any other pattern) as if it were perfectly straight - e.g. design your pattern as if the X axis were the path and the Y axis were the normals.
- now take your actual (non-straight) path, divide it into sections and calculate the normals just as described in the post. Don't do any skewing.
For now, I assume the path has a fixed length, so the length of your pattern must be the same as the length of the path.
- divide the pattern into the same number of sections as the path has.
- now you have a 1:1 correspondence between sections on the path and sections on the pattern. Each section begins and ends with a normal perpendicular to the length of the path or pattern. The only difference is that the pattern's sections are rectangular while the path's sections are trapezoids.
- ...well, not quite: The "x axis" of a path section is the curve of the path itself, so in theory, it can perform arbitrary bends within the section. However, you can take the two points where the normals intersect the path and draw a straight line between them to get an actual trapezoid. If your straight line differs too much from the path curve, you have to increase the number of sections and make the individual sections smaller.
- now you have n trapezoidal sections on one side and n rectangular sections on the other side. You can map the coordinate systems between them with a linear transformation.
- finally, you can take the vectors that make up your rope pattern and transformation them into the coordinate systems of the path, section by section. Now your previously straight pattern follows the path!
Caveats:
1) This assumes your path has a fixed length and your pattern is defined over the entire length. It would probably be more practical to have a repeating pattern or a function that can generate a pattern given the desired path length.
2) Each section is transformed individually, so if in the pattern, you have a straight line that crosses a section boundary, the line will have a visible kink after transformation. A way to mitigate this would be to use multiple overlapping sets of sections and interpolate the transformation between them.