Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 18.2 — Context Loss and Recovery
3D Web · Example 3 of 3

The 16-context limit

Chromium's limit; other browsers differ. The devtools console also shows the browser's warning.

The GPU can take a canvas's WebGL context away while the page is open. three.js rebuilds its own objects when it comes back, but anything you drew once, or made with your own WebGL code, is empty until you rebuild it.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 4 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Write the two handlers for a page whose render loop is loop: stop it when the context is lost, start it when restored.

02

A scene paints a static reflection into target once at load. Add the code that keeps it correct after a restore.

Explain it without notes

01

Which things does three.js bring back after webglcontextrestored, and which do you have to rebuild? Give two examples of each.

02

Why is calling event.preventDefault() in the lost handler important?

The 16-context limit
Press Run to see the page.
idle

Console

Nothing logged yet.

Expected console output

contexts made: 20 | lost: 4 | the oldest ones: 0, 1, 2, 3
the newest (19) is alive: true