forked from ebidel/demos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
offscreencanvas.html
224 lines (191 loc) · 6.21 KB
/
offscreencanvas.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<!--
Copyright 2018
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Author: Eric Bidelman (@ebidel)
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Offscreen Canvas</title>
<style>
:root {
--default-padding: 16px;
--default-padding-half: 8px;
--material-blue-grey-100: #CFD8DC;
--material-blue-grey-300: #90A4AE;
--material-blue-grey-400: #78909C;
--material-blue-grey-600: #546E7A;
--material-blue-grey-800: #37474F;
--material-blue-grey-dark: #263238;
--material-red-500: #F44336;
--material-red-600: #E53935;
}
body {
font-family: 'Google Sans', 'Product Sans', sans-serif;
-webkit-font-smoothing: antialiased;
line-height: 1.8;
padding: 1em 2em;
color: var(--material-blue-grey-800);
}
* {
box-sizing: border-box;
}
.support {
display: none;
}
.notsupported {
display: block;
font-weight: bold;
text-align: center;
padding: var(--default-padding-half);
background-color: var(--material-red-500);
color: white;
}
</style>
</head>
<body>
<h2>Offscreen canvas demo</h2>
<p><b>What</b>: <a href="https://www.chromestatus.com/feature/5681560598609920" target="_blank">OffscreenCanvas</a>
allows <code><canvas></code> rendering contexts (2D and WebGL) to be used
in workers. This can increase parallelism and better performance on multi-core
systems.</p>
<p><b>Support</b>: Available in Chrome 69. FF is in development.</p>
<div class="support">Your browser does not support OffscreenCanvas.</div>
<p><b>Demo</b>: The canvas animation is fully driven inside of the worker. <code>Worker.requestAnimationFrame()</code>
is used to drive the animation withi the worker and pixels are manipulated / drawn to the canvas
using <code>OffscreenCanvas</code>. The worker introduces jank to the
animation every so often to simulate a CPU-intensie operation. However, the main thread
says responsive!</p>
<p>
<b>Instructions</b>: Select an image below. Click <button>this button</button> during jank. It stays responsive.</li>
</p>
<p><input type="file"></p>
<canvas></canvas>
<script type="script/worker" id="workerCode">
let angle = 0;
let ctx = null;
let backgroundBitmap = null;
function drawCircle() {
const radius = ctx.canvas.height * Math.abs(Math.cos(angle));
const w = ctx.canvas.width;
const h = ctx.canvas.height;
ctx.save();
ctx.globalAlpha = 0.5;
ctx.fillStyle = '#fff';
ctx.beginPath();
ctx.arc(w / 2, h / 2, radius, 0, Math.PI * 2, true);
ctx.fill();
ctx.restore();
angle += Math.PI / 64;
}
function drawBackground() {
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
ctx.drawImage(backgroundBitmap, 0, 0);
}
function fibonacci(num) {
if (num <= 1) {
return 1;
}
return fibonacci(num - 1) + fibonacci(num - 2);
}
function animate() {
// Periodic jank!
if (counter++ >= 150) {
fibonacci(40);
counter = 0;
}
drawBackground();
drawCircle();
//ctx.commit(); // Don't need this since our animation is powered by rAF, but we could shipt a frame explicitly.
self.requestAnimationFrame(animate);
}
let counter = 0;
self.onmessage = function(e) {
switch (e.data.msg) {
case 'start':
backgroundBitmap = e.data.bitmap;
const canvas = e.data.canvas;
ctx = canvas.getContext('2d');
animate();
break;
}
};
</script>
<script>
(async() => {
/**
* Note: You can also create an OSC in a worker and postMessage transferrable
* bitmaps back to the main page:
*
* // worker.js
* self.onmessage = function(e) {
* const canvas = new OffscreenCanvas(e.data.width, e.data.height);
* ctx = canvas.getContext('2d');
* // draw fancy things ...
* const bitmap = ctx.canvas.transferToImageBitmap();
* postMessage({bitmap}, [bitmap]);
* };
*
* // index.html
* const canvas = document.createElement('canvas');
* canvas.width = 400;
* canvas.height = 400;
*
* const worker = new Worker('worker.js');
* worker.onmessage = function(e) {
* const ctx = canvas.getContext('bitmaprenderer');
* ctx.transferFromImageBitmap(e.data.bitmap);
* };
*
* worker.postMessage({width: canvas.width, height: canvas.height});
*/
// Feature detect.
document.querySelector('.support').classList.toggle(
'notsupported', !('OffscreenCanvas' in window));
const canvas = document.querySelector('canvas');
const workerCode = document.querySelector('#workerCode').textContent;
const blob = new Blob([workerCode], {type: 'text/javascript'});
const url = URL.createObjectURL(blob);
const worker = new Worker(url);
worker.onmessage = e => {
switch (e.data.msg) {
case 'render':
const ctx = canvas.getContext('bitmaprenderer');
ctx.transferFromImageBitmap(e.data.bitmap);
// ctx.canvas.toBlob(blob => console.log(blob), 'image/jpeg');
break;
}
};
const input = document.querySelector('input[type="file"]');
input.addEventListener('change', async e => {
const image = e.target.files[0];
const bitmap = await createImageBitmap(image);
canvas.width = bitmap.width;
canvas.height = bitmap.height;
canvas.style.width = canvas.width / devicePixelRatio;
canvas.style.height = canvas.height / devicePixelRatio;
const offscreen = canvas.transferControlToOffscreen();
worker.postMessage({msg: 'start', canvas: offscreen, bitmap}, [offscreen, bitmap]);
});
URL.revokeObjectURL(url); // cleanup
})();
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-43475701-1', 'ebidel.github.io');
ga('send', 'pageview');
</script>
</body>
</html>