Animate (motion)
Learn how to animate Pixi’VN canvas components using the canvas.animate function, including examples of movement, rotation, fading, zooming, and mirroring.
Pixi’VN allows developers to animate canvas components using a function called canvas.animate. This function is a re-implementation of the animate function from the motion library, adapted to use PixiJS tickers for triggering animation events.
motion is a popular JavaScript library that provides a simple and powerful way to create animations. You can read more about it here.
Use
canvas.animate is one of the most commonly used functions in Pixi’VN and is designed to be straightforward — though the underlying concept may not be immediately obvious at first. The idea is simple: you define a target state for a canvas component (e.g. a position, scale, or opacity) and a duration, and Pixi’VN will smoothly transition the component to that state frame by frame. Once the target is reached, the animation ends automatically.
For example, to move a component to position { x: 100, y: 50 } over one second, you pass { x: 100, y: 50 } as the keyframes and { duration: 1 } as the options. The keyframes parameter defines what to reach; options defines how to get there.
canvas.animate internally creates a Ticker. For advanced use cases — such as pausing, resuming, or stopping an animation — you can use the functions available to control Pixi’VN Tickers.
Examples
import { canvas, ImageSprite, newLabel, showImage } from "@drincs/pixi-vn";
export const startLabel = newLabel("start", [
async () => {
const alien = await showImage("alien");
canvas.animate(alien, { xAlign: 1, yAlign: 0 }, { ease: "easeOut" });
},
() =>
canvas.animate<ImageSprite>(
"alien",
{ xAlign: 1, yAlign: 1 },
{ ease: "backOut" },
),
() =>
canvas.animate<ImageSprite>(
"alien",
{ xAlign: 0, yAlign: 1 },
{ ease: "circIn" },
),
() =>
canvas.animate<ImageSprite>(
"alien",
{ xAlign: 0, yAlign: 0 },
{ ease: "linear" },
),
]);import { canvas, newLabel, showImage } from "@drincs/pixi-vn";
export const startLabel = newLabel("start", [
async () => {
const alien = await showImage("alien", "alien", {
align: 0.5,
anchor: 0.5,
});
canvas.animate(
alien,
{ angle: 360 },
{ duration: 1, type: "spring", repeat: Infinity, repeatDelay: 0.2 },
);
},
]);import { canvas, newLabel, showImage } from "@drincs/pixi-vn";
export const startLabel = newLabel("start", [
async () => {
const alien = await showImage("alien", "alien", {
align: 0.5,
anchor: 0.5,
scale: 0,
});
canvas.animate(
alien,
{ scaleX: 1, scaleY: 1 },
{ ease: "circInOut", duration: 1 },
);
},
]);Other features
The canvas.animate function can also be used to create sequences of animations.
To create a sequence, you can pass arrays as properties values in the keyframes object. In this case, you can use the times property to specify the timing of each keyframe.
For example:
import { canvas, showImage } from "@drincs/pixi-vn";
const alien = await showImage("alien");
canvas.animate(
alien,
{
xAlign: [0, 1, 1, 0, 0],
yAlign: [0, 0, 1, 1, 0],
},
{ repeat: Infinity, duration: 10 },
);This method has some limitations compared to the previous one, such as restrictions on the repeat property due to the original motion library.
Another way to create animation sequences with canvas.animate is by using a timeline. This is useful when you want to chain multiple animations that are not strictly linear. You can provide an array of keyframes, where each keyframe is an object with the properties to animate and their values, along with optional animation options.
For example:
import { canvas, showImage } from "@drincs/pixi-vn";
const alien = await showImage("alien");
canvas.animate(
alien,
[
[{ xAlign: 0, yAlign: 0 }, { ease: "circInOut" }],
[{ xAlign: 1, yAlign: 0 }, { ease: "backInOut" }],
[{ xAlign: 1, yAlign: 1 }, { ease: "linear" }],
[{ xAlign: 0, yAlign: 1 }, { ease: "anticipate" }],
[{ xAlign: 0, yAlign: 0 }, { ease: "easeOut" }],
],
{ repeat: 10, duration: 10 },
);Pixi’VN also exposes the animate function directly from motion, which you can import as:
import { animate } from "@drincs/pixi-vn/motion";Unlike canvas.animate, this function does not save the current animation state. This makes it more performant, and it is the right choice for animating PixiJS UI components — since PixiJS UI elements are not part of Pixi’VN's saved state anyway.