Ticker
Panoramica dei ticker di Pixi'VN, utilizzati per animare i componenti dell'area di lavoro ed eseguire transizioni. Scopri come creare ticker personalizzati e utilizzarli efficacemente nei tuoi progetti di Pixi'VN.
Pixi'VN ti consente di animare i componenti dell'area di lavoro utilizzando i ticker.
Un ticker è una classe che viene eseguita su ogni fotogramma e che esegue una funzione. I ticker sono utilizzabili per animare i componenti, eseguire transizioni o eseguire qualsiasi logica che necessiti di aggiornamenti regolari.
Rispetto ai PixiJS.tickers, i ticker di Pixi'VN sono classi con un metodo fn chiamate a ogni fotogramma. Questo metodo viene utilizzato per animare i componenti dell'area di lavoro. Pixi'VN gestisce tutti i ticker in esecuzione, rileva quando non sono più necessari, e ti consente di interromperli, riprenderne la riproduzione o eliminarli, utilizzando vari metodi.
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),
]);
},
]);Altre funzionalità
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.
Ad esempio:
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({}),
);
},
]);