Skip to content

RenderSurface

Manages the WebGPU adapter, GPU device, canvas context, surface format, depth texture, and per-frame command encoder.

import { RenderSurface } from 'webgpu-spritebatch'
const surface = await RenderSurface.create(canvas)

create() is an async factory that requests a WebGPU adapter and device, configures the canvas context, and creates a depth texture.

MemberTypeDescription
canvasHTMLCanvasElementThe underlying canvas element
gpuDeviceGPUDeviceThe raw GPU device for advanced usage
formatGPUTextureFormatThe preferred canvas texture format
depthFormatGPUTextureFormatThe depth texture format (depth24plus)
width / heightnumberCanvas size in CSS pixels
physicalWidth / physicalHeightnumberCanvas size in device pixels
dprnumberCurrent device pixel ratio
commandEncoderGPUCommandEncoderCurrent frame’s command encoder
colorView / depthViewGPUTextureViewCurrent frame’s render target views

Async factory. Requests a WebGPU adapter and device, configures the canvas context.

const surface = await RenderSurface.create(canvas)

Resizes the canvas if needed, clears color and depth, and starts a command encoder.

surface.beginFrame({
clearColor: { r: 0.39, g: 0.58, b: 0.93, a: 1 }
})
OptionTypeDefaultDescription
clearColorColorRGBABlackBackground clear color

Submits the command buffer to the GPU.

surface.endFrame()