-
Notifications
You must be signed in to change notification settings - Fork 5
/
use-with-lib.html
143 lines (124 loc) · 4.63 KB
/
use-with-lib.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
<!--
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: Ewa Gasperowicz (@devnook)
-->
<!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>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<header class="hide-in-iframe">
<h1>
OffscreenCanvas
</h1>
<div class="desc">
The OffscreenCanvas allows to create a canvas that can be rendered off screen. It
can also be used in web workers.
</div>
<nav>
<a href="index.html">Avoid jank</a>
<a href="keep-ui-responsive.html">Keep UI responsive</a>
<a href="gain-cpu-time.html">Gain CPU time</a>
<a href="use-with-lib.html" class="selected">Use with a library</a>
</nav>
</header>
<main>
<section class="support">Your browser does not support OffscreenCanvas.</section>
<section>
<p class="hide-in-iframe">
OffscreenCanvas can be used as a progressive enhancement, also with some of the leading graphic
libraries on the market like three.js.
</p>
<p class="desc">
When you click "make busy" button, the animation on window canvas is
blocked, while OffscreenCanvas, running on a worker, still plays smoothly.
</p>
<button id="make-busy">Make me busy!</button>
<div id="busy"> </div>
<div class="display">
<div>
<h1>
Canvas on main thread
</h1>
<canvas id="canvas-window" width="400" height="200"></canvas>
</div>
<div>
<h1>
Canvas on worker
</h1>
<canvas id="canvas-worker" width="400" height="200"></canvas>
</div>
</div>
</section>
</main>
<script src="animation.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/94/three.min.js"></script>
<script src="threejs-cube.js"></script>
<script type="script/worker" id="workerCode">
let cube = null;
self.onmessage = function(e) {
switch (e.data.msg) {
case 'start':
if (!cube) {
importScripts('https://cdnjs.cloudflare.com/ajax/libs/three.js/94/three.min.js');
importScripts(e.data.origin + '/threejs-cube.js');
e.data.canvas.style = { width: 0, height: 0 }
const renderer = new THREE.WebGLRenderer({ canvas: e.data.canvas });
renderer.setSize(400, 200);
cube = new ThreejsCube(renderer);
}
cube.animate();
break;
}
};
</script>
<script>
(async () => {
// Feature detect.
document.querySelector('main').classList.toggle(
'supported', ('OffscreenCanvas' in window));
document.body.classList.toggle(
'iframe', (window.location != window.parent.location));
document.querySelector('#make-busy').addEventListener('click', () => {
document.querySelector('#busy').innerText = 'Main thread working...';
requestAnimationFrame(() => {
requestAnimationFrame(() => {
Animation.fibonacci(40); // Artificially slow down main thread.
document.querySelector('#busy').innerText = 'Done!';
});
})
});
const canvasWindow = document.querySelector("#canvas-window");
const canvasOffscreen = document.querySelector("#canvas-worker").transferControlToOffscreen();
const renderer = new THREE.WebGLRenderer({ canvas: canvasWindow });
renderer.setSize(400, 200);
const cube = new ThreejsCube(renderer);
cube.animate();
const workerCode = document.querySelector('#workerCode').textContent;
const blob = new Blob([workerCode], { type: 'text/javascript' });
const url = URL.createObjectURL(blob);
const worker = new Worker(url);
const urlParts = location.href.split('/');
if (urlParts[urlParts.length - 1].indexOf('.') !== -1) {
urlParts.pop();
}
worker.postMessage({ msg: 'start', origin: urlParts.join('/'), canvas: canvasOffscreen }, [canvasOffscreen]);
URL.revokeObjectURL(url); // cleanup
})();
</script>
</body>
</html>