phollyer/elm-motion/1.0.1

NOTE: the latest version is 3.0.4

Anim.Property.Translate

Move elements along the X, Y, and Z axes.

Default: 0 for all axes

When no start value is configured for any axis, the default will be used for that axis.

Types

type alias Builder eng =
TranslateBuilder eng

Builder type for translate animations.

type alias AnimGroupName =
String

Type alias for the animation group name.

Initialize

AnimGroupName
-> Float
-> Float
-> Float
-> AnimBuilder eng
-> AnimBuilder eng

Set the initial X, Y, and Z position.

import Anim.Engine.* as Engine
import Anim.Property.Translate as Translate


init : ( Model, Cmd Msg )
init =
    ( { animState = Engine.init [ Translate.initXYZ "animGroupName" 100 20 50 ] }
    , Cmd.none
    )
initXY : AnimGroupName -> Float -> Float -> AnimBuilder eng -> AnimBuilder eng

Set the initial X and Y position.

initXZ : AnimGroupName -> Float -> Float -> AnimBuilder eng -> AnimBuilder eng

Set the initial X and Z position.

initX : AnimGroupName -> Float -> AnimBuilder eng -> AnimBuilder eng

Set the initial X position.

initYZ : AnimGroupName -> Float -> Float -> AnimBuilder eng -> AnimBuilder eng

Set the initial Y and Z position.

initY : AnimGroupName -> Float -> AnimBuilder eng -> AnimBuilder eng

Set the initial Y position.

initZ : AnimGroupName -> Float -> AnimBuilder eng -> AnimBuilder eng

Set the initial Z position.

Build

begin : AnimBuilder eng -> Builder eng

Turn the AnimBuilder into a translate animation Builder for the specified animation group.

Use this to start configuring a translate animation.

myAnimation : AnimBuilder eng -> AnimBuilder eng
myAnimation =
    Translate.begin
        >> ... -- Configure and build the animation
end : Builder eng -> AnimBuilder eng

Complete the Builder animation configuration and return an AnimBuilder so you can continue configuring other property animations or execute the animation with an Engine.

myAnimation : AnimBuilder eng -> AnimBuilder eng
myAnimation =
    Translate.begin
        >> ... -- configure the animation with from, to, duration, easing, etc.
        >> Translate.end
        >> ... -- continue with animation

Configure

Start Value

📖 See Start Values for details.

fromXYZ : Float -> Float -> Float -> Builder eng -> Builder eng

Set the starting X, Y, and Z position.

myAnimation : AnimBuilder eng -> AnimBuilder eng
myAnimation =
    Translate.begin
        >> Translate.fromXYZ 100 20 50
        >> ... -- continue with animation
fromXY : Float -> Float -> Builder eng -> Builder eng

Set the starting X and Y position. Z is left unchanged (or 0 if not set).

fromXZ : Float -> Float -> Builder eng -> Builder eng

Set the starting X and Z position. Y is left unchanged (or 0 if not set).

fromX : Float -> Builder eng -> Builder eng

Set the starting X position. Y and Z are left unchanged (or 0 if not set).

fromYZ : Float -> Float -> Builder eng -> Builder eng

Set the starting Y and Z position. X is left unchanged (or 0 if not set).

fromY : Float -> Builder eng -> Builder eng

Set the starting Y position. X and Z are left unchanged (or 0 if not set).

fromZ : Float -> Builder eng -> Builder eng

Set the starting Z position. X and Y are left unchanged (or 0 if not set).

End Value

📖 See End Values for details.

Absolute

toXYZ : Float -> Float -> Float -> Builder eng -> Builder eng

Set the target X, Y, and Z position for the current animation group.

toXY : Float -> Float -> Builder eng -> Builder eng

Set the target X and Y position. Z is left unchanged (or 0 if not set).

toXZ : Float -> Float -> Builder eng -> Builder eng

Set the target X and Z position. Y is left unchanged (or 0 if not set).

toX : Float -> Builder eng -> Builder eng

Set the target X position. Y and Z are left unchanged (or 0 if not set).

toYZ : Float -> Float -> Builder eng -> Builder eng

Set the target Y and Z position. X is left unchanged (or 0 if not set).

toY : Float -> Builder eng -> Builder eng

Set the target Y position. X and Z are left unchanged (or 0 if not set).

toZ : Float -> Builder eng -> Builder eng

Set the target Z position. X and Y are left unchanged (or 0 if not set).

Relative

Move by a delta instead of to a fixed position. The end value is current + delta, where current is the live animated position.

Only available on the Sub and WAAPI engines. Using these with any other engine results in a type error.

Float
-> Float
-> Float
-> Builder { eng | withLiveDelta : () }
-> Builder { eng | withLiveDelta : () }

Move by specific amounts on the X, Y, and Z axes.

myAnimation : AnimBuilder { eng | withLiveDelta : () } -> AnimBuilder { eng | withLiveDelta : () }
myAnimation =
    Translate.begin
        >> Translate.byXYZ 50 -25 10
        >> ... -- continue with animation
Float
-> Float
-> Builder { eng | withLiveDelta : () }
-> Builder { eng | withLiveDelta : () }

Move by specific amounts on the X and Y axes. Z is unaffected.

Float
-> Float
-> Builder { eng | withLiveDelta : () }
-> Builder { eng | withLiveDelta : () }

Move by specific amounts on the X and Z axes. Y is unaffected.

byX :
Float
-> Builder { eng | withLiveDelta : () }
-> Builder { eng | withLiveDelta : () }

Move by a specific amount on the X axis. Y and Z are unaffected.

Float
-> Float
-> Builder { eng | withLiveDelta : () }
-> Builder { eng | withLiveDelta : () }

Move by specific amounts on the Y and Z axes. X is unaffected.

byY :
Float
-> Builder { eng | withLiveDelta : () }
-> Builder { eng | withLiveDelta : () }

Move by a specific amount on the Y axis. X and Z are unaffected.

byZ :
Float
-> Builder { eng | withLiveDelta : () }
-> Builder { eng | withLiveDelta : () }

Move by a specific amount on the Z axis. X and Y are unaffected.

Timing

📖 See Animation Timing for details.

Int
-> Builder { eng | withTiming : () }
-> Builder { eng | withTiming : () }

Set the delay (milliseconds) before the animation starts.

Int
-> Builder { eng | withTiming : () }
-> Builder { eng | withTiming : () }

Set the animation duration (milliseconds).

Float
-> Builder { eng | withTiming : () }
-> Builder { eng | withTiming : () }

The speed represents how many Units the element moves per second.

So if the CSS Unit is Px and the speed is 100, the element will move 100 pixels per second, likewise, if the CSS Unit is Cqw and the speed is 100, the element will move 100% of it's container's width per second.

Easing

📖 See Easing for details.

easing : Easing -> Builder eng -> Builder eng

Set the easing function for the animation.

import Motion.Easing exposing (Easing(..))


Translate.easing EaseInOut

Spring

📖 See Spring for details.

Spring
-> Builder { eng | withSpring : () }
-> Builder { eng | withSpring : () }

Drive this property with a spring.

import Motion.Spring as Spring


Translate.spring Spring.wobbly

CSS Units

Set the CSS length unit(s) used in translate animations.

cssUnit : Unit -> AnimBuilder eng -> AnimBuilder eng

Set the length Unit for all axes.

import Anim.Unit exposing (Unit(..))


Engine.init
    [ Translate.initX "box" 50
        >> Translate.cssUnit Cqw
    ]
cssUnitX : Unit -> AnimBuilder eng -> AnimBuilder eng

Set the length Unit for the X axis.

cssUnitY : Unit -> AnimBuilder eng -> AnimBuilder eng

Set the length Unit for the Y axis.

cssUnitZ : Unit -> AnimBuilder eng -> AnimBuilder eng

Set the length Unit for the Z axis.

Responsive Animations

When using responsive units like % or Cqw, the animation automatically responds to changes in screen or container size without extra configuration. However, when using fixed units like Px, the animation needs to be made aware of size changes in order to respond to them. This is done with the functions below.

📖 See Responsive Animations for patterns and examples.

Bounds

Keep translate values within a range you choose. Values outside the range are clamped to the nearest boundary. An animation that is within the bounds, either mid-flight or paused, will me remapped proportionally inside the bounds.

type alias Bounds =
{ min : Float
, max : Float
}

A numeric range with min and max boundaries.

type alias AxisBounds =
{ x : Maybe Bounds
, y : Maybe Bounds
, z : Maybe Bounds
}

Per-axis resize ranges. Nothing leaves an axis untouched.

{ x = Just { min = 0, max = 100 }
, y = Nothing
, z = Nothing
}
AnimGroupName
-> AxisBounds
-> AnimBuilder { eng | withBounds : () }
-> AnimBuilder { eng | withBounds : () }

Apply new translate bounds for an anim group during resize.

Pass this inside an engine's onResize builder:

Sub.onResize model.animState <|
    Translate.bounds "box"
        { x = Just { min = 0, max = newWidth - boxSize }
        , y = Nothing
        , z = Nothing
        }

You can set the bounds for multiple anim groups in one call:

Sub.onResize model.animState <|
    Translate.bounds "box" boxBounds
        >> Translate.bounds "card" cardBounds

The engine proportionally remaps the in-flight animation onto the new range and pins its endpoints to it.

Only callable from inside an engine's onResize builder - calling it from anywhere else results in a type error.

Clamping

Keep translate values on each axis within a range you choose.

Values outside the range are clamped to the nearest boundary.

Similar to bounds, but without proportional remapping.

The range stays in effect for future animations until you Unclamp it:

update msg model =
    case msg of
        GotCanvas (Ok { element }) ->
            let
                w =
                    element.width


                h =
                    element.height


                ( animState, cmd ) =
                    WAAPI.retarget model.animState <|
                        Translate.begin
                            >> Translate.clampX 0 (w - boxWidth)
                            >> Translate.clampY 0 (h - boxWidth)
                            >> Translate.end
            in
            ( { model | animState = animState }
            , cmd
            )

Clamp

clampX : Float -> Float -> Builder eng -> Builder eng

Keep the X axis translate within min and max values. If min > max the values are flipped.

clampY : Float -> Float -> Builder eng -> Builder eng

Keep the Y axis translate within min and max values. If min > max the values are flipped.

clampZ : Float -> Float -> Builder eng -> Builder eng

Keep the Z axis translate within min and max values. If min > max the values are flipped.

Unclamp

unclampX : Builder eng -> Builder eng

Remove the X axis range for this animation group. Does nothing if no range is set.

unclampY : Builder eng -> Builder eng

Remove the Y axis range for this animation group. Does nothing if no range is set.

unclampZ : Builder eng -> Builder eng

Remove the Z axis range for this animation group. Does nothing if no range is set.

Snap

Snap to a specific position, cancelling any in-flight animation on this property.

setXYZ : Float -> Float -> Float -> Builder eng -> Builder eng

Snap target X, Y and Z values silently

setXY : Float -> Float -> Builder eng -> Builder eng

Snap target X and Y values, preserving the current Z value.

setXZ : Float -> Float -> Builder eng -> Builder eng

Snap target X and Z values, preserving the current Y value.

setX : Float -> Builder eng -> Builder eng

Snap target X value, preserving the current Y and Z values.

setYZ : Float -> Float -> Builder eng -> Builder eng

Snap target Y and Z values, preserving the current X value.

setY : Float -> Builder eng -> Builder eng

Snap target Y value, preserving the current X and Z values.

setZ : Float -> Builder eng -> Builder eng

Snap target Z value, preserving the current X and Y values.