AnimatedSprite
A simple way to display an animation depicted by a list of textures.
I recommend using spritesheets created by TexturePacker (they have a great tutorial on it). If you don’t want to use spritesheets, you can simply just pass in an array of your desired textures.
PIXI.AnimatedSprite description from PixiJS
sourceAn AnimatedSprite is a simple way to display an animation depicted by a list of textures.
Type-Safety Limitation with Subclasses
Usage
Basic
<script> import * as PIXI from 'pixi.js' import { AnimatedSprite } from 'svelte-pixi'</script>
<AnimatedSprite x={360} y={200} textures={[ PIXI.Texture.from('adventurer-idle-00.png'), PIXI.Texture.from('adventurer-idle-01.png'), PIXI.Texture.from('adventurer-idle-02.png'), ]} playing animationSpeed={0.1} anchor={0.5} scale={3}/><script> import * as PIXI from 'pixi.js' import { AnimatedSprite } from 'svelte-pixi/svelte-4'</script>
<AnimatedSprite x={360} y={200} textures={[ PIXI.Texture.from('adventurer-idle-00.png'), PIXI.Texture.from('adventurer-idle-01.png'), PIXI.Texture.from('adventurer-idle-02.png'), ]} playing animationSpeed={0.1} anchor={0.5} scale={3}/>Multiple Animations
<script> import { AnimatedSprite } from 'svelte-pixi' import { Assets } from 'pixi.js'
// loaded from parent <AssetsLoader /> const spritesheet = Assets.get('/assets/adventurer-spritesheet.json') let textures = spritesheet.animations['adventurer-idle']
function changeAnimation() { // pick an an animation from the spritesheet at random const keys = Object.keys(spritesheet.animations) const randomIndex = Math.floor(Math.random() * keys.length) textures = spritesheet.animations[keys[randomIndex]] }
</script>
<AnimatedSprite {textures} x={360} y={200} playing animationSpeed={0.1} anchor={0.5} scale={3} onloop={changeAnimation}/><script> import { AnimatedSprite } from 'svelte-pixi/svelte-4' import { Assets } from 'pixi.js'
// loaded from parent <AssetsLoader /> const spritesheet = Assets.get('/assets/adventurer-spritesheet.json') let textures = spritesheet.animations['adventurer-idle']
function changeAnimation() { // pick an an animation from the spritesheet at random const keys = Object.keys(spritesheet.animations) const randomIndex = Math.floor(Math.random() * keys.length) textures = spritesheet.animations[keys[randomIndex]] }
</script>
<AnimatedSprite {textures} x={360} y={200} playing animationSpeed={0.1} anchor={0.5} scale={3} on:loop={changeAnimation}/>API
Props
* denotes required
| Name | Description |
|---|---|
textures* | PIXI.Texture<PIXI.TextureSource<any>>[]PIXI.FrameObject[]The array of textures or frame objects used for the animation sequence. Can be set to either an array of Textures or an array of FrameObjects with custom timing. |
accessible | booleanFlag for if the object is accessible. If true AccessibilityManager will overlay a shadow div with attributes set |
accessibleChildren | booleanSetting to false will prevent any children inside this container to be accessible. Defaults to true. |
accessibleHint | nullstringSets the aria-label attribute of the shadow div |
accessiblePointerEvents | "visible""auto""none""visiblePainted""visibleFill""visibleStroke""painted""fill""stroke""all""inherit"Specify the pointer-events the accessible div will use Defaults to auto. |
accessibleTitle | nullstringSets the title attribute of the shadow div If accessibleTitle AND accessibleHint has not been this will default to 'container [tabIndex]' |
accessibleType | "object""label""a""abbr""address""area""article""aside""audio""b""base""bdi""bdo""blockquote""body""br""button""canvas""caption""cite""code""col""colgroup""data""datalist""dd""del""details""dfn""dialog""div""dl""dt""em""embed""fieldset""figcaption""figure""footer""form""h1""h2""h3""h4""h5""h6""head""header""hgroup""hr""html""i""iframe""img""input""ins""kbd""legend""li""link""main""map""mark""menu""meta""meter""nav""noscript""ol""optgroup""option""output""p""picture""pre""progress""q""rp""rt""ruby""s""samp""script""search""section""select""slot""small""source""span""strong""style""sub""summary""sup""table""tbody""td""template""textarea""tfoot""th""thead""time""title""tr""track""u""ul""var""video""wbr"Specify the type of div the accessible layer is. Screen readers treat the element differently depending on this type. Defaults to button. |
alpha | number |
anchor | number[number, number]{ x: number; y: number }PIXI.PointThe anchor sets the origin point of the sprite. The default value is taken from the {@link Texture} and passed to the constructor.
If you pass only single parameter, it will set both x and y to the same value as shown in the example below. |
angle | numberThe angle of the object in degrees.
|
animationSpeed | numberThe speed that the AnimatedSprite will play at. Higher is faster, lower is slower. |
autoUpdate | booleanControls whether the animation automatically updates using the shared ticker. When enabled, the animation will update on each frame. When disabled, you must manually call update() to advance the animation. |
blendMode | "none""inherit""normal""add""multiply""screen""darken""lighten""erase""color-dodge""color-burn""linear-burn""linear-dodge""linear-light""hard-light""soft-light""pin-light""difference""exclusion""overlay""saturation""color""luminosity""normal-npm""add-npm""screen-npm""subtract""divide""vivid-light""hard-mix""negation""min""max" |
boundsArea | PIXI.RectangleAn optional bounds area for this container. Setting this rectangle will stop the renderer from recursively measuring the bounds of each children and instead use this single boundArea.
|
cacheAsBitmap | booleanLegacy property for backwards compatibility with PixiJS v7 and below.
Use |
cacheAsTexture | PIXI.CacheAsTextureOptionsboolean |
cullable | booleanControls whether this object should be culled when out of view. When true, the object will not be rendered if its bounds are outside the visible area. |
cullableChildren | booleanControls whether children of this container can be culled. When false, skips recursive culling checks for better performance. |
cullArea | PIXI.RectangleCustom shape used for culling calculations instead of object bounds. Defined in local space coordinates relative to the object.
|
cursor | "progress""auto""none""default""context-menu""help""pointer""wait""cell""crosshair""text""vertical-text""alias""copy""move""no-drop""not-allowed""e-resize""n-resize""ne-resize""nw-resize""s-resize""se-resize""sw-resize""w-resize""ns-resize""ew-resize""nesw-resize""col-resize""nwse-resize""row-resize""all-scroll""zoom-in""zoom-out""grab""grabbing"string & {}The cursor style to display when the mouse pointer is hovering over the object. Accepts any valid CSS cursor value or custom cursor URL. |
effects | PIXI.Effect[]todo Needs docs |
eventMode | "auto""none""passive""static""dynamic"Enable interaction events for the Container. Touch, pointer and mouse events are supported. |
filterArea | PIXI.RectangleThe area the filter is applied to. This is used as an optimization to define a specific region for filter effects instead of calculating the display object bounds each frame.
|
filters | readonly PIXI.Filter[]Sets the filters for the displayObject. Filters are visual effects that can be applied to any display object and its children.
|
height | numberThe height of the Container,
|
hitArea | nullPIXI.IHitAreaDefines a custom hit area for pointer interaction testing. When set, this shape will be used for hit testing instead of the container's standard bounds. |
instance | TThe PIXI.AnimatedSprite instance. Can be set or bound to. WARNING: Type-safety limitation - If you are using a subclass of PIXI.AnimatedSprite, you MUST provide the instance prop with your custom instance. Due to TypeScript's limitations with generic types, if you don't provide an instance, a base PIXI.AnimatedSprite will be created and cast to your type, which will cause runtime errors when trying to access subclass-specific properties or methods. Example:
|
interactive | booleanWhether this object should fire UI events. This is an alias for |
interactiveChildren | booleanControls whether children of this container can receive pointer events. Setting this to false allows PixiJS to skip hit testing on all children, improving performance for containers with many non-interactive children. |
isRenderGroup | boolean |
label | stringThe instance label of the object. |
loop | booleanWhether or not the animation repeats after playing. When true, the animation will restart from the beginning after reaching the last frame. When false, the animation will stop on the last frame. |
mask | numberPIXI.Container<PIXI.ContainerChild>Partial<PIXI.MaskOptionsAndMask> |
name | stringThe instance name of the object. |
onadded | () => voidCalled when the instance is added to its parent or stage |
onclick | nullPIXI.FederatedEventHandler<PIXI.FederatedPointerEvent>Property-based event handler for the |
oncomplete | () => voidAlias for onComplete |
onComplete | () => voidUser-assigned function to call when an AnimatedSprite finishes playing. This function is called when the animation reaches the end and stops playing. If the animation is set to loop, this function will not be called. |
oncreate | (instance: T) => voidCalled on creation of the component. You can use this to do any setup on the instance directly |
onframechange | (currentFrame: number) => voidAlias for onFrameChange |
onFrameChange | (currentFrame: number) => voidUser-assigned function to call when an AnimatedSprite changes which texture is being rendered. This function is called every time the current frame changes during playback. It receives the current frame index as an argument. |
onglobalmousemove | nullPIXI.FederatedEventHandler<PIXI.FederatedPointerEvent>Property-based event handler for the Fired when the mouse moves anywhere, regardless of whether the pointer is over this object.
The object must have |
onglobalpointermove | nullPIXI.FederatedEventHandler<PIXI.FederatedPointerEvent>Property-based event handler for the Fired when the pointer moves anywhere, regardless of whether the pointer is over this object.
The object must have |
onglobaltouchmove | nullPIXI.FederatedEventHandler<PIXI.FederatedPointerEvent>Property-based event handler for the Fired when a touch interaction moves anywhere, regardless of whether the pointer is over this object.
The object must have |
onloop | () => voidAlias for onLoop |
onLoop | () => voidUser-assigned function to call when |
onmousedown | nullPIXI.FederatedEventHandler<PIXI.FederatedPointerEvent>Property-based event handler for the |
onmouseenter | nullPIXI.FederatedEventHandler<PIXI.FederatedPointerEvent>Property-based event handler for the |
onmouseleave | nullPIXI.FederatedEventHandler<PIXI.FederatedPointerEvent>Property-based event handler for the |
onmousemove | nullPIXI.FederatedEventHandler<PIXI.FederatedPointerEvent>Property-based event handler for the |
onmouseout | nullPIXI.FederatedEventHandler<PIXI.FederatedPointerEvent>Property-based event handler for the |
onmouseover | nullPIXI.FederatedEventHandler<PIXI.FederatedPointerEvent>Property-based event handler for the |
onmouseup | nullPIXI.FederatedEventHandler<PIXI.FederatedPointerEvent>Property-based event handler for the |
onmouseupoutside | nullPIXI.FederatedEventHandler<PIXI.FederatedPointerEvent>Property-based event handler for the |
onpointercancel | nullPIXI.FederatedEventHandler<PIXI.FederatedPointerEvent>Property-based event handler for the |
onpointerdown | nullPIXI.FederatedEventHandler<PIXI.FederatedPointerEvent>Property-based event handler for the |
onpointerenter | nullPIXI.FederatedEventHandler<PIXI.FederatedPointerEvent>Property-based event handler for the |
onpointerleave | nullPIXI.FederatedEventHandler<PIXI.FederatedPointerEvent>Property-based event handler for the |
onpointermove | nullPIXI.FederatedEventHandler<PIXI.FederatedPointerEvent>Property-based event handler for the |
onpointerout | nullPIXI.FederatedEventHandler<PIXI.FederatedPointerEvent>Property-based event handler for the |
onpointerover | nullPIXI.FederatedEventHandler<PIXI.FederatedPointerEvent>Property-based event handler for the |
onpointertap | nullPIXI.FederatedEventHandler<PIXI.FederatedPointerEvent>Property-based event handler for the |
onpointerup | nullPIXI.FederatedEventHandler<PIXI.FederatedPointerEvent>Property-based event handler for the |
onpointerupoutside | nullPIXI.FederatedEventHandler<PIXI.FederatedPointerEvent>Property-based event handler for the |
onremoved | () => voidCalled when the instance removed from its parent or stage |
onrightclick | nullPIXI.FederatedEventHandler<PIXI.FederatedPointerEvent>Property-based event handler for the |
onrightdown | nullPIXI.FederatedEventHandler<PIXI.FederatedPointerEvent>Property-based event handler for the |
onrightup | nullPIXI.FederatedEventHandler<PIXI.FederatedPointerEvent>Property-based event handler for the |
onrightupoutside | nullPIXI.FederatedEventHandler<PIXI.FederatedPointerEvent>Property-based event handler for the |
ontap | nullPIXI.FederatedEventHandler<PIXI.FederatedPointerEvent>Property-based event handler for the |
ontouchcancel | nullPIXI.FederatedEventHandler<PIXI.FederatedPointerEvent>Property-based event handler for the |
ontouchend | nullPIXI.FederatedEventHandler<PIXI.FederatedPointerEvent>Property-based event handler for the |
ontouchendoutside | nullPIXI.FederatedEventHandler<PIXI.FederatedPointerEvent>Property-based event handler for the |
ontouchmove | nullPIXI.FederatedEventHandler<PIXI.FederatedPointerEvent>Property-based event handler for the |
ontouchstart | nullPIXI.FederatedEventHandler<PIXI.FederatedPointerEvent>Property-based event handler for the |
pivot | number[number, number]{ x: number; y: number }PIXI.PointThe center of rotation, scaling, and skewing for this display object in its local space.
The By default, the pivot is the origin (0, 0). |
playing | booleanIndicates if the AnimatedSprite is currently playing. This is a read-only property that reflects the current playback state. |
position | number[number, number]{ x: number; y: number }PIXI.PointThe coordinate of the object relative to the local coordinates of the parent. |
renderable | booleanControls whether this object can be rendered. If false the object will not be drawn, but the transform will still be updated. This is different from visible, which skips transform updates. |
rotation | numberThe rotation of the object in radians.
|
roundPixels | booleanWhether or not to round the x/y position of the sprite. |
scale | number[number, number]{ x: number; y: number }PIXI.PointThe scale factors of this object along the local coordinate axes. The default scale is (1, 1). |
skew | number[number, number]{ x: number; y: number }PIXI.PointThe skew factor for the object in radians. Skewing is a transformation that distorts the object by rotating it differently at each point, creating a non-uniform shape. |
sortableChildren | booleanIf set to true, the container will sort its children by This actually changes the order of elements in the array of children, so it will affect the rendering order.
|
tabIndex | numberSets the tabIndex of the shadow div. You can use this to set the order of the elements when using the tab key to navigate. |
visible | booleanThe visibility of the object. If false the object will not be drawn, and the transform will not be updated. |
width | numberThe width of the Container, setting this will actually modify the scale to achieve the value set.
|
x | numberThe position of the container on the x axis relative to the local coordinates of the parent. An alias to position.x |
y | numberThe position of the container on the y axis relative to the local coordinates of the parent. An alias to position.y |
zIndex | numberThe zIndex of the container. Controls the rendering order of children within their parent container. A higher value will mean it will be moved towards the front of the rendering order. |
Snippets
| Name | Type |
|---|---|
| children | Snippet<[instance: T]> |
Props
| Name | Description |
|---|---|
anchor | PointLikeThe anchor sets the origin point of the text. |
animationSpeed 1 | numberThe speed that the AnimatedSprite will play at. Higher is faster, lower is slower. |
autoUpdate | booleanWhether to use PIXI.Ticker.shared to auto update animation time |
blendMode 'normal' | stringThe blend mode to be applied to the sprite. Apply a value of 'normal' to reset the blend mode. |
instance | PIXI.AnimatedSpriteThe PIXI.AnimatedSprite instance. Can be set or bound to. WARNING: Type-safety limitation - If you are using a subclass of PIXI.AnimatedSprite, you MUST provide the instance prop with your custom instance. Due to TypeScript's limitations with generic types, if you don't provide an instance, a base PIXI.AnimatedSprite will be created and cast to your type, which will cause runtime errors when trying to access subclass-specific properties or methods. Example:
|
loop true | booleanWhether or not the animate sprite repeats after playing. |
playing true | booleanPlays the animation according to the textures |
roundPixels | booleanIf true PixiJS will Math.floor() x/y values when rendering, stopping pixel interpolation. Advantages can include sharper image quality (like text) and faster rendering on canvas. The main disadvantage is movement of objects may appear less smooth. |
textures [] | PIXI.Texture<PIXI.Resource>[]PIXI.FrameObject[]The array of textures to use |
Additional props are passed on to Container
Slots
| Name | Props | Fallback |
|---|---|---|
| default | {} |
Events
| Name | Type | Detail |
|---|---|---|
| added | forwarded | |
| click | forwarded | |
| complete | dispatched | null |
| create | forwarded | |
| frameChange | dispatched | null |
| globalmousemove | forwarded | |
| globalpointermove | forwarded | |
| globaltouchmove | forwarded | |
| loop | dispatched | null |
| mousedown | forwarded | |
| mousemove | forwarded | |
| mouseout | forwarded | |
| mouseover | forwarded | |
| mouseup | forwarded | |
| mouseupoutside | forwarded | |
| pointercancel | forwarded | |
| pointerdown | forwarded | |
| pointermove | forwarded | |
| pointerout | forwarded | |
| pointerover | forwarded | |
| pointertap | forwarded | |
| pointerup | forwarded | |
| pointerupoutside | forwarded | |
| removed | forwarded | |
| removedFrom | forwarded | |
| rightclick | forwarded | |
| rightdown | forwarded | |
| rightup | forwarded | |
| rightupoutside | forwarded | |
| tap | forwarded | |
| touchcancel | forwarded | |
| touchend | forwarded | |
| touchendoutside | forwarded | |
| touchmove | forwarded | |
| touchstart | forwarded |