phollyer/elm-motion/1.0.0

NOTE: the latest version is 3.0.4

Anim.Property.PerspectiveOrigin

Animate the CSS perspective-origin property, which controls the vanishing point for 3D transforms applied to a parent element.

Default unit: %.

Default value: 50% 50% (center of the element)

Note: This module is for animating perspective-origin, if all you need is to set a static perspective-origin, use the View3D.perspectiveOrigin function instead, or set the style attribute yourself in your view.

Types

type alias Builder eng =
PerspectiveOriginBuilder eng

Builder type for perspective-origin animations.

type alias AnimGroupName =
String

Type alias for the animation group name.

Initialize

initXY : AnimGroupName -> Float -> Float -> AnimBuilder eng -> AnimBuilder eng

Set the initial perspective origin on both axes.

import Anim.Engine.* as Engine
import Anim.Property.PerspectiveOrigin as PerspectiveOrigin


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

Set the initial X-axis perspective origin.

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

Set the initial Y-axis perspective origin.

Build

begin : AnimBuilder eng -> Builder eng

Turn the AnimBuilder into a perspective origin animation Builder for the specified animation group.

Use this to start configuring a perspective origin animation.

myAnimation : AnimBuilder eng -> AnimBuilder eng
myAnimation =
    PerspectiveOrigin.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 =
    PerspectiveOrigin.begin
        >> ... -- configure the animation with from, to, duration, easing, etc.
        >> PerspectiveOrigin.end
        >> ... -- continue with animation

Configure

Start Value

When not set, the default will be used.

📖 See Start Values for details.

from : Float -> Builder eng -> Builder eng

Set the uniform starting X and Y values.

fromXY : Float -> Float -> Builder eng -> Builder eng

Set the starting X and Y values.

fromX : Float -> Builder eng -> Builder eng

Set the starting X value, preserving the current Y value.

fromY : Float -> Builder eng -> Builder eng

Set the starting Y value, preserving the current X value.

End Value

📖 See End Values for details.

Absolute

to : Float -> Builder eng -> Builder eng

Set the uniform target X and Y values.

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

Set the target X and Y values.

toX : Float -> Builder eng -> Builder eng

Set the target X value, preserving the current Y value.

toY : Float -> Builder eng -> Builder eng

Set the target Y value, preserving the current X value.

Relative

Move by a delta instead of to a fixed perspective origin. 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.

by : Float -> Builder eng -> Builder eng

Move by a delta on both axes.

byXY : Float -> Float -> Builder eng -> Builder eng

Move by a delta on the X and Y axes.

byX : Float -> Builder eng -> Builder eng

Move by a delta on the X axis. Y is unaffected.

byY : Float -> Builder eng -> Builder eng

Move by a delta on the Y axis. X is 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 per second the perspective origin changes.

For example, an animation from 0 to 200px with a speed of 100.0 will take 2 seconds to complete.

Easing

📖 See Easing for details.

easing : Easing -> Builder eng -> Builder eng

Set the easing function for the animation.

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


PerspectiveOrigin.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


PerspectiveOrigin.spring Spring.wobbly

CSS Units

Set the length Unit for both axes.

cssUnit : Unit -> AnimBuilder eng -> AnimBuilder eng

Set the length Unit for both axes.

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


Engine.init
    [ PerspectiveOrigin.initXY "vp" 200 150
        >> PerspectiveOrigin.cssUnit Px
    ]
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.

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 perspective origin 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. z is ignored for this property.

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

Apply new perspective-origin bounds for an anim group during resize.

Pass this inside an engine's onResize builder:

Sub.onResize model.animState <|
    PerspectiveOrigin.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 <|
    PerspectiveOrigin.bounds "box" boxBounds
        >> PerspectiveOrigin.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 perspective-origin 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
        GotCard (Ok { element }) ->
            let
                w =
                    element.width


                h =
                    element.height


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

Clamp

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

Keep the X axis perspective-origin within min and max values. If min > max the values are flipped.

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

Keep the Y axis perspective-origin 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.

Snap

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

set : Float -> Builder eng -> Builder eng

Snap to a uniform X and Y value.

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

Snap target X and Y values.

setX : Float -> Builder eng -> Builder eng

Snap target X value, preserving the current Y value.

setY : Float -> Builder eng -> Builder eng

Snap target Y value, preserving the current X value.