• Stars
    star
    444
  • Rank 98,300 (Top 2 %)
  • Language
    Elm
  • License
    BSD 3-Clause "New...
  • Created almost 9 years ago
  • Updated about 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

The style animation library for Elm!

The Style Animation library for Elm!

The Basics

To get started, there are a few things that need to happen.

Set an initial style in your model.

import Animation exposing (px)

init : Model
init =
    { style = 
        Animation.style
            [ Animation.left (px 0.0)
            , Animation.opacity 1.0
            ]
    }

Subscribe to Animation's subscription. This will animate using AnimationFrame when something is running, and stop giving updates when there is no animation.

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

Set up an update Msg in your update function.

    Animate animMsg ->
        { model
            | style = Animation.update animMsg model.style
        }

Render our animation at the necessary element in your view. Not all animated properties are style properties(such as the svg.d property and polygon.points property), so Animation.render actually returns a list of Html.Attributes. Fortunately, you can add your own style because Html.Attributes.style stacks!

    div
        (List.concat
            [ Animation.render model.style
            , [ style
                    [ ( "position", "absolute" )
                    , ( "border-style", "dotted" )
                    ]
               ]
            ]
        )
        [ text "This is being Animated!" ]

Start an animation in your update statement.

case msg of
    Show ->
        let 
            newStyle = 
                Animation.interrupt
                    [ Animation.to 
                        [ Animation.left (px 0.0)
                        , Animation.opacity 1.0
                        ]
                    ]
                    model.style
        in
            { model
                | style = newStyle
            }

Here's generally how we compose animations.

  • Choose Animation.queue or Animation.interrupt, both of which take a list of steps and your animation model. This describes what the strategy should be if the thing you're trying to animate is already in the process of being animated. You either want to interrupt what its doing and do this new animation. Or you want to queue up this new animation to run after the current animation is finished. 90% of the time you want Animation.interrupt
  • Steps can be
    • Animation.to - Animate to a target style
    • Animation.set - Set a animation to a style immediately.
    • Animation.wait (5 * second) - wait for some amount of time
    • Animation.repeat x [..list of steps to repeat] - Repeat a list of steps x times.
    • Animation.loop [..list of steps to repeat] - Loop a list of steps forever/until interrupted.

Examples

Advanced!

Note!

The compiler is going to refer to your animation model as Animation.Model.Animation msg. Animation.State is just a synonym for that.

Sending Messages

  • Send Messages Example - Code

First, import Animation.Messenger

Change your Animation.State to Animation.Messenger.State MyMsgType.

You can now use Animation.Messenger.send MyCustomMessage as a step in composing your animation.

You need to update this new animation state using Animation.Messenger.update, which will return (newAnimState, messagesSentinCmdForm). So you need to change your animation update section to something like the following.

case msg of
    Animate animMsg ->
        let
            (newStyle, cmds) =
                Animation.Messenger.update
                    animMsg
                    model.style
        in
            ( { model
                 | style = newStyle
              },
              cmds
            )

Note! Make sure you're sending the cmds in the above code. If you're note, then the animation will run, but the messages won't be sent.

Also, if you're running this in a child component, make sure you're Cmd.maping the child's commands back to the child or else the messages will be lost!

Animating Properties that aren't directly supported.

You can construct custom properties if you don't find them in the library using Animation.custom. These will be rendered in the style property.

Animation.to
    [ Animation.custom "my-custom-prop" 5 "px"
    ]

There is also customColor for color based properties.

Setting Custom Interpolation

Behind the curtain elm-style-animation mostly uses springs to animate values from A to B. However you can specify custom values for a spring, or a duration and easing if you want. There are two basic ways to do this.

Set them with your initial style.

Use Animation.styleWith or Animation.styleWithEach to set your initial style instead of Animation.style.

Animation.styleWith
    (Animation.spring
        { stiffness = 400
        , damping = 23 }
    )
    [ Animation.opacity 0
    , Animation.left (px 20)
    ]

This will set the spring used for these properties. Alternatively Animation.styleWithEach is a way to set a custom interpolation for each individual property.

Set a temporary spring/duration + easing

You can also use Animation.toWith and Animation.toWithEach. These can be substituted for Animation.to and allow you to specify a spring or duration+easing that lasts for exactly one step. After that step, whatever default spring or duration/easing there is (either the auto default or via being specified in Animation.styleWith) is then used.

Animation.interrupt
    [ Animation.toWith
        (Animation.easing
            { duration = 2*second
            , ease = (\x -> x^2)
            }
        ) 
        [ Animation.left (px 0.0)
        , Animation.opacity 1.0
        ]
    ]
    model.style

More Repositories

1

elm-ui

What if you never had to write CSS again?
Elm
1,322
star
2

style-elements

Create styles that don't mysteriously break!
HTML
446
star
3

elm-markup

Elm-friendly markup
Elm
177
star
4

elm-codegen

Elm
137
star
5

elm-animator

A timeline-based animation engine for Elm
Elm
131
star
6

elm-optimize-level-2

Elm
128
star
7

stylish-elephants

This project has matured and been released! Go here ->
Elm
44
star
8

elm-animation-flower-menu

JavaScript
17
star
9

design-discussion-elm-ui-2

A repo for discussing changes to Elm UI 2
17
star
10

elm-dev

Haskell
17
star
11

atom-inline-messenger

Inline Messaging in the Atom Editor
CoffeeScript
14
star
12

mechanical-elephant-hakyll

mechanical-elephant.com
CSS
11
star
13

elm-color-mixing

Color mixing in elm!
Elm
9
star
14

elm-markup-cli

JavaScript
9
star
15

elm-skimmer

Browse elm packages and projects with metrics!
HTML
8
star
16

style-elements-design-discussion

8
star
17

elm-animation-pack

Easily manage your animation states in Elm!
Elm
6
star
18

elm-markup-vscode

Syntax Highlighting for .up files for `elm-markup`
JavaScript
6
star
19

Elmsmith

A Static Site Generator in Elm
HTML
5
star
20

kubernetes-starterkit

WARNING: not for use quite yet.
Python
5
star
21

elm-prefab

Maintainable code generation for Elm Apps
JavaScript
5
star
22

ui-animation

All Work from this Repo has been published at https://github.com/mdgriffith/elm-style-animation
Elm
5
star
23

elm-gql-github-example

An example repo for elm-gql that uses the Github GraphQL API
Elm
4
star
24

low-poly-background

CoffeeScript
4
star
25

drakon-anywhere

The Drakon Editor in a Docker container so it can run anywhere
Shell
4
star
26

elm-html-animation

DEPRECATED: Use https://github.com/mdgriffith/elm-style-animation if you're on elm 0.17
Elm
4
star
27

elm-path

UNDER CONSTRUCTION! Just a sketch at the moment :)
HTML
4
star
28

roam-notion-import

A script to help import files from notion to roam research
Rust
3
star
29

elm-debug-watch

Convenience functions to log the input and output of a function.
Elm
3
star
30

elm-color

Elm
3
star
31

elm-animation-talk

JavaScript
2
star
32

elm-all-docs

A cache of all the docs.json files on the Elm Package website
TypeScript
2
star
33

roc-tui

CSS
2
star
34

site-selfie

Haskell
1
star
35

elm-style-elements-simple-example

DEPRECATED (This repo uses a much older version of style-elements)
Elm
1
star
36

elm-style-elements-complex-example

DEPRECATED (This repo uses a much older version of style-elements)
HTML
1
star
37

language-guitar-tab

Syntax Highlighter for Guitar Tablature for the Atom Code Editor
CoffeeScript
1
star
38

elm-style-animation-zero-sixteen

elm-style-animation for elm 0.16 (This is generally deprecated, use elm-style-animation for elm 0.17 and above)
Elm
1
star
39

elm-ui-testing

A repo for tracking elm-ui testing results
HTML
1
star