From aa79c6155c80ac1c58ab8fd0e568bbd52d2fdf46 Mon Sep 17 00:00:00 2001 From: Luna <279187109+lunadogbot@users.noreply.github.com> Date: Wed, 29 Apr 2026 11:47:34 +0000 Subject: [PATCH 1/2] docs: document OffscreenCanvas (2.8) Adds an OffscreenCanvas section to the Web platform APIs reference, describing the new API available in Deno 2.8. Covers a typical 2D-drawing + convertToBlob() example, common use cases (server-side image generation, off-thread drawing, thumbnails), and notes that WebGL/WebGPU contexts on OffscreenCanvas are not yet supported. Refs denoland/deno#29357 --- runtime/reference/web_platform_apis.md | 28 ++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/runtime/reference/web_platform_apis.md b/runtime/reference/web_platform_apis.md index 5f5c7e15d..b0840d360 100644 --- a/runtime/reference/web_platform_apis.md +++ b/runtime/reference/web_platform_apis.md @@ -489,6 +489,34 @@ const worker = new Worker(import.meta.resolve("./worker.js"), { }); ``` +## OffscreenCanvas + +Starting in Deno 2.8, the +[`OffscreenCanvas`](https://developer.mozilla.org/en-US/docs/Web/API/OffscreenCanvas) +API is available. `OffscreenCanvas` is a canvas that lives outside any DOM and +can be used in any context — including Web Workers — for off-thread rendering +and image generation. + +```ts +const canvas = new OffscreenCanvas(256, 256); +const ctx = canvas.getContext("2d"); +ctx.fillStyle = "tomato"; +ctx.fillRect(0, 0, 256, 256); + +// Encode to PNG / JPEG / WebP via convertToBlob() +const blob = await canvas.convertToBlob({ type: "image/png" }); +await Deno.writeFile("./tile.png", new Uint8Array(await blob.arrayBuffer())); +``` + +Typical uses: + +- generating images server-side without spinning up a headless browser, +- running 2D drawing code originally written for the browser inside a Web + Worker, +- producing thumbnails or social-card images at request time. + +WebGL / WebGPU rendering contexts on `OffscreenCanvas` are not yet supported. + ## Deviations of other APIs from spec ### Cache API From 4fcc9716f4dea9fe639f92774d4819aab14931fd Mon Sep 17 00:00:00 2001 From: Luna <279187109+lunadogbot@users.noreply.github.com> Date: Fri, 1 May 2026 12:19:22 +0000 Subject: [PATCH 2/2] docs(review): correct OffscreenCanvas context list and example MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Per @Hajime-san — Deno's OffscreenCanvas supports only \`bitmaprenderer\` and \`webgpu\`; \`2d\` / \`webgl\` / \`webgl2\` return null. Replaces the broken 2D drawing example with a bitmaprenderer image-encode flow and rewrites the "limitations" line into an explicit "Supported rendering contexts" subsection. WebGPU is supported, so it no longer appears in the unsupported list. Refs denoland/deno#29357 --- runtime/reference/web_platform_apis.md | 49 ++++++++++++++++++-------- 1 file changed, 35 insertions(+), 14 deletions(-) diff --git a/runtime/reference/web_platform_apis.md b/runtime/reference/web_platform_apis.md index b0840d360..202b41db4 100644 --- a/runtime/reference/web_platform_apis.md +++ b/runtime/reference/web_platform_apis.md @@ -494,28 +494,49 @@ const worker = new Worker(import.meta.resolve("./worker.js"), { Starting in Deno 2.8, the [`OffscreenCanvas`](https://developer.mozilla.org/en-US/docs/Web/API/OffscreenCanvas) API is available. `OffscreenCanvas` is a canvas that lives outside any DOM and -can be used in any context — including Web Workers — for off-thread rendering -and image generation. +can be used anywhere — including Web Workers — for off-thread rendering and +image generation. + +### Supported rendering contexts + +`OffscreenCanvas#getContext` accepts two of the spec-defined context ids: + +- `"bitmaprenderer"` — returns an + [`ImageBitmapRenderingContext`](https://developer.mozilla.org/en-US/docs/Web/API/ImageBitmapRenderingContext) + for displaying an `ImageBitmap` produced via `createImageBitmap`. +- `"webgpu"` — returns a + [`GPUCanvasContext`](https://developer.mozilla.org/en-US/docs/Web/API/GPUCanvasContext) + for rendering with WebGPU. + +Calling `getContext` with `"2d"`, `"webgl"`, or `"webgl2"` returns `null` — +these contexts are not implemented in Deno. + +### Example: encoding an image to PNG + +Decode an image into an `ImageBitmap`, place it on an `OffscreenCanvas` via +the `bitmaprenderer` context, and write the result to disk: ```ts -const canvas = new OffscreenCanvas(256, 256); -const ctx = canvas.getContext("2d"); -ctx.fillStyle = "tomato"; -ctx.fillRect(0, 0, 256, 256); +const data = await Deno.readFile("./input.jpg"); +const bitmap = await createImageBitmap(new Blob([data])); + +const canvas = new OffscreenCanvas(bitmap.width, bitmap.height); +const ctx = canvas.getContext("bitmaprenderer")!; +ctx.transferFromImageBitmap(bitmap); -// Encode to PNG / JPEG / WebP via convertToBlob() const blob = await canvas.convertToBlob({ type: "image/png" }); -await Deno.writeFile("./tile.png", new Uint8Array(await blob.arrayBuffer())); +await Deno.writeFile( + "./output.png", + new Uint8Array(await blob.arrayBuffer()), +); ``` Typical uses: -- generating images server-side without spinning up a headless browser, -- running 2D drawing code originally written for the browser inside a Web - Worker, -- producing thumbnails or social-card images at request time. - -WebGL / WebGPU rendering contexts on `OffscreenCanvas` are not yet supported. +- producing thumbnails, format conversions, or social-card images at request + time without spinning up a headless browser, +- running off-thread image work inside a Web Worker, +- driving WebGPU rendering targets that don't need a window. ## Deviations of other APIs from spec