UNPKG

pixi-fusion

Version:

This module offers a set of common components needed for playing games.

34 lines (24 loc) 815 B
import { useMemo } from "react"; import { Sprite, SpriteOptions } from "pixi.js"; import { useTextures } from "./useTexture"; import { useObject } from "./useObject"; type UseSpriteOptions = Omit<SpriteOptions, "texture"> & { texture: string; }; export const useSprite = ({ texture = "", ...options }: UseSpriteOptions) => { const textureKeys = useMemo(() => { return [...(texture ? [texture] : [])]; }, [texture]); const { isFetched } = useTextures({ keys: textureKeys }); const sprite = useMemo(() => { if (!isFetched) { return null; } if (texture) { return Sprite.from(texture); } return new Sprite({}); }, [texture, frames, isFetched]); useObject({ object: sprite, ...options }); return sprite; };