andrewMacmurray/elm-simple-animation/1.0.0

NOTE: the latest version is 2.3.2

Simple.Animated

Render animations on the page

This module gives you animated Html divs out of the box, and some helpers to create animated versions of whatever UI library you're using.

e.g. You want animated elm-ui Elements? Create an animated helper like this:

animatedUi =
    Simple.Animated.ui
        { behindContent = Element.behindContent
        , htmlAttribute = Element.htmlAttribute
        , html = Element.html
        }

Then make animated els, columns and rows!

el : Animation -> List (Element.Attribute msg) -> Element msg -> Element msg
el =
    animatedUi Element.el

Then render an animation

dot : Element msg
dot =
    Utils.Animated.el expandFade [] elmUiDot

Common Helpers

You can find many of these common helpers here: https://github.com/andrewMacmurray/elm-simple-animation/blob/main/examples/src/Utils/Animated.elm

Html Animations

div : Animation -> List (Attribute msg) -> List (Html msg) -> Html msg

Render an Animated div

dot : Html msg
dot =
    Animated.div expandFade [ class "dot" ] []


expandFade : Animation
expandFade =
    Animation.fromTo
        { duration = 2000
        , options = [ Animation.loop ]
        }
        [ P.opacity 1, P.scale 1 ]
        [ P.opacity 0, P.scale 2 ]
(List (Attribute msg) -> List (Html msg) -> Html msg)
-> Animation
-> List (Attribute msg)
-> List (Html msg)
-> Html msg

Create any animated Html node:

For example, Simple.Animated.div is defined like this:

div : Animation -> List (Html.Attribute msg) -> List (Html msg) -> Html msg
div =
    Simple.Animated.html Html.div

Hook Into SVG

So you can use whatever version of elm/svg you like use the Simple.Animated.svg function along with Svg.Attributes.class to create animated Svg elements:

g : Animation -> List (Svg.Attribute msg) -> List (Svg msg) -> Svg msg
g =
    animatedSvg Svg.g


animatedSvg =
    Simple.Animated.node Svg.Attributes.class

Then render an animation

dot : Svg msg
dot =
    Svg.svg []
        [ Utils.Animated.g expandFade [] [ svgDot ]
        ]
svg :
(ClassName -> Attribute msg)
-> (List (Attribute msg) -> List (Html msg) -> Html msg)
-> Animation
-> List (Attribute msg)
-> List (Html msg)
-> Html msg

Hook Into Elm UI

Create animated elm-ui Elements

el : Animation -> List (Element.Attribute msg) -> Element msg -> Element msg
el =
    animatedUi Element.el


animatedUi =
    Simple.Animated.ui
        { behindContent = Element.behindContent
        , htmlAttribute = Element.htmlAttribute
        , html = Element.html
        }

Then render an animation

dot : Element msg
dot =
    Utils.Animated.el expandFade [] elmUiDot
type alias UiOptions element attribute msg =
{ behindContent : element -> attribute
, html : Html msg -> element
, htmlAttribute : Attribute msg -> attribute
}
ui :
UiOptions element attribute msg
-> (List attribute -> children -> element)
-> Animation
-> List attribute
-> children
-> element

Custom Renderer

When you want to completely customise how to render animations you can use the low level Simple.Animated.custom - which gives you access to the raw animation stylesheet and class name that will apply the animation.

For example, say you wanted to animate elm-community/typed-svg nodes - you could create animated versions like this:

g : Animation -> List (TypedSvg.Attribute msg) -> List (TypedSvg.Svg msg) -> TypedSvg.Svg msg
g =
    animatedTypedSvg TypedSvg.g


animatedTypedSvg node animation attributes children =
    Simple.Animated.custom
        (\className stylesheet ->
            node
                (TypedSvg.Attributes.class [ className ] :: attributes)
                (TypedSvg.style [] [ TypedSvg.text stylesheet ] :: children)
        )
        animation
type alias ClassName =
String
type alias Stylesheet =
String
custom : (ClassName -> Stylesheet -> animated) -> Animation -> animated