将 SVG 路径转换为线段
我正在尝试将 SVG 路径转换为 Node.js 中的点列表。我正在使用 elementtree 来解析 SVG 文件。
d 是路径的定义,getPos 只是将“x,y”转换为具有 x 和 ay 的对象,doLine 只是将坐标添加到列表中。
d = path.get('d')
words = d.split(' ')
oldPos = undefined
startPos = undefined
for i in [0..words.length]
word = words[i]
if word == 'm' or word == 'M'
oldPos = getPos(words[i + 1])
startPos = getPos(words[i + 1])
i += 1
else if word == 'l' or word == 'L'
console.log('done nothing...')
else if word == 'z' or word == 'Z'
doLine(oldPos, startPos)
else if word
pos = getPos(word)
doLine(oldPos, pos)
oldPos = pos
目前,这似乎无法正常工作。
我知道我的道路永远不会有曲线,所以我不需要担心这一点。
我不确定 SVG 标准,所以如果有人可以帮助我,那将非常感谢。
I am trying to turn a SVG path into a list of points in Node. I'm using elementtree to parse the SVG file.
d is the definition for the path, getPos simply turns a "x,y" into an object with an x and a y, doLine simply adds the coordinates to the list.
d = path.get('d')
words = d.split(' ')
oldPos = undefined
startPos = undefined
for i in [0..words.length]
word = words[i]
if word == 'm' or word == 'M'
oldPos = getPos(words[i + 1])
startPos = getPos(words[i + 1])
i += 1
else if word == 'l' or word == 'L'
console.log('done nothing...')
else if word == 'z' or word == 'Z'
doLine(oldPos, startPos)
else if word
pos = getPos(word)
doLine(oldPos, pos)
oldPos = pos
Currently, this doesn't seem to work correctly.
I know that my path will never have curves, so I don't need to worry about that.
I'm not sure on the SVG standard, so if anyone could help me, that would be many thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
SVG 包含自己的路径段 解析器 那么为什么要重新发明轮子呢。尝试在此基础上构建: http://jsfiddle.net/longsonr/skWH5/
在 gecko 中,路径是解析方法是从开头开始,读取一个非空白字符,然后使用后面的预期参数数量表来了解要读取这么多数字(最多可能有一个逗号分隔它们)。这一直持续到字符串末尾。
SVG contains its own path segment parser so why reinvent the wheel. Try building on this: http://jsfiddle.net/longsonr/skWH5/
In gecko the path is parsed by starting at the beginning, reading one non-whitespace character, then using a table of the number of expected arguments that follow to know to read so many numbers (which may have up to one comma separating them). This continues till the end of the string.
我不熟悉咖啡脚本,但您的代码似乎存在一些问题。
最重要的是,似乎你的 i += 1 仅在 word == 'm 或 'M' 时发生,所以我认为你大多数时候都会陷入循环。
其次,如果 word == 'l' 或 'L' 你似乎没有做任何事情,但我会认为那是你想要添加节点的时候。如果你没有曲线,那么你肯定有直线,除非你只是使用水平或垂直线。您还需要更新当前位置,并根据它是相对命令还是绝对命令
d="M90,20L100,30L110,20z"
是有效路径。你说你知道你的路径中不会有曲线,所以也许你也知道你的所有路径都会被空格分开。在这种情况下,这很公平,但可能值得牢记。希望这有一些帮助。
I'm not familiar with coffeescript, but there seem to be a few problems with your code.
Most importantly, it seems that your i += 1 only happens if word == 'm or 'M', so I would have thought you get stuck in a loop most of the time.
Second, you don't seem to do anything if word == 'l' or 'L' and I would have though that is when you would want to add a node. If you don't have curves, then surely you have lines, unless you're just using horizontal or vertical lines. You will also need to update your current position and do so differently depending on whether it is a relative or absolute command.
d="M90,20L100,30L110,20z"
is a valid path. You say you know there will be no curves in your path, so maybe you also know that all your paths will be split with spaces. In which case, that's fair enough, but it might be worth bearing in mind.Hope that's of some help.