Texture loading

This example fetches a bundled PNG with a base-aware URL, decodes it through createImageBitmap, and uploads its pixels to WebGPU through the game-owned asset cache. The sprite rotates around its center as part of the game loop.

status: loading

dimensions: 0 × 0

background sample: waiting

GPU sample: waiting

rotation: 0°

What this demonstrates

  • A static PNG resolved with SvelteKit's base-aware asset helper.
  • A checked fetch response converted to a Blob for assets.load_texture.
  • Nearest-neighbor filtering for pixel art, a rotating sprite, and a real WebGPU readback of the uploaded texture.
  • Game-owned resource cleanup when the host is disposed.

Load a bundled texture

const response = await fetch(asset("textures/player.png"), { signal });
if (!response.ok) throw new Error(`texture request failed: ${response.status}`);
const texture = await assets.load_texture(await response.blob(), {
  filter: "nearest",
  signal,
});

renderer.draw_texture(texture.read(), {
  x: player.x,
  y: player.y,
  width: 64,
  height: 64,
  origin: { x: 0.5, y: 0.5 },
  rotation: elapsed_seconds,
});

Public type shape

assets.load_texture accepts a Blob or File and returns a ResourceHandle<WebGpuTexture>. The handle exposes the decoded width, height, filtering policy, and explicit release and share operations. The game resource owner also releases handles during game disposal.