问题描述
我正在尝试将 SVG 路径转换为 Node.js 中的点列表.我正在使用 elementtree 来解析 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 是路径的定义,getPos 只是将 "x,y" 转换为带有 x 和 y 的对象,doLine 只是将坐标添加到列表中.
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.
我不确定 SVG 标准,所以如果有人可以帮助我,非常感谢.
I'm not sure on the SVG standard, so if anyone could help me, that would be many thanks.
推荐答案
SVG 包含自己的路径段 解析器 那么为什么要重新发明轮子呢.尝试在此基础上构建:http://jsfiddle.net/longsonr/skWH5/
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.
这篇关于将 SVG 路径转换为线段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!