> ## Documentation Index
> Fetch the complete documentation index at: https://glassen.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Supply images, canvases and GPU textures

> Implement a Glassen source for static or animated pixels, with versioned uploads, context-local GPU resources and explicit ownership on teardown.

A `GlassSource` supplies pixels to one or more lenses. Choose a canvas/image source for browser content or a GPU renderer when your application already renders textures.

## Images and canvases

```js theme={null}
const image = new Image()
image.src = '/artwork.png'
await image.decode()
const source = { sample: () => ({ image, version: 1, sampling: 'linear' }) }
```

Set `crossOrigin = 'anonymous'` before `src` for a cross-origin image, and configure the image server to allow it. Image uploads use premultiplied RGBA; the adapter handles top-left browser coordinates. `sampling: 'nearest'` preserves pixel edges.

Use a stable version until the pixels change. Increment it after drawing into the same canvas; replacing the image object also invalidates the upload. Every mounted surface can share a source identity without sharing a WebGL texture between contexts.

## Animated drawing

```js theme={null}
const source = {
  draw(pen, frame) {
    const p = frame.pixels
    pen.fillStyle = '#e84040'
    pen.beginPath()
    pen.arc(p * (0.5 + Math.sin(frame.time) * 0.15), p / 2, p * 0.12, 0, Math.PI * 2)
    pen.fill()
  },
}
```

The host clears a canvas matching `frame.pixelWidth` × `frame.pixelHeight` and calls `draw` on its display clock. This path uploads pixels each frame. Use `sample` for static sources or `createRenderer(gl)` to keep animation entirely on the GPU. Do not start a second animation clock inside a source.

## GPU source

`createRenderer(gl)` returns `{ bytes, render(frame), dispose() }`. `render` returns `{ target, version }`, where `target` contains `texture`, `framebuffer`, `width`, `height`, `bytes` and `dispose()`.

The texture must belong to `gl`, use bottom-left texture coordinates, and contain premultiplied, display-encoded RGBA. Establish your renderer's own draw state. Linear Three.js render targets require the appropriate color conversion before Glassen samples them.

Create independent resources for every `createRenderer` call. The surface disposes those renderers when the final consumer leaves or the context is lost. It never destroys the caller's source object. Make disposal idempotent and clean up partial allocations if construction fails.

## Updates and activity

`subscribe(notify)` returns an unsubscribe function. Notify after source changes to wake suspended surfaces. `setActive(active, owner)` identifies each consumer so a shared producer can pause only when no consumers remain active. `look(point)` optionally receives pointer coordinates.

[DOM capture](https://glassen.mintlify.site/api.md#dom-capture) is an optional adapter.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.