-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathkeep-ui-responsive.html
161 lines (144 loc) · 5.42 KB
/
keep-ui-responsive.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
<!--
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" class="selected">Keep UI responsive</a>
<a href="gain-cpu-time.html">Gain CPU time</a>
<a href="use-with-lib.html">Use with a library</a>
</nav>
</header>
<main class="supported">
<section class="support">Your browser does not support OffscreenCanvas.</section>
<section>
<p class="hide-in-iframe">
If an app has long running rendering tasks (e.g. ray tracing in WebGL), running those
tasks in a worker allows the web app’s UI to remain responsive while the rendering
task runs continuously in the background.
</p>
<p class="desc">
The animation below is running a heavy task while changing the color theme.
If you click on the button at such moment, the interaction is blocked
for a short while causing bad user experience.
</p>
<p>
<input type="checkbox" id="play" class="cbx hidden"/>
<label for="play" id="play-label" class="lbl">Runs on main thread</label>
</p>
<p>
<button id="interactive">Interact! Counter: <span id="log">0</span></button>
</p>
<div class="display">
<div>
<h1>Canvas on main thread</h1>
<p>Interaction is blocked when a theme is loading</p>
<canvas id="canvas-window" width="400" height="400"></canvas>
</div>
<div>
<h1>Canvas on worker thread</h1>
<p>Interaction works even if a theme is loading</p>
<canvas id="canvas-worker" width="400" height="400"></canvas>
</div>
</div>
</section>
</main>
<script src="animation.js"></script>
<script type="script/worker" id="workerCode">
let animationWorker = null;
self.onmessage = function(e) {
switch (e.data.msg) {
case 'start':
if (!animationWorker) {
importScripts(e.data.origin + '/animation.js');
animationWorker = new ThemedAnimation(e.data.canvas.getContext('2d'));
}
animationWorker.start();
break;
case 'stop':
if (!animationWorker) {
return;
}
animationWorker.stop();
break;
}
};
</script>
<script>
(() => {
// Feature detect.
document.querySelector('main').classList.toggle(
'supported', ('OffscreenCanvas' in window));
document.body.classList.toggle(
'iframe', (window.location != window.parent.location));
const playWindowBtn = document.querySelector('#play-window');
const playWorkerBtn = document.querySelector('#play-worker');
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 animationWindow = new ThemedAnimation(document.querySelector('#canvas-window').getContext('2d'));
const offscreen = document.querySelector('#canvas-worker').transferControlToOffscreen();
let workerStarted = false;
document.querySelector('#interactive').addEventListener('click', () => {
const logBox = document.querySelector('#log');
const value = parseInt(logBox.innerText, 10) + 1;
logBox.innerText = value;
});
const playBtn = document.querySelector('#play');
const playLabel = document.querySelector('#play-label');
playBtn.addEventListener('change', (e) => {
const inWorker = e.target.checked;
playLabel.innerHTML = inWorker ? 'Runs in worker' : 'Runs on main thread';
playAnimation(inWorker);
});
function playAnimation(inWorker) {
if (inWorker) {
animationWindow.stop();
if (workerStarted) {
worker.postMessage({ msg: 'start' });
} else {
const urlParts = location.href.split('/');
if (urlParts[urlParts.length - 1].indexOf('.') !== -1) {
urlParts.pop();
}
worker.postMessage({ msg: 'start', origin: urlParts.join('/'), canvas: offscreen }, [offscreen]);
workerStarted = true;
}
} else {
worker.postMessage({ msg: 'stop' });
animationWindow.start();
}
}
animationWindow.start();
})()
</script>
</body>
</html>