### Install SveltePixi with Vite Template
Source: https://github.com/mattjennings/svelte-pixi/blob/master/docs/src/content/docs/getting-started/installation.mdx
This snippet demonstrates how to initialize a new SveltePixi project using the Vite template. It involves cloning the template, installing dependencies, and starting the development server. No specific inputs or outputs are detailed beyond the standard NPM commands.
```bash
npx degit mattjennings/svelte-pixi/examples/vite
npm install
npm run dev
```
--------------------------------
### Install Dependencies and Run Dev Server
Source: https://github.com/mattjennings/svelte-pixi/blob/master/README.md
These commands are used to install project dependencies using pnpm and then start the development server for the SveltePixi documentation website. After installation, the 'pnpm run dev' command makes the documentation accessible locally for development.
```bash
pnpm install
pnpm run dev
```
--------------------------------
### External Store Example in Svelte
Source: https://github.com/mattjennings/svelte-pixi/blob/master/examples/vite/README.md
Demonstrates how to create a simple external store using Svelte's `writable` function. This is recommended for preserving component state during Hot Module Replacement (HMR).
```javascript
// store.js
// An extremely simple external store
import { writable } from 'svelte/store'
export default writable(0)
```
--------------------------------
### Install SveltePixi Dependencies
Source: https://github.com/mattjennings/svelte-pixi/blob/master/docs/src/content/docs/getting-started/installation.mdx
This snippet shows the commands to add SveltePixi and its core dependency, PixiJS, to an existing Svelte project. It assumes the user already has a Svelte project set up and needs to integrate the PixiJS capabilities.
```bash
npm install pixi.js svelte-pixi
```
--------------------------------
### SveltePixi: Setup PixiJS Application with Auto Rendering
Source: https://context7.com/mattjennings/svelte-pixi/llms.txt
This snippet shows how to create a PixiJS application using the SveltePixi Application component. It handles canvas setup and automatic rendering. Dependencies include 'svelte-pixi' and 'pixi.js'. It takes props like width, height, and background color.
```svelte
{#if texture}
{/if}
```
--------------------------------
### Install Custom Pixi.js Dependencies
Source: https://github.com/mattjennings/svelte-pixi/blob/master/docs/src/content/docs/guides/reducing-bundle-size.mdx
This command installs all the necessary individual '@pixi/*' packages that are exported in the custom pixi.js build. Running this ensures that all required modules are available for the custom build to function correctly.
```sh
npm install @pixi/accessibility @pixi/app @pixi/assets @pixi/basis @pixi/compressed-textures @pixi/core @pixi/display @pixi/events @pixi/extract @pixi/graphics @pixi/mesh @pixi/particle-container @pixi/prepare @pixi/sprite-animated @pixi/sprite-tiling @pixi/sprite @pixi/spritesheet @pixi/text-bitmap @pixi/text
```
--------------------------------
### Svelte Pixi Container Usage Example
Source: https://github.com/mattjennings/svelte-pixi/blob/master/docs/src/content/docs/api/components/container.mdx
Demonstrates how to use the Container component in svelte-pixi to group and position Text elements. Child elements' coordinates are relative to the parent Container.
```svelte
```
--------------------------------
### Install pnpm Package Manager
Source: https://github.com/mattjennings/svelte-pixi/blob/master/README.md
This snippet shows how to install the pnpm package manager globally using npm. pnpm is a requirement for developing and running the SveltePixi project. Ensure Node.js and npm are installed before executing this command.
```bash
npm i -g pnpm
```
--------------------------------
### SveltePixi: Render Sprites with Reactive Properties
Source: https://context7.com/mattjennings/svelte-pixi/llms.txt
This example demonstrates rendering sprites using the SveltePixi Sprite component. It allows for reactive properties like position, rotation, and scale. Event handling for user interactions like pointerdown is also supported. Dependencies include 'svelte-pixi' and 'pixi.js'.
```svelte
{#if texture}
console.log('Sprite clicked!', e)}
/>
{/if}
```
--------------------------------
### Get PIXI Application Context using getApp in JavaScript
Source: https://github.com/mattjennings/svelte-pixi/blob/master/docs/src/content/docs/api/utilities/get-app.mdx
This snippet demonstrates how to import and use the getApp function from 'svelte-pixi' to retrieve the PIXI.Application instance. The returned object contains the 'app' property, which is the PIXI.Application instance.
```javascript
import { getApp } from 'svelte-pixi'
const { app } = getApp()
```
--------------------------------
### Basic Svelte Pixi Application Setup
Source: https://github.com/mattjennings/svelte-pixi/blob/master/docs/src/content/docs/api/components/application.mdx
This snippet demonstrates the basic usage of the Svelte Pixi Application component. It initializes a PixiJS application with a specified width and height, enabling antialiasing, and renders a simple 'Hello World' text element within it. No external dependencies are required beyond `svelte-pixi`.
```svelte
```
--------------------------------
### Initialize AssetsLoader in Svelte with PixiJS v7
Source: https://github.com/mattjennings/svelte-pixi/blob/master/docs/src/content/docs/releases/v7-0-0.md
Demonstrates how to initialize PIXI.Assets with a base URL and use the AssetsLoader component in Svelte for loading assets like sprite sheets. It includes a loading progress indicator and displays a sprite once loaded. This example assumes PixiJS v7 compatibility.
```svelte
{#await initPromise then}
{/await}
```
--------------------------------
### Render on Demand with Renderer
Source: https://github.com/mattjennings/svelte-pixi/blob/master/docs/src/content/docs/api/components/renderer.mdx
Implements render-on-demand functionality with the Renderer component by manually controlling rendering based on a `needsRender` flag and an `invalidate` event. This example logs 'render' to the console each time a render occurs.
```svelte
{
needsRender = true
}}
>
{
if (needsRender) {
renderer.render(stage)
needsRender = false
// check out the console
console.log('render')
}
}}
>
```
--------------------------------
### Demand Rendering Optimization in Svelte-Pixi
Source: https://context7.com/mattjennings/svelte-pixi/llms.txt
This example illustrates how to optimize performance in Svelte-Pixi applications by enabling demand rendering. The canvas only updates when reactive properties change, reducing unnecessary re-renders. It shows how to load a sprite asset and move it to a random position upon clicking, updating a render count display. This is particularly useful for applications where frequent visual updates are not always required.
```svelte
{#if texture}
{/if}
```
--------------------------------
### Load Assets with Progress and Display
Source: https://github.com/mattjennings/svelte-pixi/blob/master/docs/src/content/docs/api/components/assets-loader.mdx
Demonstrates how to use the AssetsLoader component to load an image asset ('/assets/great-success.png'). It includes a 'loading' slot to display the loading progress percentage and a default slot to render the loaded Sprite once complete. This example requires svelte-pixi and pixi.js.
```svelte
```
--------------------------------
### Access PixiJS Instance with bind:instance in Svelte
Source: https://github.com/mattjennings/svelte-pixi/blob/master/docs/src/content/docs/getting-started/usage.mdx
Explains how to get a reference to the underlying PixiJS instance of a Svelte Pixi component using the `bind:instance` directive. This allows direct manipulation of the PixiJS object, similar to how `bind:this` works for DOM elements. This example modifies the fill color of a Text instance after the component mounts.
```svelte
```
--------------------------------
### Svelte Pixi SimplePlane Usage Example
Source: https://github.com/mattjennings/svelte-pixi/blob/master/docs/src/content/docs/api/components/simple-plane.mdx
Demonstrates how to use the SimplePlane component to draw a texture and animate its vertices over time. It requires svelte-pixi and pixi.js. The component takes texture, vertex count, and position as props, and the animation logic updates vertex positions based on time.
```svelte
```
--------------------------------
### Svelte Pixi TilingSprite Usage Example
Source: https://github.com/mattjennings/svelte-pixi/blob/master/docs/src/content/docs/api/components/tiling-sprite.mdx
This Svelte component demonstrates the usage of TilingSprite from svelte-pixi. It utilizes PixiJS for rendering and updates the sprite's scale and position dynamically using the onTick function. Dependencies include svelte-pixi, PixiJS, and the Astro component for API display.
```svelte
import { onMount } from 'svelte'
import { TilingSprite, AssetsLoader, onTick } from 'svelte-pixi'
import * as PIXI from 'pixi.js'
let count = 0
let scale = { x: 1, y: 1 }
let tilePosition = { x: 0, y: 0 }
onTick((delta) => {
count += 0.01 * delta
scale = { x: 2 + Math.sin(count), y: 2 + Math.cos(count) }
tilePosition = { x: tilePosition.x + 1, y: tilePosition.y + 1 }
})
```
```html
```
--------------------------------
### Draw a dynamic circle with Svelte Pixi Graphics
Source: https://github.com/mattjennings/svelte-pixi/blob/master/docs/src/content/docs/api/components/graphics.mdx
This example demonstrates how to use the Svelte Pixi Graphics component to draw a circle whose size animates over time. It utilizes the `draw` prop to interact with the PIXI.Graphics API and `onTick` for animation updates. No external dependencies are required beyond svelte-pixi.
```svelte
{
graphics.clear()
graphics.beginFill(0xde3249)
graphics.drawCircle(0, 0, size)
graphics.endFill()
}}
/>
```
--------------------------------
### Use Custom PixiJS Instance in Svelte
Source: https://github.com/mattjennings/svelte-pixi/blob/master/docs/src/content/docs/getting-started/usage.mdx
Demonstrates how to provide a custom, pre-instantiated PixiJS object to a Svelte Pixi component using the `instance` prop. This is useful for integrating with custom PixiJS classes or third-party libraries that extend PixiJS. The example uses a custom `GreenText` class that inherits from `PIXI.Text`.
```svelte
```
--------------------------------
### SveltePixi: Draw Vector Shapes with Graphics Component
Source: https://context7.com/mattjennings/svelte-pixi/llms.txt
This example showcases the SveltePixi Graphics component for drawing vector shapes programmatically. It utilizes a `draw` function prop that receives a graphics object to perform drawing operations like `clear`, `beginFill`, and `drawCircle`/`drawRect`. Dependencies include 'svelte-pixi'.
```svelte
size += 10}
/>
```
--------------------------------
### Handle Mouse, Pointer, and Touch Events in Svelte-Pixi
Source: https://context7.com/mattjennings/svelte-pixi/llms.txt
This snippet demonstrates how to handle interactive events such as pointerdown, pointerup, pointermove, and click events on display objects within a Svelte-Pixi application. It utilizes Svelte's reactive system to update the UI based on user interactions, including hover states and click counts. The example showcases drag-and-drop functionality and visual feedback for hover effects.
```svelte
isHovered = true}
on:pointerout={() => isHovered = false}
on:click={() => clickCount++}
>
```
--------------------------------
### Create Custom Pixi.js Build
Source: https://github.com/mattjennings/svelte-pixi/blob/master/docs/src/content/docs/guides/reducing-bundle-size.mdx
This JavaScript code exports specific modules from various '@pixi/*' packages to create a minimized pixi.js build. This allows developers to include only the features they need, reducing the overall bundle size. Ensure all listed dependencies are installed.
```javascript
// custom pixi.js file
export * from '@pixi/accessibility'
export * from '@pixi/app'
export * from '@pixi/assets'
export * from '@pixi/basis'
export * from '@pixi/compressed-textures'
export * from '@pixi/core'
export * from '@pixi/display'
export * from '@pixi/events'
export * from '@pixi/extract'
export * from '@pixi/graphics'
export * from '@pixi/mesh'
export * from '@pixi/particle-container'
export * from '@pixi/prepare'
export * from '@pixi/sprite-animated'
export * from '@pixi/sprite-tiling'
export * from '@pixi/sprite'
export * from '@pixi/spritesheet'
export * from '@pixi/text-bitmap'
export * from '@pixi/text'
```
--------------------------------
### Customizing Canvas View in Svelte Pixi Application
Source: https://github.com/mattjennings/svelte-pixi/blob/master/docs/src/content/docs/api/components/application.mdx
This example shows how to customize the DOM element where the PixiJS canvas is rendered using the `view` slot. The canvas will be appended as a child to the element provided in the slot. It also includes CSS to style the canvas itself. Dependencies: `svelte-pixi`.
```svelte
```
--------------------------------
### Render Custom Mesh with Geometry and Shader in Svelte
Source: https://github.com/mattjennings/svelte-pixi/blob/master/docs/src/content/docs/api/components/mesh.mdx
This snippet demonstrates how to create and render a custom mesh using svelte-pixi. It defines geometry with vertex positions and UVs, a custom vertex and fragment shader, and updates uniforms over time. This example is suitable for Svelte applications and requires the 'svelte-pixi' and 'pixi.js' libraries.
```svelte
```
--------------------------------
### Svelte Pixi ParticleContainer Usage Example
Source: https://github.com/mattjennings/svelte-pixi/blob/master/docs/src/content/docs/api/components/particle-container.mdx
Demonstrates how to use the ParticleContainer component in Svelte for rendering a large number of stars. It initializes stars with random positions and updates their scale, anchor, and screen position based on camera movement for a 3D starfield effect. Performance is optimized by directly managing Pixi instances for thousands of components.
```svelte
```
--------------------------------
### Ticker Usage with Text Updates
Source: https://github.com/mattjennings/svelte-pixi/blob/master/docs/src/content/docs/api/components/ticker.mdx
This example demonstrates how to use the Ticker component to update a Text element on each frame. It imports Ticker and Text from 'svelte-pixi' and uses the 'on:tick' event to update frame count and delta time. The Text component displays these values, and its position and style are configured.
```svelte
{
frame++
delta = ev.detail
}}>
```
--------------------------------
### Animate Sprite using onTick in Svelte Pixi
Source: https://github.com/mattjennings/svelte-pixi/blob/master/docs/src/content/docs/getting-started/usage.mdx
Demonstrates how to create animations by hooking into the PixiJS application's update loop using the `onTick` function. The `onTick` callback receives the time delta since the last frame, allowing for framerate-independent movement. This example animates a sprite in a circular path.
```svelte
```
--------------------------------
### NineSlicePlane Component Usage in Svelte
Source: https://github.com/mattjennings/svelte-pixi/blob/master/docs/src/content/docs/api/components/nine-slice-plane.mdx
Demonstrates how to use the NineSlicePlane component in a Svelte application. It requires PixiJS and svelte-pixi to be installed. The component takes a texture and defines stretchable areas via `leftWidth`, `topHeight`, `rightWidth`, and `bottomHeight`. The example dynamically updates the width and height for a visual effect.
```svelte
```
--------------------------------
### Render Text with Svelte Pixi
Source: https://github.com/mattjennings/svelte-pixi/blob/master/docs/src/content/docs/api/components/text.mdx
Demonstrates how to use the Text component from svelte-pixi to render "Hello World" on the canvas. It showcases basic positioning, text content, anchor point, and style customization like text fill color.
```svelte
```
--------------------------------
### Context Helpers for PixiJS Instance Access
Source: https://context7.com/mattjennings/svelte-pixi/llms.txt
Access parent PixiJS instances using context helpers within Svelte components. Functions like getApp, getContainer, and getStage allow retrieving the PIXI.Application, the current Container, and the root Stage respectively. This is useful for interacting with the PixiJS rendering context from child components.
```svelte
{#if texture}
{/if}
```
--------------------------------
### Get Parent Container Instance with svelte-pixi
Source: https://github.com/mattjennings/svelte-pixi/blob/master/docs/src/content/docs/api/utilities/get-container.mdx
Retrieves the PIXI.Container instance of the parent component. This is useful for components that need to interact with or manipulate their parent container, such as Sprites or Graphics. It relies on the svelte-pixi library.
```javascript
import { getContainer } from 'svelte-pixi'
const { container } = getContainer()
```
--------------------------------
### Get Renderer Context with JavaScript
Source: https://github.com/mattjennings/svelte-pixi/blob/master/docs/src/content/docs/api/utilities/get-renderer.mdx
Retrieves the PIXI.Renderer instance and an invalidate function from the parent Renderer component. The `renderer` is a PIXI.Renderer object, and `invalidate` is a function to trigger a manual render if using Svelte Pixi's Application.
```javascript
import { getRenderer } from 'svelte-pixi'
const { renderer, invalidate } = getRenderer()
```
--------------------------------
### Initialize PIXI.Assets with Base Path
Source: https://github.com/mattjennings/svelte-pixi/blob/master/docs/src/content/docs/api/components/assets-loader.mdx
Shows how to initialize PIXI.Assets with a custom base path before rendering the AssetsLoader. The initialization is asynchronous and must be awaited. This ensures that all subsequent asset loading uses the specified base path. It requires svelte-pixi and pixi.js.
```svelte
{#await initPromise then}
{/await}
```
--------------------------------
### Basic Renderer Usage with Ticker
Source: https://github.com/mattjennings/svelte-pixi/blob/master/docs/src/content/docs/api/components/renderer.mdx
Demonstrates the basic usage of the Renderer component, binding to its instance and stage, and utilizing a Ticker to manually control the render loop. It includes a DraggableCircle and Text component within a Container.
```svelte
{
renderer.render(stage)
}}
>
```
--------------------------------
### Display Animation from Spritesheet with Svelte
Source: https://github.com/mattjennings/svelte-pixi/blob/master/docs/src/content/docs/api/components/animated-sprite.mdx
Demonstrates how to display an animation using a spritesheet in svelte-pixi. It requires loading the spritesheet using AssetsLoader and then providing an array of textures to the AnimatedSprite component. The animation can be controlled with properties like 'playing' and 'animationSpeed'.
```svelte
```
--------------------------------
### AssetsLoader Component for Asset Loading with Progress
Source: https://context7.com/mattjennings/svelte-pixi/llms.txt
Load assets with progress tracking and conditional rendering. This component uses Svelte-Pixi and PixiJS to manage asset loading. It accepts an array of assets to load, a bundle name, and an option to unload assets after loading. It provides progress updates and allows rendering of content only after assets are loaded.
```svelte
handleProgress(e.detail)}
let:progress
>
```
--------------------------------
### SveltePixi: Display Styled Text with Text Component
Source: https://context7.com/mattjennings/svelte-pixi/llms.txt
This snippet demonstrates using the SveltePixi Text component to display styled text. It allows for customization of font properties, colors, effects like drop shadows, and text wrapping. Dependencies include 'svelte-pixi'.
```svelte
fontSize += 5}
/>
```
--------------------------------
### Render a Sprite with Texture in Svelte
Source: https://github.com/mattjennings/svelte-pixi/blob/master/docs/src/content/docs/api/components/sprite.mdx
Demonstrates how to use the Sprite component from svelte-pixi to render an image on the screen. It requires PixiJS to be imported and specifies properties like position, texture, anchor, and scale.
```svelte
```
--------------------------------
### Render Bitmap Text with Svelte Pixi
Source: https://github.com/mattjennings/svelte-pixi/blob/master/docs/src/content/docs/api/components/bitmap-text.mdx
This snippet demonstrates how to use the BitmapText component in Svelte Pixi to render text. It requires loading font assets beforehand using AssetsLoader and specifies font properties like name, size, and alignment. The text can include line breaks.
```svelte
```
--------------------------------
### Render Many Sprites with Svelte Components
Source: https://github.com/mattjennings/svelte-pixi/blob/master/docs/src/content/docs/guides/optimizing-performance.mdx
Demonstrates rendering a large number of stars using individual Svelte Sprite components. This approach can lead to performance issues due to excessive prop updates and component lifecycle management for thousands of elements. It serves as a baseline for performance comparison.
```svelte
{#each stars as star}
{@const z = star.z - cameraZ}
{@const distance = Math.max(0, (2000 - z) / 2000)}
{/each}
```
--------------------------------
### Preload Assets with Svelte Pixi AssetsLoader
Source: https://github.com/mattjennings/svelte-pixi/blob/master/docs/src/content/docs/getting-started/usage.mdx
Illustrates how to preload multiple assets using the `AssetsLoader` component in Svelte Pixi. This component displays a loading state (via the `loading` slot) until all specified assets are ready, after which it renders its default content. This prevents assets from appearing one by one.
```svelte
{#each images as image, i}
{/each}
```
--------------------------------
### Configure Vite Alias for Custom Pixi.js
Source: https://github.com/mattjennings/svelte-pixi/blob/master/docs/src/content/docs/guides/reducing-bundle-size.mdx
This JavaScript configuration updates the Vite build process to use the custom pixi.js file. By setting up an alias, Vite will resolve 'pixi.js' requests to the custom file, ensuring that the optimized build is used. The `optimizeDeps.exclude` setting is crucial for SveltePixi to correctly adopt the alias.
```javascript
// vite.config.js
import { defineConfig } from 'vite'
export default defineConfig({
/* ... */
optimizeDeps: {
// required so svelte-pixi will use pixi.js alias
exclude: ['svelte-pixi'],
},
resolve: {
alias: {
// update path to wherever your pixi.js is
'pixi.js': '/src/pixi.js',
},
},
})
```