Skip to content

ModuleCore / HostsConcept: Platforms

Surfaces

A canvas configured for your root, with a drawing buffer that follows its layout, on the web and React Native.

On this page
import
import * as web from '@typegpu/kit/web'
call
web.surface(root, canvas, options?) → Surfacealso paced · orbitControls · pointer · web.host · web.video
facts
allocateseach framelifecycledestroy()platformwebonly
reference
Every call, option and member 38 options · 26 membersHow to read this

A page lays out a canvas in CSS pixels, while its drawing buffer measures canvas.width by canvas.height device pixels. Layout, screen and zoom changes can make those sizes disagree, blurring or stretching the picture. A surface configures the canvas for your root and keeps the drawing buffer matched to its layout.

The drawing buffer's pixels over the screen's, under a pixel-ratio cap

Try Raise the phone's cap to 3. Each buffer pixel then sits on one screen pixel, and the buffer holds 2.25 times as many. The cap never adds pixels to the laptop or the monitor.
import * as
import web
web
from '@typegpu/kit/web';
const
const canvas: HTMLCanvasElement
canvas
=
var document: Document

window.document returns a reference to the document contained in the window.

MDN Reference

document
.
ParentNode.querySelector<"canvas">(selectors: "canvas"): HTMLCanvasElement | null (+4 overloads)

Returns the first element that is a descendant of node that matches selectors.

MDN Reference

querySelector
('canvas')!;
const
const surface: web.Surface
surface
=
import web
web
.
function surface(root: TgpuRoot, canvas: HTMLCanvasElement, options?: web.SurfaceOptions): web.Surface

Configures a canvas for the root. Matches its drawing buffer to the layout size or the fixed options.size. Throws if the fixed size is invalid.

@param ― canvas the canvas it configures and sizes

@returns ― a surface you draw into and destroy

surface
(
const root: TgpuRoot
root
,
const canvas: HTMLCanvasElement
canvas
);
const
const loop: frames.Frames
loop
=
import frames
frames
.
function create(root: TgpuRoot, record: Recorder): frames.Frames

Records a frame into one encoder and submits once, with a clock. Borrows the root. The synchronous recorder borrows each frame's encoder.

@submits ― always once a frame after your recorder returns, and again at each frame.submit()

@param ― record your frame, recorded into each frame's encoder

@returns ― a loop you run with each frame's time and destroy

create
(
const root: TgpuRoot
root
, (
frame: frames.Frame
frame
) => {
const
const pass: TgpuRenderPass
pass
=
frame: frames.Frame
frame
.
Frame.encoder: TgpuCommandEncoder

Records commands for submission after the recorder returns. submit() replaces this encoder. Read it at each call instead of keeping it, and let the frame submit it.

encoder
.
TgpuCommandEncoder.beginRenderPass(descriptor: TgpuRenderPassDescriptor): TgpuRenderPass

Begins recording a render pass. Attachment views accept TypeGPU textures, texture views and canvas contexts, next to raw

GPUTextureView

s.

beginRenderPass
({
TgpuRenderPassDescriptor.colorAttachments?: ColorAttachment | readonly (ColorAttachment | null)[] | undefined
colorAttachments
: {
ColorAttachment.view: GPUTextureView | (ColorTextureConstraint & RenderFlag) | TgpuTextureView<WgslTexture<WgslTextureProps>> | TgpuTextureRenderView | GPUCanvasContext

A

GPUTextureView

describing the texture subresource that will be output to for this color attachment.

view
:
const surface: web.Surface
surface
.
Surface.view(frame: frames.Frame): GPUTextureView

Returns a view of the current canvas texture, created once per texture. Applies pending size changes first, which a pass using context would skip.

view
(
frame: frames.Frame
frame
) } });
// draw
const pass: TgpuRenderPass
pass
.
TgpuRenderPass.end(): void

Completes the recording of this render pass

end
();
});
import frames
frames
.
function animate(loop: Pick<frames.Frames, "run"> & Partial<Pick<frames.Frames, "seconds">>, options?: frames.AnimateOptions): frames.Animation
export animate

Runs the loop on each scheduled frame. Time advances by at most maxDt per frame, never steps back, and stops while the surface is paused. Destroying the animation keeps the loop alive. Another animation on the same loop continues from its recorded time. A frame error, or a run that returns false because the loop was destroyed, stops the animation.

@param ― loop the loop to run on each unpaused frame. Continues from its seconds if it ran before

@returns ― an animation you destroy to stop the frames

animate
(
const loop: frames.Frames
loop
); // runs the loop at every animation frame
  • The drawing buffer is the layout size times the screen’s pixel ratio, capped at maxPixelRatio, 2 by default. On a 3× phone that leaves about 44% of the full-resolution pixels, so full-size use less than half the memory.
  • Multiply sizes in points, such as dots, hairlines and margins, by the surface’s pixelRatio. The screen’s own ratio can exceed the cap.
  • maxPixelRatio caps the ratio but never raises it above the screen’s ratio. To render more pixels, draw into a target sized with targets.scaled(surface, 2) and filter it down. To use the same size on every screen, fix the size.
  • The size is clamped to the device’s maxTextureDimension2D.
  • For fewer pixels, or a resolution that changes while the game runs, render at a resolution below the surface.
flow-field65 536 particles flowing along Perlin noise.
This canvas hosts the flow field scene. The loupe shows drawing-buffer pixels one for one with screen pixels, up to a pixel ratio of 2. Point size is measured in drawing-buffer pixels.

A benchmark, GPU capture or offline render needs a fixed pixel size, such as 2560 × 1440. Pass size in pixels to set the drawing buffer before surface returns. Layout and pixel ratio changes leave it unchanged. The canvas’s CSS size still scales the image on screen. The benchmark below steps frames by hand:

import * as
import web
web
from '@typegpu/kit/web';
const
const surface: web.Surface
surface
=
import web
web
.
function surface(root: TgpuRoot, canvas: HTMLCanvasElement, options?: web.SurfaceOptions): web.Surface

Configures a canvas for the root. Matches its drawing buffer to the layout size or the fixed options.size. Throws if the fixed size is invalid.

@param ― canvas the canvas it configures and sizes

@returns ― a surface you draw into and destroy

surface
(
const root: TgpuRoot
root
,
const canvas: HTMLCanvasElement
canvas
, {
SurfaceOptions.size?: web.FixedSize | undefined

Fixes the drawing-buffer size before surface returns. The canvas CSS size still scales it on screen. Default the layout size multiplied by the capped pixel ratio, following layout changes.

size
: {
FixedSize.width: number
width
: 2560,
FixedSize.height: number
height
: 1440 } });
const
const scene: {
frame(frame: Frame): void;
}
scene
=
function createScene(root: TgpuRoot, surface: Surface): {
frame(frame: Frame): void;
}
createScene
(
const root: TgpuRoot
root
,
const surface: web.Surface
surface
);
const
const loop: frames.Frames
loop
=
import frames
frames
.
function create(root: TgpuRoot, record: Recorder): frames.Frames

Records a frame into one encoder and submits once, with a clock. Borrows the root. The synchronous recorder borrows each frame's encoder.

@submits ― always once a frame after your recorder returns, and again at each frame.submit()

@param ― record your frame, recorded into each frame's encoder

@returns ― a loop you run with each frame's time and destroy

create
(
const root: TgpuRoot
root
, (
frame: frames.Frame
frame
) =>
const scene: {
frame(frame: Frame): void;
}
scene
.
function frame(frame: Frame): void
frame
(
frame: frames.Frame
frame
));
for (let
let i: number
i
= 0;
let i: number
i
< 600;
let i: number
i
++)
const loop: frames.Frames
loop
.
Frames.run(seconds: number): boolean

Records and submits one frame with a fresh Frame and encoder. Requires finite, nondecreasing seconds. Throws for an invalid time, a nested frame or an asynchronous recorder. Returns false if the loop is destroyed before or during recording. Runs all after-submit callbacks. If any throw, the frame still counts and run rethrows their errors.

run
(
let i: number
i
/ 60);
await
const root: TgpuRoot
root
.
TgpuRoot.device: GPUDevice

The GPU device associated with this root.

device
.
GPUDevice.queue: GPUQueue

The queue read-only property of the GPUDevice interface returns the primary GPUQueue for the device.

MDN Reference

The primary

GPUQueue

for this device.

queue
.
GPUQueue.onSubmittedWorkDone(): Promise<undefined> (+1 overload)

Returns a Promise that resolves once this queue finishes processing all the work submitted up to this moment. Resolution of this Promise implies the completion of

GPUBuffer#mapAsync

calls made prior to that call, on

GPUBuffer

s last used exclusively on that queue.

onSubmittedWorkDone
();
  • The surface’s pixelRatio is then the pixelRatio you give in size, 1 by default. A size in points gives the same number of pixels on every screen. maxPixelRatio doesn’t apply.
  • Use setSize for a run at several resolutions. web.host takes size too, and its page reaches setSize through send(({ surface }) => …).
  • A size that isn’t whole pixels from 1 to maxTextureDimension2D throws.
  • On React Native, native.surface and the React Native host take size too. resize then changes nothing until setSize(undefined), and setSize applies at once.
  • On the web, a ResizeObserver watches the canvas’s device-pixel box, the screen pixels it covers, so the drawing buffer matches the screen pixel for pixel. Without that box, the size is the CSS size times the ratio, rounded.
  • A size change reaches the canvas at the next view(), the in which your render targets follow it through fit(). Waiting until then keeps the resize from clearing a frame already drawn.

A pass that uses surface.context directly skips pending size changes, so take views from view(frame).

Use the Surface type from @typegpu/kit for code shared by both platforms and its tests. Pass the frame to view so React Native can present after submit.

import type {
(alias) interface Frame
import Frame

Represents one frame being recorded. Each run creates a separate object, so modules can compare frames by identity.

Frame
,
(alias) interface Surface
import Surface

Provides a destination for frames, such as a canvas from web.surface or native.surface, or a texture from offscreenSurface in a test. Pass the frame to view for code that works with every destination.

Surface
} from '@typegpu/kit';
export function
function createScene(root: TgpuRoot, surface: Surface): {
frame(frame: Frame): void;
}
createScene
(
root: TgpuRoot
root
:
(alias) interface TgpuRoot
import TgpuRoot
TgpuRoot
,
surface: Surface
surface
:
(alias) interface Surface
import Surface

Provides a destination for frames, such as a canvas from web.surface or native.surface, or a texture from offscreenSurface in a test. Pass the frame to view for code that works with every destination.

Surface
) {
return {
function frame(frame: Frame): void
frame
(
frame: Frame
frame
:
(alias) interface Frame
import Frame

Represents one frame being recorded. Each run creates a separate object, so modules can compare frames by identity.

Frame
) {
const
const pass: TgpuRenderPass
pass
=
frame: Frame
frame
.
Frame.encoder: TgpuCommandEncoder

Records commands for submission after the recorder returns. submit() replaces this encoder. Read it at each call instead of keeping it, and let the frame submit it.

encoder
.
TgpuCommandEncoder.beginRenderPass(descriptor: TgpuRenderPassDescriptor): TgpuRenderPass

Begins recording a render pass. Attachment views accept TypeGPU textures, texture views and canvas contexts, next to raw

GPUTextureView

s.

beginRenderPass
({
TgpuRenderPassDescriptor.colorAttachments?: ColorAttachment | readonly (ColorAttachment | null)[] | undefined
colorAttachments
: {
ColorAttachment.view: GPUTextureView | (ColorTextureConstraint & RenderFlag) | TgpuTextureView<WgslTexture<WgslTextureProps>> | TgpuTextureRenderView | GPUCanvasContext

A

GPUTextureView

describing the texture subresource that will be output to for this color attachment.

view
:
surface: Surface
surface
.
Surface.view(frame: Frame): GPUTextureView

Returns a view of the current texture, created once per texture. React Native requires the frame to present after submission.

view
(
frame: Frame
frame
) },
});
// draw
const pass: TgpuRenderPass
pass
.
TgpuRenderPass.end(): void

Completes the recording of this render pass

end
();
},
};
}

With --disable-gpu-vsync --disable-frame-rate-limit, Chrome can schedule frames faster than the GPU finishes them. Use web.paced(queue) as schedule to bound queued work. It waits until at most inFlight frames remain, 2 by default, before scheduling another frame. Normal vsync needs no extra pacing.

import frames
frames
.
function animate(loop: Pick<frames.Frames, "run"> & Partial<Pick<frames.Frames, "seconds">>, options?: frames.AnimateOptions): frames.Animation
export animate

Runs the loop on each scheduled frame. Time advances by at most maxDt per frame, never steps back, and stops while the surface is paused. Destroying the animation keeps the loop alive. Another animation on the same loop continues from its recorded time. A frame error, or a run that returns false because the loop was destroyed, stops the animation.

@param ― loop the loop to run on each unpaused frame. Continues from its seconds if it ran before

@returns ― an animation you destroy to stop the frames

animate
(
const loop: frames.Frames
loop
, {
AnimateOptions.schedule?: frames.Schedule | undefined

Supplies animation frames. Use runtimes.vsync(worker) on a React Native worker runtime, or a schedule you step by hand in a test. Default requestAnimationFrame.

schedule
:
import web
web
.
function paced(queue: Pick<GPUQueue, "onSubmittedWorkDone">, options?: web.PacedOptions): frames.Schedule

Supplies animation frames that wait while inFlight frames remain unfinished on the GPU. Use as animate's schedule when frame callbacks can outpace GPU work. Throws unless inFlight is an integer above 0.

@param ― queue the device's queue, root.device.queue

@returns ― a schedule to give animate

paced
(
const root: TgpuRoot
root
.
TgpuRoot.device: GPUDevice

The GPU device associated with this root.

device
.
GPUDevice.queue: GPUQueue

The queue read-only property of the GPUDevice interface returns the primary GPUQueue for the device.

MDN Reference

The primary

GPUQueue

for this device.

queue
) });

web.pointer(element, options) follows the mouse, touch or pen on an element in the coordinates a camera’s rayAt takes:

import * as
import web
web
from '@typegpu/kit/web';
const
const ray: {
origin: number[];
direction: number[];
}
ray
= {
origin: number[]
origin
: [0, 0, 0],
direction: number[]
direction
: [0, 0, 0] };
const
const ground: number[]
ground
= [0, 0, 0];
const
const mouse: web.Pointer
mouse
=
import web
web
.
function pointer(element: HTMLElement, options?: web.PointerOptions): web.Pointer

Follows the mouse, touch or pen on an element in the coordinates rayAt takes, 0 to 1 across the element with +y down. Read the position each frame or take each move, press, release and zoom. A button pressed on the element keeps the pointer until it comes up, so a drag continues outside and its positions run past 0 and 1.

@param ― element what the pointer moves over, usually the canvas

@returns ― the pointer, which you destroy to remove the listeners

pointer
(
const canvas: HTMLCanvasElement
canvas
, {
PointerOptions.down?: web.PointerHandler | undefined

Runs as a button goes down on the element. Read the button from event.button. Default none.

@cpu

down
(
x: number
x
,
y: number
y
,
event: PointerEvent
event
) {
if (
event: PointerEvent
event
.
MouseEvent.button: number

The MouseEvent.button read-only property indicates which button was pressed or released on the mouse to trigger the event.

MDN Reference

button
!== 0) return;
const view: cameras.OrbitCamera
view
.
CpuView.rayAt(x: number, y: number, out: cameras.CpuRay): void

Writes the ray through a point of the surface into out, as ray does. x and y range from 0 to 1 across the surface with +y down, as pointer handlers receive them.

rayAt
(
x: number
x
,
y: number
y
,
const ray: {
origin: number[];
direction: number[];
}
ray
);
if (
import cameras
cameras
.
function hitHeight(ray: cameras.CpuRay<ArrayLike<number>>, height: number | ((x: number, z: number) => number), out?: cameras.Out3, options?: cameras.HitOptions): number | undefined

Finds where a ray, such as one from view.rayAt, meets the ground at height, such as the ground under a pointer. A height function, such as a terrain's, is searched in growing steps up to reach, then refined by halving the last step. Writes the point into out when given. Allocates nothing.

@param ― ray an origin and a unit direction

@param ― height the y of a level plane, or of the ground at each x and z

@param ― out where the point goes

@returns ― the distance along the ray, or undefined for a ray that misses

hitHeight
(
const ray: {
origin: number[];
direction: number[];
}
ray
, 0,
const ground: number[]
ground
) !==
var undefined
undefined
)
function select(point: number[]): void
select
(
const ground: number[]
ground
);
},
});
// Each frame, the ground under the pointer
if (
const mouse: web.Pointer
mouse
.
Pointer.over: boolean

Tells whether the pointer is over the element or holds a button down that began on it. A finger is over only while it touches.

over
) {
const view: cameras.OrbitCamera
view
.
CpuView.rayAt(x: number, y: number, out: cameras.CpuRay): void

Writes the ray through a point of the surface into out, as ray does. x and y range from 0 to 1 across the surface with +y down, as pointer handlers receive them.

rayAt
(
const mouse: web.Pointer
mouse
.
Pointer.x: number

Holds the last position from 0 to 1 across the element, +y down. Starts at 0.5.

x
,
const mouse: web.Pointer
mouse
.
Pointer.y: number

Holds the last position from 0 to 1 down the element, as x does.

y
,
const ray: {
origin: number[];
direction: number[];
}
ray
);
import cameras
cameras
.
function hitHeight(ray: cameras.CpuRay<ArrayLike<number>>, height: number | ((x: number, z: number) => number), out?: cameras.Out3, options?: cameras.HitOptions): number | undefined

Finds where a ray, such as one from view.rayAt, meets the ground at height, such as the ground under a pointer. A height function, such as a terrain's, is searched in growing steps up to reach, then refined by halving the last step. Writes the point into out when given. Allocates nothing.

@param ― ray an origin and a unit direction

@param ― height the y of a level plane, or of the ground at each x and z

@param ― out where the point goes

@returns ― the distance along the ray, or undefined for a ray that misses

hitHeight
(
const ray: {
origin: number[];
direction: number[];
}
ray
, 0,
const ground: number[]
ground
);
}
  • Position Read x and y each frame, or take them in move, down and up.
  • Zoom zoom(by) runs as the wheel turns and as two fingers pinch. For the wheel, by is edeltaY·0.001. For a pinch, it is the old distance between the fingers divided by the new one. Multiply your distance by by. A value above 1 means farther away.
  • Drags A press keeps the pointer until its button comes up, so a drag continues outside the element, with positions past 0 and 1. up also runs when the browser cancels a pointer.
  • Touch and menu It sets the element’s touch-action to none. With page: true, it sets pan-y so vertical touch drags can scroll the page. In that mode, the wheel zooms only while you hold Ctrl or ⌘. menu: false keeps the context menu shut for drags with the secondary button.
  • Hosted pages Create the pointer in attach on a hosted page to dispose it with the scene.
  • React Native On React Native, divide a gesture’s position by the canvas’s layout size for the same coordinates.
import
import * as native from '@typegpu/kit/native'
call
native.surface(root, canvas, { pixelRatio, … }) → Surfacealso checkDevice
facts
allocateseach framelifecycledestroy()requiresimplicit-device-synchronizationplatformnativeonly

@typegpu/kit/native has no React Native imports. Pass it the canvas, pixel ratio and layout.

  • The surface native.surface takes the CanvasRef that <Canvas ref> holds, the web’s canvas options and the pixelRatio. The drawing buffer’s size is on its context’s canvas.
  • Presenting view(frame) schedules context.present() after submit. React Native requires an explicit present. Dawn rejects presenting without first acquiring the canvas texture.
  • Pausing A backgrounded app must not submit GPU work. pause() sets paused, which frames.animate checks before every frame when you pass it the surface. view() throws while the surface is paused, so a loop of your own fails at once instead of submitting in the background.
  • Resuming After a long background pause, the first getCurrentTexture after resume() can block the JS thread.
  • Device errors device.lost and uncapturederror reach only the main JS .
  • Features tgpu.init({ device: { optionalFeatures } }) keeps each feature the adapter has; device.features says which it got.
  • Above the cap, the device-pixel size is scaled by maxPixelRatio / ratio, so the drawing buffer matches the screen pixel for pixel only at or below the cap.
  • On React Native, a frame that takes the view and never finishes loses its present callback. The next frame that takes the view and reaches its after-submit callbacks presents the canvas.
  • The web surface’s view() doesn’t throw while paused, since a submit from a paused page does no harm.
  • implicit-device-synchronization costs one uncontended mutex per API call.
targetA target is a texture that a frame draws into.Scenes and passes →
frameA frame is one turn of the loop, and the object your function gets for it, which holds the encoder, the time and the time since the last frame.You own the frame →
runtimeA runtime is one JavaScript thread with its own memory, such as the page, a worker or a React Native worklet thread.Words →
hostA host runs a scene for you, which means it makes the device, the surface and the frame loop and frees them in order.Hosting scenes →

Reference

kit.web

platformHow to read this card

Hosts scenes on web canvases and provides surfaces, GPU pacing, pointer input and orbit controls.

Callsborrows, never destroystakesreturns

web.surface(root, canvas, options?) → Surface

root
the root
canvas
the canvas it configures and sizes
Surface
a surface you draw into and destroy
takes

SurfaceOptions

7 fields
size?

FixedSize

Fixes the drawing-buffer size before surface returns.Default the layout size multiplied by the capped pixel ratio, following layout changes.

maxPixelRatio?

number

Caps drawing-buffer pixels per CSS pixel unless size is set.Default 2.

format?

GPUTextureFormat

Sets the canvas texture format.Default navigator.gpu.getPreferredCanvasFormat().

alphaMode?

GPUCanvasAlphaMode

Controls canvas transparency.Default opaque.

usage?

GPUTextureUsageFlags

Sets the canvas texture usage flags.Default GPUTextureUsage.RENDER_ATTACHMENT.

colorSpace?

PredefinedColorSpace

Sets the colour space of canvas values.Default 'srgb'.

toneMapping?

GPUCanvasToneMapping

Controls the range of canvas values for display.Default { mode: 'standard' }.

returns

Surface

11 members
context

GPUCanvasContext

Provides the canvas context for a TypeGPU colour attachment view.

width

number

Reports the drawing-buffer size in pixels, from the fixed size or the layout size in device pixels.

height

number

Reports the drawing-buffer height in pixels, as width does.

pixelRatio

number

Reports the fixed size's pixel ratio, or follows zoom and screen changes when the size follows layout.

paused

boolean

Reports whether the surface is paused, true between pause and resume.

setSize(size)

Fixes the drawing-buffer size from the next view(), as options.size does.

pause()

Pauses the surface for a canvas outside the viewport or a pause menu.

resume()

Resumes the frames after pause.

view(frame)

Returns a view of the current canvas texture, created once per texture.

format

GPUTextureFormat

Reports the format of its textures, which pipelines that draw into it target.

destroy()

Releases the drawing destination.

web.paced(queue, options?) → Schedule

queue
the device's queue, root.device.queue
Schedule
a schedule to give animate
takes

PacedOptions

1 field
inFlight?

number

Limits the number of frames whose GPU work may be unfinished when the next frame starts.Default 2.

returns

Schedule

2 members
request(callback)

Calls callback with the time in milliseconds at the next frame and returns the request's id.

cancel(request)

Cancels the request with this id.

web.orbitControls(orbit, element, options?) → OrbitControls

orbit
an orbit camera, whose goal it writes
element
what takes the drag, the wheel and the pinch
OrbitControls
the controls, which you destroy to remove the listeners
takes

OrbitControlsOptions

7 fields
sensitivity?

number

Sets rotation per dragged pixel, in radians.Default 0.008, as cameras.turn.

zoomSpeed?

number

Sets wheel zoom speed.Default 0.001.

button?

'primary' | 'secondary' | 'middle'

Orbits with this mouse button.Default 'primary'.

pan?

boolean

Enables panning with the secondary button, or the middle one when the secondary orbits, and with two fingers moving together.Default false.

page?

boolean

Allows page scrolling around an embedded view.Default false, so the element handles every drag and wheel input.

pointer?

Pointer

Leaves the element's touch-action and pointer capture to this pointer on the same element, which sets them from its own page.Default none, so the controls set both.

onChange?

() => void

Runs after each input that changes the goal, for views that draw on demand.Default none.

returns

OrbitControls

1 member
destroy()

Removes the listeners and restores the element's touch-action, unless a pointer set it.

web.pointer(element, options?) → Pointer

element
what the pointer moves over, usually the canvas
Pointer
the pointer, which you destroy to remove the listeners
takes

PointerOptions

7 fields
move?

PointerHandler

Runs as the pointer moves over the element, or anywhere while it holds a button down.Default none.

down?

PointerHandler

Runs as a button goes down on the element.Default none.

up?

PointerHandler

Runs as a button comes up, or as the browser cancels the pointer.Default none.

leave?

() => void

Runs as the pointer leaves the element with no button down.Default none.

zoom?

(by: number) => void

Runs as the wheel turns or two fingers pinch on the element, with the factor to scale a distance by, above 1 farther: e^(deltaY × 0.001) for the wheel, and the ratio of the fingers' last spread to the new one for a pinch.Default none.

page?

boolean

Allows page scrolling around an embedded element: touch drags scroll the page instead of reaching move, and only the wheel with Ctrl or Command held zooms.Default false, so the element takes every touch drag and wheel turn.

menu?

boolean

Shows the context menu on a secondary click.Default true.

returns

Pointer

5 members
x

number

Holds the last position from 0 to 1 across the element, +y down.

y

number

Holds the last position from 0 to 1 down the element, as x does.

over

boolean

Tells whether the pointer is over the element or holds a button down that began on it.

buttons

number

Holds the buttons held down, as PointerEvent.buttons: 1 primary, 2 secondary, 4 middle.

destroy()

Removes the listeners and restores the element's touch-action.

and web.host, web.video

takes

HostOptions

host · 15 fields
canvas

HTMLCanvasElement

Supplies the canvas for the scene.

optionalFeatures?

Iterable<GPUFeatureName>

Requests these features when the adapter supports them, such as timestamp-query.Default none.

device?

GPUDevice

Supplies a device that the host keeps alive.Default a device that the web host requests and destroys with the scene.

root?

TgpuRoot

Supplies a root that the host keeps alive, so scenes can share it.Default a root that the host creates on device and destroys with the scene.

schedule?

Schedule | false

Supplies animation frames.Default requestAnimationFrame, or display vsync on a React Native worker.

onFrame?

(stage: Stage<S, Canvas>, frame: Frame) => void

Runs after the scene's frame each frame, for page work such as a HUD that reads scene state.Default none.

attach?

(stage: Stage<S, Canvas>) => Detach | void

Connects page code, such as orbit controls, to the stage after scene creation.Default none.

onError?

Callback<[unknown]>

Receives the first scene error, from createScene, a frame, onFrame, ready or the device, which stops the frames.Default console.error.

format?

GPUTextureFormat

Sets the canvas texture format.Default navigator.gpu.getPreferredCanvasFormat().

alphaMode?

GPUCanvasAlphaMode

Controls canvas transparency.Default opaque.

usage?

GPUTextureUsageFlags

Sets the canvas texture usage flags.Default GPUTextureUsage.RENDER_ATTACHMENT.

colorSpace?

PredefinedColorSpace

Sets the colour space of canvas values.Default 'srgb'.

toneMapping?

GPUCanvasToneMapping

Controls the range of canvas values for display.Default { mode: 'standard' }.

size?

FixedSize

Fixes the drawing-buffer size before surface returns.Default the layout size multiplied by the capped pixel ratio, following layout changes.

maxPixelRatio?

number

Caps drawing-buffer pixels per CSS pixel unless size is set.Default 2.

takes

VideoOptions

video · 1 field
mirrored?

boolean

Flips every frame left to right, as a mirror shows a front camera.Default false.

returns

Hosted

host · 5 members
pause()

Stops frames and elapsed time for an app in the background, a canvas outside the viewport or a pause menu.

resume()

Resumes the frames after pause.

send(fn, ...args)

Runs fn with the value on its runtime, after earlier sent calls.

run(fn, ...args)

Runs fn from React Native or the page, as send does, and copies its result back as post does.

destroy()

Calls destroy on the value's runtime after earlier sent calls, then releases the value without waiting for their promises.

returns

Video

video · 2 members
destroy()

Stops watching the element for new frames.

latest(device, seen?)

Imports the newest frame on device.

Holds

owns

the canvas's configuration and a resize observer; a hosted scene also owns the surface ·its root and the device unless it was given them ·the scene's stage and what its attach returned

allocates

each frame

A view of the canvas's texture, which is new every frame; under paced, a promise of the frame's work; for video, an imported texture per read of a frame.

lifecycle

destroy()[Symbol.dispose]

Connects to

platform

webonly

Exports

types
CreateScene Scene FixedSize OrbitGoal PointerHandler Stage

used byevery example page

kit.native

platformHow to read this card

Provides a react-native-webgpu canvas surface that pauses in the background, and a check that two threads can share a device.

Callsborrows, never destroystakesreturns

native.surface(root, canvas, { pixelRatio, … }) → Surface

root
the root
canvas
the CanvasRef from a <Canvas ref>; its context's canvas holds the size
pixelRatio
PixelRatio.get()
Surface
a surface you resize, pause, resume and destroy
takes

SurfaceOptions

8 fields
pixelRatio

number

Sets the display pixel ratio from PixelRatio.get().

maxPixelRatio?

number

Caps drawing-buffer pixels per point unless size is set.Default 2.

size?

FixedSize

Fixes the drawing-buffer size.Default the layout size in points multiplied by the capped pixel ratio.

format?

GPUTextureFormat

Sets the canvas texture format.Default navigator.gpu.getPreferredCanvasFormat().

alphaMode?

GPUCanvasAlphaMode

Controls canvas transparency.Default opaque.

usage?

GPUTextureUsageFlags

Sets the canvas texture usage flags.Default GPUTextureUsage.RENDER_ATTACHMENT.

colorSpace?

PredefinedColorSpace

Sets the colour space of canvas values.Default 'srgb'.

toneMapping?

GPUCanvasToneMapping

Controls the range of canvas values for display.Default { mode: 'standard' }.

returns

Surface

12 members
context

GPUCanvasContext

Provides the canvas context, configured for the root.

paused

boolean

Reports whether the surface is paused.

resize(width, height)

Sizes the drawing buffer from the canvas's layout size in points, as onLayout reports it.

setSize(size)

Fixes the drawing-buffer size, as options.size does.

pause()

Pauses frames while the app is in the background, where it must not submit GPU work.

resume()

Resumes the surface and reconfigures its context with the same options.

format

GPUTextureFormat

Reports the format of its textures, which pipelines that draw into it target.

width

number

Reports the drawing-buffer size in pixels.

height

number

Reports the drawing-buffer height in pixels.

pixelRatio

number

Reports drawing-buffer pixels per CSS pixel or point after applying the cap.

view(frame)

Returns a view of the current texture, created once per texture.

destroy()

Releases the drawing destination.

native.checkDevice(device)

device
the device to check, such as a <GPUDeviceProvider>'s

Holds

owns

the canvas's configuration

allocates

each frame

A view of the canvas's texture, which is new every frame.

lifecycle

destroy()[Symbol.dispose]

Connects to

requires
  • implicit-device-synchronizationrequired

    React-native-webgpu requests it for every device; checkDevice throws without it. Devices used from two threads require it to avoid crashes.

platform

nativeonly

Exports

types
FixedSize Canvas