MCPcopy Index your code
hub / github.com/ai/offscreen-canvas

github.com/ai/offscreen-canvas @0.1.1

Chat with this repo
repository ↗ · DeepWiki ↗ · release 0.1.1 ↗ · + Follow
2 symbols 4 edges 4 files 0 documented · 0%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

Offscreen Canvas Polyfill

JS polyfill (375 bytes) for OffscreenCanvas to move Three.js, WebGL or 2D canvas to Web Worker.

It will improve performance in Chrome and will load worker by <script> in Firefox, Safari, and other browsers.

The tutorial for this library: Faster WebGL/Three.js 3D graphics with OffscreenCanvas and Web Workers.

// index.js
import createWorker from 'offscreen-canvas/create-worker'

const worker = createWorker(canvas, '/worker.js', e => {
  // Messages from the worker
})

button.addEventListener('click', () => {
  worker.post({ message: 'update' })
})
// worker.js
import insideWorker from 'offscreen-canvas/inside-worker'

const worker = insideWorker(e => {
  if (e.data.canvas) {
    // Draw on the canvas
  } else if (e.data.message === 'move') {
    // Messages from main thread
  }
})

Sponsored by Evil Martians

Usage

Create separated bundle in webpack, Parcel or any other bundler:

```diff js entry: { app: './src/app.js', + worker: './src/worker.js' }


Move all code working with `<canvas>` to `worker.js`. It means to move all WebGL
or Three.js imports and scene related code.

```js
import insideWorker from 'offscreen-canvas/inside-worker'
// Move Three.js imports here if you use Three.js

const worker = insideWorker(e => {
  if (e.data.canvas) {
    // Move scene building code here
  }
})

Some of Three.js code (mostly loaders) will not work in Web Worker. Use worker.isWorker to switch loaders:

    if (worker.isWorker) {
      loader = new ImageBitmapLoader()
    } else {
      loader = new ImageLoader()
    }

Put preload link to HTML templates with a URL to worker.js. Your bundle will add cache buster to bundle names, so bundle names will change every time you deploy application. This is why we need to store path to worker.js in HTML:

```diff html +


Load worker in main `app.js`:

```js
import createWorker from 'offscreen-canvas/create-worker'

const workerUrl = document.querySelector('[rel=preload][as=script]').href
const canvas = document.querySelector('canvas')

const worker = createWorker(canvas, workerUrl)

Keep all UI interaction code (listeners for clicks, mouse move, etc) in app.js. Send message to Worker when your need to update <canvas> after user actions:

button.addEventListener('click', () => {
  worker.post({ message: 'move' })
})

Process this messages in the worker:

diff js const worker = insideWorker(e => { if (e.data.canvas) { // Move scene building code here - } + } else if (e.data.message === 'move') { + // Move object on the scene + } })

Core symbols most depended-on inside this repo

fill
called by 3
test/worker.js
formatTime
called by 2
test/index.js

Shape

Function 2

Languages

TypeScript100%

Modules by API surface

test/worker.js1 symbols
test/index.js1 symbols

For agents

$ claude mcp add offscreen-canvas \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact