⮌ back | Overview | Home

🔎🔍The Zoom Animation

The zoom animation is an animation that zooms or shrinks the content of a transition group. For live examples visit the animation demo page and select one of the zoom presets in the button filed 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 zoom 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 zoom() animation without any parameters. This will use the default values for the animation.

MyComponent.astro
---
import { zoom } from "astro-vtbot/animations/zoom";
---
...
<h1 transition:animate={zoom()}>Zoom</h1>
...

The effect will zoom the old image to 500% while fading it out and it lets the new image grow from a point to 100% of its size while fading in. Reverse for the backwards direction. The animation will take 0.3 seconds and use the ease-in-out easing function.

Clipping

The zoom animation is not clipped, i.e. when zooming the image bigger it might cover the whole viewport. If you want to restrict the effect to the current animation group, see the description for the Swing animation.

Parametrized usage

The default values for the animation can be overwritten by passing an object with the desired values to the zoom() function. The following example shows how to change the duration and easing of the animation.

MyComponent.astro
---
import { zoom } from "astro-vtbot/animations/zoom";
---
...
<h1 transition:animate={zoom({duration:'1.5s', easing: 'cubic-bezier(.16,.73,.05,1.52)' })}>Zoom</h1>
...

You can set the following parameters:

Property nameCSS propertyDefault value
durationanimation-duration0.3s
easinganimation-timing-functionease-in-out
delayanimation-delay0s
directionanimation-directionnormal
fillModeanimation-fill-modeboth

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 customZoom function as described in the next section.

Extended parametrized usage

If you want to use more than the 5 properties supported by zoom() or want to have more control over the animations keyframes, you can use the customZoom() function. This function takes a name for the transition and an object with the following optional properties:

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, customZoom() 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 customZoom(), not with zoom().

MyComponent.astro
---
import { customZoom } from "astro-vtbot/animations/zoom";
import AnimationStyle from "astro-vtbot/animations/AnimationStyle.astro";
const zoomScope = customZoom('myZoom', {
keyframes: { scale: { forwardOut: 0, backwardIn: 0 } },
base: { duration: '0.3s', easing: 'cubic-bezier(.16,.73,.05,1.52)' },
extensions: {
forwards: { new: { 'animation-delay': '0.2s' } },
backwards: { new: { 'animation-delay': '0.2s' } },
},
});
---
...
<AnimationStyle name="myZoom"/>
<h1 data-astro-transition-scope={zoomScope}>Zoom</h1>
...

Keyframes

The keyframes object of the customZoom() function has the following properties:

type ZoomKeyframeParameter = {
scale?: { forwardOut?: number; forwardIn?: number; backwardOut?: number; backwardIn?: number; };
opacity?: { forwardOut?: number; forwardIn?: number; backwardOut?: number; backwardIn?: number; };
};

The forwardOut values say how big the old image should be zoomed when leaving the page (starting from it’s original size). The forwardIn value is the start value for the new image when entering the new page (before it is zoomed to its natural size). Values < 1 shrink the image, values > 1 enlarge it. The backwards values are for history back navigation. Opacity values follow the same logic.

The default values are all 0 with the exception of forwards.out.scale and backwards.in.scale which are 5.

The values are used as parameters of the keyframe definition in

this style sheet template
@keyframes ${keyframeNamePrefix}FwdZoomOut {
from {
transform: scale(1);
opacity: 1;
}
to {
transform: scale(${forwardOutScale});
opacity: ${forwardOutOpacity};
}
}
@keyframes ${keyframeNamePrefix}FwdZoomIn {
from {
transform: scale(${forwardInScale});
opacity: ${forwardInOpacity};
}
to {
transform: scale(1);
opacity: 1;
}
}
@keyframes ${keyframeNamePrefix}BwdZoomOut {
from {
transform: scale(1);
opacity: 1;
}
to {
transform: scale(${backwardOutScale});
opacity: ${backwardOutOpacity};
}
}
@keyframes ${keyframeNamePrefix}BwdZoomIn {
from {
transform: scale(${backwardInScale});
opacity: ${backwardInOpacity};
}
to {
transform: scale(1);
opacity: 1;
}
}

Base

The value of the base property is used to call the zoom() function as defined above.

Extensions

With the extensions property you can define all those CSS properties that can not be defined by zoom() directly. You can specify different values for forwards and backwards, in and out animations. These values are merged with the result of callingzoom() with base parameters and overwrite its properties.