Renderer
This is an alternative to using the Application component.
It allows you to use a custom PIXI.Renderer instance but – like its native class – will not
start the render loop or create the root container for you. You will need to do this yourself.
By default it will call PIXI.autoDetectRenderer to determine the instance. Like Application, this
is an asynchronous operation and will call oninit when complete.
Usage
Basic
<script> import { Renderer, Container, Ticker, Text } from 'svelte-pixi' import DraggableCircle from '$lib/components/DraggableCircle.svelte'
let renderer let stage</script>
<Renderer bind:instance={renderer} width={400} height={400} antialias> <!-- use our own ticker to control the render loop --> <Ticker ontick={() => { renderer.render(stage) }} > <!-- the root container is referred to as the "stage", child components can access it with getStage() --> <Container bind:instance={stage}> <DraggableCircle x={200} y={200} /> <Text x={200} y={300} text="Click and drag" style={{ fill: 'white' }} anchor={0.5} /> </Container> </Ticker></Renderer><script> import { Renderer, Container, Ticker, Text } from 'svelte-pixi/svelte-4' import DraggableCircle from '$lib/components/DraggableCircle.svelte'
let renderer let stage</script>
<Renderer bind:instance={renderer} width={400} height={400} antialias> <!-- use our own ticker to control the render loop --> <Ticker on:tick={() => { renderer.render(stage) }} > <!-- the root container is referred to as the "stage", child components can access it with getStage() --> <Container bind:instance={stage}> <DraggableCircle x={200} y={200} /> <Text x={200} y={300} text="Click and drag" style={{ fill: 'white' }} anchor={0.5} /> </Container> </Ticker></Renderer>Custom View
If you want to customize the element that the canvas is rendered into, you can use the view slot. The canvas will be appended as a child of the slot element.
<script> import { Renderer, Container, Ticker, Text } from 'svelte-pixi'
let renderer let stage</script>
<Renderer bind:instance={renderer} width={400} height={400} antialias autoDensity> {#snippet view(canvas)} <div class="custom"> <!-- canvas will be placed here --> </div> {/snippet}
<!-- pixi components go here --> <Container bind:instance={stage}> <Ticker ontick={() => { renderer.render(stage) }} /> <Text x={200} y={200} anchor={0.5} text="Hello World" style={{ fill: 'white' }} /> </Container></Renderer>
<style> .custom :global(canvas) { border: 5px solid tomato; border-radius: 5px; }</style><script> import { Renderer, Container, Ticker, Text } from 'svelte-pixi/svelte-4'
let renderer let stage</script>
<Renderer bind:instance={renderer} width={400} height={400} antialias autoDensity> <div slot="view" class="custom"> <!-- canvas will be placed here --> </div>
<!-- pixi components go here --> <Container bind:instance={stage}> <Ticker on:tick={() => { renderer.render(stage) }} /> <Text x={200} y={200} anchor={0.5} text="Hello World" style={{ fill: 'white' }} /> </Container></Renderer>
<style> .custom :global(canvas) { border: 5px solid tomato; border-radius: 5px; }</style>Render on Demand
The Application component supports rendering on demand. While Renderer doesn’t offer the same render="demand" prop,
you can still implement this manually:
<script> import { Renderer, Container, Ticker, Text } from 'svelte-pixi' import DraggableCircle from '$lib/components/DraggableCircle.svelte'
let renderer let stage let needsRender = true</script>
<Renderer bind:instance={renderer} width={400} height={400} antialias oninvalidate={() => { needsRender = true }}> <Ticker ontick={() => { if (needsRender) { renderer.render(stage) needsRender = false // check out the console console.log('render') } }} > <Container bind:instance={stage}> <DraggableCircle x={200} y={200} /> <Text x={200} y={300} text="Click and drag" style={{ fill: 'white' }} anchor={0.5} /> </Container> </Ticker></Renderer><script> import { Renderer, Container, Ticker, Text } from 'svelte-pixi/svelte-4' import DraggableCircle from '$lib/components/DraggableCircle.svelte'
let renderer let stage let needsRender = true</script>
<Renderer bind:instance={renderer} width={400} height={400} antialias on:invalidate={() => { needsRender = true }}> <Ticker on:tick={() => { if (needsRender) { renderer.render(stage) needsRender = false // check out the console console.log('render') } }} > <Container bind:instance={stage}> <DraggableCircle x={200} y={200} /> <Text x={200} y={300} text="Click and drag" style={{ fill: 'white' }} anchor={0.5} /> </Container> </Ticker></Renderer>See Render on Demand for more information.
API
Props
* denotes required
| Name | Description |
|---|---|
antialias | booleanWhether to enable anti-aliasing. This may affect performance. |
autoDensity | booleanResizes renderer view in CSS pixels to allow for resolutions other than 1. |
background | stringnumbernumber[]Float32ArrayUint8ArrayUint8ClampedArrayPIXI.HslColorPIXI.HslaColorPIXI.HsvColorPIXI.HsvaColorPIXI.RgbColorPIXI.RgbaColorPIXI.ColorAlias for backgroundColor |
backgroundAlpha | numberTransparency of the background color, value from |
backgroundColor | stringnumbernumber[]Float32ArrayUint8ArrayUint8ClampedArrayPIXI.HslColorPIXI.HslaColorPIXI.HsvColorPIXI.HsvaColorPIXI.RgbColorPIXI.RgbaColorPIXI.ColorThe background color used to clear the canvas. See {@link ColorSource} for accepted color values. |
bezierSmoothness | numberA value from 0 to 1 that controls the smoothness of bezier curves (the higher the smoother) |
canvas | PIXI.ICanvasThe canvas to use as a view, optional. |
clearBeforeRender | booleanWhether to clear the canvas before new render passes. |
context | nullWebGL2RenderingContextUser-provided WebGL rendering context object. |
depth | booleanWhether to ensure the main view has can make use of the depth buffer. Always true for WebGL renderer. |
eventFeatures | Partial<PIXI.EventSystemFeatures>The event features that are enabled by the EventSystem. |
eventMode | "none""passive""auto""static""dynamic"The default event mode for all display objects. |
failIfMajorPerformanceCaveat | boolean |
forceFallbackAdapter | booleanForce the use of the fallback adapter |
height | numberThe height of the screen. |
hello | booleanWhether to log the version and type information of renderer to console. |
instance | TThe PIXI.Renderer instance. Can be set or bound to. By default PIXI.autoDetectRenderer() is called and sets the instance when it resolves |
manageImports | boolean |
multiView | booleanWhether to enable multi-view rendering. Set to true when rendering to multiple canvases on the dom. |
oninit | (ev: { renderer: T; }) => voidCalled when PIXI.autoDetectRenderer() resolves (only if no |
oninvalidate | () => voidCalled whenever the renderer needs to be redrawn because of a change |
onpostrender | (item: unknown) => voidEvent handler for the postrender PIXI.Runner |
onprerender | (item: unknown) => voidEvent handler for the prerender PIXI.Runner |
onrender | (item: unknown) => voidEvent handler for the render PIXI.Runner |
onrenderstart | (item: unknown) => voidEvent handler for the renderStart PIXI.Runner |
powerPreference | "low-power""high-performance"An optional hint indicating what configuration of GPU is suitable for the WebGL context,
can be |
preference | "webgpu""webgl"The preferred renderer type. WebGPU is recommended as its generally faster than WebGL. |
preferWebGLVersion | 12The preferred WebGL version to use. |
premultipliedAlpha | booleanWhether the compositor will assume the drawing buffer contains colors with premultiplied alpha. |
preserveDrawingBuffer | booleanWhether to enable drawing buffer preservation. If enabled, the drawing buffer will preserve
its value until cleared or overwritten. Enable this if you need to call |
renderableGCActive | booleanIf set to true, this will enable the garbage collector on the GPU. |
renderableGCFrequency | numberFrames between two garbage collections. |
renderableGCMaxUnusedTime | numberThe maximum idle frames before a texture is destroyed by garbage collection. |
resolution | numberThe resolution / device pixel ratio of the renderer. |
roundPixels | boolean |
skipExtensionImports | booleanWhether to stop PixiJS from dynamically importing default extensions for the renderer. It is false by default, and means PixiJS will load all the default extensions, based on the environment e.g browser/webworker. If you set this to true, then you will need to manually import the systems and extensions you need. e.g.
|
textureGCActive | booleanIf set to true, this will enable the garbage collector on the GPU. |
textureGCAMaxIdle | number |
textureGCCheckCountMax | numberFrames between two garbage collections. |
textureGCMaxIdle | numberThe maximum idle frames before a texture is destroyed by garbage collection. |
useBackBuffer | booleanif true will use the back buffer where required |
webgl | Partial<PIXI.WebGLOptions>Optional WebGLOptions to pass only to the WebGL renderer |
webgpu | Partial<PIXI.WebGPUOptions>Optional WebGPUOptions to pass only to WebGPU renderer. |
width | numberThe width of the screen. |
Snippets
| Name | Type |
|---|---|
| children | Snippet<[]> |
| loading | Snippet<[]> |
| view | Snippet<[]> |
Props
| Name | Description |
|---|---|
antialias | Sets antialias |
autoDensity | Resizes renderer view in CSS pixels to allow for resolutions other than 1. |
background | PIXI.ColorSourceAlias for backgroundColor |
backgroundAlpha | Value from 0 (fully transparent) to 1 (fully opaque). |
backgroundColor | The background color of the rendered area (shown if not transparent). |
bezierSmoothness | numberA value from 0 to 1 that controls the smoothness of bezier curves (the higher the smoother) |
clearBeforeRender | booleanThis sets if the renderer will clear the canvas or not before the new render pass. |
context | WebGL2RenderingContextUser-provided WebGL rendering context object. |
depth | booleanWhether to ensure the main view has can make use of the depth buffer. Always true for WebGL renderer. |
eventFeatures | PIXI.EventSystemOptions['eventFeatures']The event features that are enabled by the EventSystem. |
eventMode | PIXI.EventModeThe default event mode mode for all display objects. This option only is available when using @pixi/events package (included in the pixi.js and pixi.js-legacy bundle), otherwise it will be ignored. |
failIfMajorPerformanceCaveat | boolean |
forceFallbackAdapter | booleanForce the use of the fallback adapter |
height | The height of the renderers view. |
hello | booleanWhether to log the version and type information of renderer to console. |
instance | The PIXI.Renderer instance. Can be set or bound to. By default it uses PIXI.autoDetectRenderer() |
multiView | booleanWhether to enable multi-view rendering. Set to true when rendering to multiple canvases on the dom. |
powerPreference | WebGLPowerPreferenceParameter passed to webgl context, set to "high-performance" for devices with dual graphics card. (WebGL only). |
preference | 'webgl''webgpu'The preferred renderer type. WebGPU is recommended as its generally faster than WebGL. |
preferWebGLVersion | 12The preferred WebGL version to use. |
premultipliedAlpha | booleanWebGL Only. Whether the compositor will assume the drawing buffer contains colors with premultiplied alpha. |
preserveDrawingBuffer | Enables drawing buffer preservation, enable this if you need to call toDataUrl on the WebGL context. |
renderableGCActive | booleanIf set to true, this will enable the garbage collector on the GPU. |
renderableGCFrequency | numberFrames between two garbage collections. |
renderableGCMaxUnusedTime | The maximum idle frames before a texture is destroyed by garbage collection. @default 60 |
resolution | numberThe resolution / device pixel ratio of the renderer. |
roundPixels | |
skipExtensionImports | booleanWhether to stop PixiJS from dynamically importing default extensions for the renderer. It is false by default, and means PixiJS will load all the default extensions, based on the environment e.g browser/webworker. If you set this to true, then you will need to manually import the systems and extensions you need. |
textureGCActive | booleanIf set to true, this will enable the garbage collector on the GPU. |
textureGCCheckCountMax | numberFrames between two garbage collections. |
textureGCMaxIdle | numberThe maximum idle frames before a texture is destroyed by garbage collection. |
useBackBuffer | booleanif true will use the back buffer where required |
webgl | PIXI.WebGLRendererOptionsOptional WebGLOptions to pass only to the WebGL renderer |
webgpu | PIXI.WebGPUOptionsOptional WebGPUOptions to pass only to WebGPU renderer. |
width | The width of the renderers view. |
Slots
| Name | Props | Fallback |
|---|---|---|
| default | {} | |
| loading | {} | |
| view | {} |
Events
| Name | Type | Detail |
|---|---|---|
| init | dispatched | |
| invalidate | forwarded | |
| postrender | dispatched | null |
| prerender | dispatched | null |
| render | dispatched | null |
| renderStart | dispatched | null |