Ticker
Overview of Pixi’VN tickers, which are used to animate canvas components and perform transitions. Learn how to create custom tickers and use them effectively in your Pixi’VN projects.
Pixi’VN allows you to animate canvas components using tickers.
A ticker is a class that runs on every frame and executes a function. Tickers can be used to animate components, perform transitions, or run any logic that needs to update regularly.
Compared to PixiJS.tickers, Pixi’VN tickers are classes with a fn method that is called every frame. This method is used to animate canvas components. Pixi’VN manages all running tickers, detects when they are no longer needed, and lets you pause, resume, or delete them using various methods.
Functions
To play, pause, or stop a ticker, you must use the functions of tickers.
It is important to keep the following behaviors in mind:
- If a ticker does not have any canvas components associated with it, it will be deleted.
- If you remove a canvas component, your alias will be unlinked from the ticker.
- If you add a canvas component with an alias that already exists, the new component will replace the old one. The new component will inherit the tickers of the old component.
To find a ticker, you must use the tickers.find function.
import { newLabel, RotateTicker, showImage, tickers } from "@drincs/pixi-vn";
export const startLabel = newLabel("start", [
async () => {
await showImage("egg_head", "egg_head", {
yAlign: 0.5,
xAlign: 0.25,
anchor: 0.5,
});
await showImage("flower_top", "flower_top", {
yAlign: 0.5,
xAlign: 0.75,
anchor: 0.5,
});
let tikerId = tickers.add(
["egg_head", "flower_top"],
new RotateTicker({}),
);
let ticker = tickers.find(tikerId);
console.log(ticker);
},
]);To remove a ticker, you must use the tickers.remove function.
import {
newLabel,
RotateTicker,
showImage,
storage,
tickers,
} from "@drincs/pixi-vn";
export const startLabel = newLabel("start", [
async () => {
await showImage("egg_head", "egg_head", {
yAlign: 0.5,
xAlign: 0.25,
anchor: 0.5,
});
await showImage("flower_top", "flower_top", {
yAlign: 0.5,
xAlign: 0.75,
anchor: 0.5,
});
let tikerId = tickers.add(
["egg_head", "flower_top"],
new RotateTicker({}),
);
storage.set("tiker_id", tikerId);
},
() => {
let tikerId = storage.get<string>("tiker_id");
tikerId && tickers.remove(tikerId);
},
]);To pause a ticker, you must use the tickers.pause function.
To resume a paused ticker, you must use the tickers.resume function.
import {
narration,
newLabel,
RotateTicker,
showImage,
tickers,
} from "@drincs/pixi-vn";
export const startLabel = newLabel("start", [
async () => {
await showImage("egg_head", "egg_head", { align: 0.5, anchor: 0.5 });
let tikerId = tickers.add(["egg_head"], new RotateTicker({}));
narration.dialogue = "start";
},
() => {
tickers.pause("egg_head");
narration.dialogue = "pause";
},
() => {
tickers.resume("egg_head");
narration.dialogue = "resume";
},
]);When the animation has a goal to reach, such as a destination, we sometimes need the animation to reach the goal before the current step ends.
To do this, you can use the tickers.completeOnStepEnd function.
import { narration, newLabel, showImage, tickers } from "@drincs/pixi-vn";
export const startLabel = newLabel("start", [
async () => {
await showImage("egg_head", "egg_head", {
yAlign: 0,
xAlign: 0,
anchor: 0,
});
let tikerId = tickers.add(
["egg_head"],
new MoveTicker({
destination: { x: 1, y: 0, type: "align" },
speed: 1,
}),
);
tikerId && tickers.completeOnStepEnd({ id: tikerId });
},
() => {
narration.dialogue = "complete";
},
]);If you want to run a sequence of tickers, you can use the tickers.addSequence function.
import { newLabel, RotateTicker, showImage, tickers } from "@drincs/pixi-vn";
export const startLabel = newLabel("start", [
async () => {
await showImage("egg_head", "egg_head", { anchor: 0.5 });
let tikerId = tickers.addSequence("egg_head", [
new MoveTicker({
destination: { x: 0.5, y: 0.5, type: "align" },
}),
new RotateTicker({ speed: 2, clockwise: false }, 2),
]);
},
]);Other features
Creating your own ticker is simple: extend the TickerBase class, override the fn method, and implement your logic.
Then, decorate the class with the @tickerDecorator decorator. The decorator can take a string as the ticker's alias; if not provided, the class name is used.
For example:
import {
canvas,
Container,
TickerBase,
tickerDecorator,
TickerValue,
} from "@drincs/pixi-vn";
@tickerDecorator() // or @tickerDecorator('RotateTicker')
export default class RotateTicker extends TickerBase<{
speed?: number;
clockwise?: boolean;
}> {
fn(
t: TickerValue,
args: {
speed?: number;
clockwise?: boolean;
},
aliases: string[],
): void {
let speed = args.speed === undefined ? 0.1 : args.speed;
let clockwise = args.clockwise === undefined ? true : args.clockwise;
aliases.forEach((alias) => {
let component = canvas.find(alias);
if (component && component instanceof Container) {
if (clockwise) component.rotation += speed * t.deltaTime;
else component.rotation -= speed * t.deltaTime;
}
});
}
}To add a ticker you must use the tickers.add function.
import { newLabel, RotateTicker, showImage, tickers } from "@drincs/pixi-vn";
export const startLabel = newLabel("start", [
async () => {
await showImage("egg_head", "egg_head", {
yAlign: 0.5,
xAlign: 0.25,
anchor: 0.5,
});
await showImage("flower_top", "flower_top", {
yAlign: 0.5,
xAlign: 0.75,
anchor: 0.5,
});
let tikerId = tickers.add(
["egg_head", "flower_top"],
new RotateTicker({}),
);
},
]);