Components
Overview of built-in and custom canvas components in Pixi’VN, including extension and memory features.
Pixi’VN provides a set of canvas components. These components extend the Pixi.js components, adding various features, such as the ability to save their current state.
Spritecorresponds to the componentPixiJS.Sprite.Containercorresponds to the componentPixiJS.Container.ImageSpriteis a component introduced by Pixi’VN.ImageContaineris a component introduced by Pixi’VN.VideoSpriteis a component introduced by Pixi’VN.Textis a component introduced by Pixi’VN.AnimatedSpritecorresponds to the componentPixiJS.AnimatedSprite, with frames loaded from a spritesheet.
- Spine 2D is a component installable via the Pixi’VN plugin system. It is used to render Spine 2D animations.
Other features
You can create custom components by extending the base components. To do this, you need to use the decorator @canvasComponentDecorator.
It is necessary to override the memory getter and the setMemory method to store the custom component properties.
In get memory(), it is very important to return the className property; this property must be equal to the id used in the decorator.
For example, you can create an AlienTinting class that extends the Sprite class to manage the direction and speed of each individual alien in an animation.
const ALIEN_TINTING_TEST_ID = "AlienTintingTest";
@canvasComponentDecorator({
name: ALIEN_TINTING_TEST_ID,
})
class AlienTintingTest extends Sprite<IAlienTintingMemory> {
readonly pixivnId: string = CANVAS_SPINE_ID;
override get memory() {
return {
...super.memory,
pixivnId: CANVAS_SPINE_ID,
direction: this.direction,
turningSpeed: this.turningSpeed,
speed: this.speed,
};
}
override async setMemory(memory: IAlienTintingMemory) {
await super.setMemory(memory);
this.direction = memory.direction;
this.turningSpeed = memory.turningSpeed;
this.speed = memory.speed;
}
direction: number = 0;
turningSpeed: number = 0;
speed: number = 0;
static override from(
source: Texture | TextureSourceLike,
skipCache?: boolean,
) {
let sprite = Sprite.from(source, skipCache);
let mySprite = new AlienTintingTest();
mySprite.texture = sprite.texture;
return mySprite;
}
}