0

I'm working on some plugin-like code to simplify moving Raphael paths to fixed positions.

Normally, the first part of a Raphael path (which uses syntax based on SVG syntax) is an 'M' moveto command, defining the origin point of the path.

Two closely related questions:

  1. Is it reasonable to assume that any arbitrary path will begin with an 'M' moveto command? (I suspect the answer is 'no' to this)
  2. Assuming 'No' to Q1. above, on encountering a Raphael path array which doesn't begin with 'M' (somePath.attr(path)[0][0] != 'M'), can it be assumed that this path's point of origin will always be 0,0? And therefore, that any movement from 0,0 will be from a past transformation and therefore will be described in somePath.attr(transform)?

From the Raphael docs, it looks like there's nothing in the spec for the Path array that would contradict 2., but it's always worth checking for unexpected cases before making an assumption.


To put it another way, is there any case in which this function won't return the correct X and Y co-ordinates for a Raphael path origin (assuming it's passed the output from somePath.attr('path') of a valid Raphael path):

function getPathOrigin (path) {
    if (path[0][0] == 'M') {
        return {x: path[0][1], y: path[0][2]}
    } else {
        return {x: 0, y: 0}
    }
}
user56reinstatemonica8
  • 32,576
  • 21
  • 101
  • 125

1 Answers1

3

According to the specification a path must begin with a moveto command.

freejosh
  • 11,263
  • 4
  • 33
  • 47
  • I'd read the specification and didn't feel it gave a clear answer: it says "A path data segment (if there is one) must begin with a "moveto" command" - but to me this could be taken as meaning, *segmented* paths must begin each segment with `M`, but an unsegmented path might not (hence "if there is one"). Got anything confirming that a continuous path needs to start with a moveto? Or an explanation of the "(if there is one)" caveat? – user56reinstatemonica8 Mar 05 '13 at 19:12
  • What would be an unsegmented path? I took it to mean that if the `path` element has a `d` attribute it must start with a moveto. I suspect that their use of the word "segment" here is in error. I haven't found anything aside from the specification that mentions this either. Currently Chrome doesn't render anything if the path doesn't begin with a moveto. – freejosh Mar 05 '13 at 19:23
  • That interpretation of "if there is a `d` attribute" makes more sense. Raphael also gives the error `Error: Problem parsing d="...co-ordinates..."` on any path without an M at the start that I test it with. Seems conclusive, thanks! – user56reinstatemonica8 Mar 05 '13 at 19:37