将SVG路径转换为线段 [英] Turn SVG path into line segments

查看:639
本文介绍了将SVG路径转换为线段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将SVG路径转换为节点中的点列表。我使用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 ay,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/

在gecko中,通过从开头开始解析路径,读取一个非空格字符,然后使用一个预期参数数量的表来知道读取这么多数字可能最多有一个逗号分隔它们)。这会持续到字符串结束。

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路径转换为线段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆