Capture OffscreenCanvas in Workers
masterSpectorJS supports OffscreenCanvas both on the main thread and inside Web Workers. Use the appropriate bundle for your environment:
| Bundle | Use |
|---|---|
dist/spector.bundle.js | Main thread (includes UI) |
dist/spector.worker.bundle.js | Inside Workers (headless, no UI) |
Manual API (Recommended for Workers)
To capture from a Worker, you must bridge the Worker on the main thread and load the worker bundle inside the worker script.
Main thread implementation:
var spector = new SPECTOR.Spector();
var worker = new Worker('myWorker.js');
// Bridge the Worker for capture
spector.spyWorker(worker);
// Capture from the Worker
spector.onCapture.add(function(capture) {
console.log('Captured from Worker:', capture.commands.length, 'commands');
spector.getResultUI().display();
spector.getResultUI().addCapture(capture);
});
spector.captureWorker(worker);Inside myWorker.js:
importScripts('spector.worker.bundle.js');
var canvas = new OffscreenCanvas(800, 600);
var gl = canvas.getContext('webgl2');
function render() {
// ... draw calls ...
setTimeout(render, 16);
}
render();Auto-injection (Best-effort)
Use spyWorkers() to monkey-patch the global Worker constructor. This automatically injects the Spector bundle into every new Worker created.
var spector = new SPECTOR.Spector();
spector.spyWorkers('spector.worker.bundle.js');
// To stop intercepting:
// spector.stopSpyingWorkers();Note: Auto-injection may fail with cross-origin Workers, strict CSP policies, or ES module Workers. Use the manual API for reliability.