folkertdev/one-true-path-experiment/3.0.0

NOTE: this package is not compatible with Elm 0.19.1

SubPath

Types

type SubPath

Type representing a subpath

A subpath is one moveto command followed by an arbitrary number of drawto commands.

Construction

subpath : MoveTo -> List DrawTo -> SubPath

Construct a subpath

subpath (moveTo (0,0)) [ lineTo [ (10,10), (10, 20) ] ]
empty : SubPath

An empty subpath

Conversion

element : SubPath -> List (Attribute msg) -> Svg msg

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

Svg.svg []
    [ SubPath.element mySubPath [ stroke "black" ] ]
toString : SubPath -> String

Convert a subpath into SVG path notation

import LowLevel.Command exposing (moveTo, lineTo)


line : SubPath
line = subpath (moveTo (0,0)) [ lineTo [ (10,10), (10, 20) ] ]


SubPath.toString line --> "M0,0 L10,10 10,20"
toSegments : SubPath -> List Segment

Convert a subpath to its Segments

subpath (moveTo (0,0)) [ lineTo [ (10, 10), (20, 10) ] ]
    |> toSegments
    --> [ line (0,0) (10,10) , line (10, 10) (20, 10) ]
fromSegments : List Segment -> SubPath

Convert a list of segments to a path

In the conversion, the starting point of a segment is discarded: It is assumed that for every two adjacent segments in the list, the first segment's end point is the second segment's starting point

import Segment exposing (line)
import LowLevel.Command exposing (moveTo, lineTo)




[ line (0,0) (10,10) , line (10, 10) (20, 10) ]
    |> fromSegments
    --> subpath (moveTo (0,0)) [ lineTo [ (10, 10)], lineTo [ (20, 10) ] ]
SubPath
-> Maybe
{ moveto : MoveTo
, drawtos : List DrawTo
}

deconstruct a subpath into its components

Composition

import Curve


curve : SubPath
curve =
    Curve.quadraticBezier ( 0, 0 )
        [ ( ( 0.5, -0.5 ), ( 1.0, 0 ) ) ]




down : SubPath
down =
    Curve.linear [ ( 0, 0 ), ( 0, 1 ) ]


curve
    |> connect down
    |> SubPath.toString
    --> "M0,0 Q0.5,-0.5 1,0 L0,0 L0,1"


curve
    |> continue down
    |> SubPath.toString
    --> "M0,0 Q0.5,-0.5 1,0 L1,1"


curve
    |> continueSmooth down
    |> SubPath.toString
    --> "M0,0 Q0.5,-0.5 1,0 L1.707106781187,0.707106781187"


close curve
    |> SubPath.toString
    --> "M0,0 Q0.5,-0.5 1,0 Z"
continue : SubPath -> SubPath -> SubPath

Start the second subpath where the first one ends

connect : SubPath -> SubPath -> SubPath

Join two subpaths, connecting them with a straight line

continueSmooth : SubPath -> SubPath -> SubPath

Start the second subpath where the first one ends, and rotate it to continue smoothly

close : SubPath -> SubPath

Append a ClosePath at the end of the subpath (if none is present)

Mapping

reverse : SubPath -> SubPath

Reverse a subpath

The direction of a subpath can be important if you want to use SVG fills.

mySubPath : SubPath
mySubPath =
    subpath (moveTo (0,0))
        [ lineTo [ (10, 10)], lineTo [ (20, 10) ] ]


mySubPath
    |> reverse
    |> reverse
    --> mySubPath
compress : SubPath -> SubPath

Try to merge adjacent instructions

This conversion is costly (timewise), but can shorten a subpath considerably, meaning other functions are faster.

Additionally, the toString output can become shorter.

translate : Vec2 Float -> SubPath -> SubPath

Translate the subpath by a vector

rotate : Float -> SubPath -> SubPath

Rotate a subpath around its starting point by an angle (in radians).

scale : Vec2 Float -> SubPath -> SubPath

Scale the subpath in the x and y direction

For more complex scaling operations, define a transformation matrix and use mapCoordinate.

mapCoordinate : (Vec2 Float -> Vec2 Float) -> SubPath -> SubPath

Map over all the 2D coordinates in a subpath

mapWithCursorState : (CursorState -> DrawTo -> b) -> SubPath -> List b

Map over each drawto with the CursorState available.

The CursorState contains the subpath start position and the current cursor position at the current DrawTo

Conversion

fromLowLevel : SubPath -> SubPath

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

Beware that the moveto is always interpreted as Absolute.

toLowLevel : SubPath -> Maybe SubPath

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