Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
832 views
in Technique[技术] by (71.8m points)

node.js - Turn SVG path into line segments

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...