folkertdev / one-true-path-experiment / Path

Module for layering SubPaths into Paths.

Most of the interesting stuff happens in the SubPath and Curve modules. Path is simply for combining multiple subpaths into one string or element.

Data Structures


type alias Path =
List SubSubPath

A path is a list of SubPaths.

Constructing Paths

parse : String -> Result (List (Parser.Advanced.DeadEnd String String)) Path

Parse a path string into a Path

import Curve
import SubPath exposing (SubPath)

expected : SubPath
expected =
    Curve.linear [ (0,0), (42, 73) ]

parse "M0,0 l42,73"
    --> Ok [expected]

Only accepts valid complete subpaths (a sequences of a move followed by zero or more draws). Relative instructions are converted to absolute ones. Short-hand curve extensions are converted to explicit curve instructions.

The parser uses elm-tools/parser. The error type is Parser.Error.

Creating SVG

element : Path -> List (Svg.Attribute msg) -> Svg msg

Construct an svg path element from a Path with the given attributes

toString : Path -> String

Turn a Path into a String. The result is ready to be used with the d attribute.

import Curve
import SubPath exposing (SubPath)

myPath : SubPath
myPath =
    Curve.linear [ (0,0), (42, 73) ]

Path.toString [ myPath ]
    --> "M0,0 L42,73"

-- forms a cycle (almost isomorphism) with parse
arc : String
arc = "M80,230 A45,45 90 0 1 125,275"

arc
    |> parse
    |> Result.map Path.toString
    --> Ok arc

Conversion

fromLowLevel : List LowLevel.SubPath -> Path

Converting a svg-path-lowlevel subpath into a one-true-path subpath. Used in parsing

toLowLevel : Path -> List LowLevel.SubPath

Convert a path to a svg-path-lowlevel list of subpaths