🚟The Move Animation
The motion animation moves the content of a transition group along two connected edges in 3D space. For live examples visit the animation demo page and select one of the move
presets in the button grid at the top of the page.
Contents
Why would I use this component?
Astro allows users to create their own animations for view transitions. The astro-vtbot
move animation is a ready-made solution that can be easily inserted into Astro’s transition:animate
property. It also supports parameters for fine-tuning the animation to your needs. It is even possible to customize the underlying keyframes when used together with the <AnimationStyle/>
component.
Usage
Install astro-vtbot in your project running npx astro add astro-vtbot
or npm install astro-vtbot
.
Basic usage
The most basic usage is to apply the move()
animation without any parameters. This will use the default values for the animation.
The effect will in a first step move the old-image away from the viewer into the background and than with the second step move it to the left. The new-image uses the first step to move in from the right. That happens in the background. In the second step the image is moved to the front.
❗ Perspective
Please note that 3D-effects require the CSS perspective
property to be set on a parent of the transformed element.
For browsers that natively support view transition good choices for that parent are the ::view-transition-group
or ::view-transition-image-pair
pseudo element, which is the parent of the ::view-transition-old
and ::view-transition-new
pseudo elements.
The animations offered by the Bag of Tricks generate this for you, so you are already save on browsers that natively support view transitions.
For browsers that do not natively support view transitions, there are no such pseudo elements. For Astro’s fallback mode for view transitions to work properly with 3D animations, you have to set the perspective
on a regular HTML element that is a parent of the animated element in the DOM.
Here is how this is done in the animation demo:
Clipping
The same applies to clipping: To support browsers that do not implement native view transitions, set overflow
to clip
on a parent element in the DOM. For browsers that support native view transitions, set it to the ::view-transition-group
or ::view-transition-image-pair
pseudo-elements. Unlike perspective
, overflow
is not set by default as it is not clear whether you want clip
or visible
. In future versions you may be able to set this with a parameter.
Parametrized usage
The default values for the animation can be overwritten by passing an object with the desired values to the move()
function. The following example shows how to change the duration and easing of the animation.
You can set the following parameters:
Property name | CSS property | Default value |
---|---|---|
duration | animation-duration | 0.15s |
easing | animation-timing-function | ease-in-out |
delay | animation-delay | (see below) |
direction | animation-direction | normal |
fillMode | animation-fill-mode | both |
If you explicitly set a value for the delay, this value is used for both the in and out animation. This is typically not what you want. If you want to use different values for the in and out animation, use the customMove
function as described in the next section.
Extended parametrized usage
If you want to use more than the 5 properties supported by move()
or want to have more control over the animations keyframes, you can use the customMove()
function. This function takes a name for the transition and an object with the following optional properties:
keyframes
: An object with parameters for the keyframes for the animation.base
: An object with the same properties as themove()
function.extensions
: An object with arbitrary CSS properties to be used in the forwards/backwards old/new animation.
The function returns a scope identifier to be used with the data-astro-transition-scope
attribute of the HTML element you want to animate. Under the hood, customMove()
generates a style sheet with keyframe information and the defined CSS properties. You need to add these extended styles to your page by using the <AnimationStyle/>
component. Note: Only use <AnimationStyle/>
in conjunction with customMove()
, not with move()
.
Keyframes
The keyframes object of the customMove()
function has the following properties:
The two edges for moving the new image onto the page are enter
to enterMid
and enterMid
to final
. The two edges for moving the old image out are original
to leaveMid
and leaveMid
to leave
. Here original
and final
are the natural positions of the image. There coordinates are implicit. For the other endpoints enter
, enterMid
, leaveMid
and leave
give relative positions on all three axis.
In addition to these moves, the image can also be rotated at the endpoints (with values of 0deg for original
and final
). For the other endpoints the rotation is around the same axis which is defined by axis
. The default values for all keyframe parameters are all 0 with the exception of axis.z = 1
.
The values are used as parameters of the keyframe definition in
this style sheet template
Base
The value of the base
property is used to call the move()
function as defined above.
Extensions
With the extensions
property you can define all those CSS properties that can not be defined by move()
directly. You can specify different values for forwards
and backwards
, in
and out
animations. These values are merged with the result of callingmove()
with base parameters and overwrite its properties.