mdgriffith / elm-style-animation

The style animation library for Elm!
http://package.elm-lang.org/packages/mdgriffith/elm-style-animation/latest
BSD 3-Clause "New" or "Revised" License
440 stars 40 forks source link

Need help with animating a path #56

Closed jiegillet closed 6 years ago

jiegillet commented 6 years ago

Hi, great project, I'm very excited to use it! I need some help with animating a path, I can't seem to make it work. I've copied my best guess as how it should work below, but when I run it, only one ticks happens and nothing shows up. Thanks!

module Main exposing (..)

import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
import Animation
import Svg
import Svg.Attributes

main : Program Never Model Msg
main =
    Html.program
        { init = init
        , view = view
        , update = update
        , subscriptions = subscriptions
        }

type alias Model =
    { style : Animation.State }

init : ( Model, Cmd Msg )
init =
    ( startAnimation
        { style =
            Animation.style
                [ Animation.path [ Animation.moveTo 0 0 ] ]
        }
    , Cmd.none
    )

subscriptions : Model -> Sub Msg
subscriptions model =
    Animation.subscription Animate [ model.style ]

type Msg
    = Animate Animation.Msg

update : Msg -> Model -> ( Model, Cmd Msg )
update action model =
    case action of
        Animate animMsg ->
            ( { model | style = Animation.update animMsg model.style }
            , Cmd.none
            )

startAnimation : Model -> Model
startAnimation model =
    { model
        | style =
            Animation.queue
                [ Animation.to
                    [ Animation.path
                        [ Animation.lineTo 200 200
                        , Animation.lineTo 200 100
                        , Animation.close
                        ]
                    ]
                ]
                model.style
    }

view : Model -> Html Msg
view model =
    Svg.svg
        [ Svg.Attributes.width "300"
        , Svg.Attributes.height "300"
        ]
        [ Svg.path
            (Animation.render model.style
                ++ [ Svg.Attributes.d "M 0 0 "
                   , Svg.Attributes.stroke "red"
                   ]
            )
            []
        ]
jiegillet commented 6 years ago

Never mind, I figured it out after I found your batman example. I had to match the path steps. Sorry about that! For reference:

init : ( Model, Cmd Msg )
init =
    ( startAnimation
        { style =
            Animation.style
                [ Animation.path
                    [ Animation.moveTo 0 0
                    , Animation.lineTo 0 0
                    ]
                ]
        }
    , Cmd.none
    )
startAnimation : Model -> Model
startAnimation model =
    { model
        | style =
            Animation.interrupt
                [ Animation.to
                    [ Animation.path
                        [ Animation.moveTo 0 0
                        , Animation.lineTo 100 100
                        ]
                    ]
                ]
                model.style
    }
view : Model -> Html Msg
view model =
    Svg.svg
        []
        [ Svg.path
            (Animation.render model.style ++ [ Svg.Attributes.stroke "red" ])
            []
        ]