|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | + <head> |
| 4 | + <meta name="viewport" content="width=device-width, initial-scale=1.0" /> |
| 5 | + |
| 6 | + <title>OffscreenCanvas API</title> |
| 7 | + |
| 8 | + <link rel="stylesheet" href="style.css" /> |
| 9 | + </head> |
| 10 | + <body> |
| 11 | + <header> |
| 12 | + <h1>OffscreenCanvas and worker threads</h1> |
| 13 | + <p> |
| 14 | + <b>Note:</b> your browser must support |
| 15 | + <a |
| 16 | + href="https://developer.mozilla.org/en-US/docs/Web/API/OffscreenCanvas" |
| 17 | + ><code>OffscreenCanvas</code></a |
| 18 | + >. |
| 19 | + </p> |
| 20 | + <p>This example has two canvases with incrementing counters.</p> |
| 21 | + |
| 22 | + <p> |
| 23 | + Canvas A is being drawn to on the main thread. Canvas B uses an |
| 24 | + <a |
| 25 | + href="https://developer.mozilla.org/en-US/docs/Web/API/OffscreenCanvas" |
| 26 | + ><code>OffscreenCanvas</code></a |
| 27 | + > |
| 28 | + so that we can draw to it in a worker thread. The purpose of this |
| 29 | + example is to show how a worker thread can keep the rest of our UI |
| 30 | + responsive. |
| 31 | + </p> |
| 32 | + |
| 33 | + <p> |
| 34 | + The button below each canvas creates <b>blocking work</b> on either the |
| 35 | + main thread (Canvas A) or a worker thread (Canvas B). When a thread is |
| 36 | + blocked, incrementing the related counter from that thread is also |
| 37 | + blocked. |
| 38 | + </p> |
| 39 | + </header> |
| 40 | + |
| 41 | + <main> |
| 42 | + <div class="canvases"> |
| 43 | + <div> |
| 44 | + <span class="canvas-title">Canvas A</span> |
| 45 | + <canvas id="main" width="200" height="200"></canvas> |
| 46 | + <div> |
| 47 | + <button id="main-button" onclick="slowMainThread()"> |
| 48 | + Block main thread |
| 49 | + </button> |
| 50 | + </div> |
| 51 | + </div> |
| 52 | + <div> |
| 53 | + <span class="canvas-title">Canvas B</span> |
| 54 | + <canvas id="worker" width="200" height="200"></canvas> |
| 55 | + |
| 56 | + <div> |
| 57 | + <button id="worker-button" onclick="slowdownWorker()"> |
| 58 | + Block worker thread |
| 59 | + </button> |
| 60 | + </div> |
| 61 | + </div> |
| 62 | + </div> |
| 63 | + <div id="canvas-description"> |
| 64 | + <p> |
| 65 | + <b>When the main thread is blocked</b>, all UI elements are frozen.<br /> |
| 66 | + The hover effect on buttons is also blocked: |
| 67 | + </p> |
| 68 | + <button>Example button</button> |
| 69 | + <p> |
| 70 | + <b>When the worker thread is blocked</b>, the main thread is free to |
| 71 | + do work such as the element hover effects and animations. Blocking the |
| 72 | + worker thread will still prevent Canvas B's counter from being |
| 73 | + updated, but the rest of the UI stays responsive while this is true. |
| 74 | + </p> |
| 75 | + </div> |
| 76 | + |
| 77 | + <footer> |
| 78 | + <p> |
| 79 | + This demo is based on an example by |
| 80 | + <a href="https://glitch.com/@PicchiKevin">Kevin Picchi</a>. |
| 81 | + </p> |
| 82 | + </footer> |
| 83 | + </main> |
| 84 | + |
| 85 | + <script> |
| 86 | + const canvasA = document.getElementById("main"); |
| 87 | + const canvasB = document.getElementById("worker"); |
| 88 | + |
| 89 | + const ctxA = canvasA.getContext("2d"); |
| 90 | + const canvasWidth = ctxA.width; |
| 91 | + const canvasHeight = ctxA.height; |
| 92 | + |
| 93 | + // Create a counter for Canvas A |
| 94 | + let counter = 0; |
| 95 | + setInterval(() => { |
| 96 | + redrawCanvasA(); |
| 97 | + counter++; |
| 98 | + }, 100); |
| 99 | + |
| 100 | + // Redraw Canvas A counter |
| 101 | + function redrawCanvasA() { |
| 102 | + ctxA.clearRect(0, 0, canvasA.width, canvasA.height); |
| 103 | + ctxA.font = "24px Verdana"; |
| 104 | + ctxA.textAlign = "center"; |
| 105 | + ctxA.fillText(counter, canvasA.width / 2, canvasA.height / 2); |
| 106 | + } |
| 107 | + |
| 108 | + // This function creates heavy (blocking) work on a thread |
| 109 | + function fibonacci(num) { |
| 110 | + if (num <= 1) { |
| 111 | + return 1; |
| 112 | + } |
| 113 | + return fibonacci(num - 1) + fibonacci(num - 2); |
| 114 | + } |
| 115 | + |
| 116 | + // Call our Fibonacci function on the main thread |
| 117 | + function slowMainThread() { |
| 118 | + fibonacci(42); |
| 119 | + } |
| 120 | + |
| 121 | + // Set up a worker thread to render Canvas B |
| 122 | + const worker = new Worker("worker.js"); |
| 123 | + |
| 124 | + // Use the OffscreenCanvas API and send to the worker thread |
| 125 | + const canvasWorker = canvasB.transferControlToOffscreen(); |
| 126 | + worker.postMessage({ canvas: canvasWorker }, [canvasWorker]); |
| 127 | + |
| 128 | + // A 'slowDown' message we can catch in the worker to start heavy work |
| 129 | + function slowdownWorker() { |
| 130 | + worker.postMessage("slowDown"); |
| 131 | + } |
| 132 | + </script> |
| 133 | + </body> |
| 134 | +</html> |
0 commit comments