Skip to content

Commit 9ca6506

Browse files
authored
Merge pull request GoogleChromeLabs#104 from GoogleChromeLabs/webworkers
Add code from Episode 12 - WebWorkers
2 parents 01aaf1a + ffdf764 commit 9ca6506

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed

index.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,14 @@ <h2>Supercharged Episodes (<a href="https://www.youtube.com/playlist?list=PLNYkx
9797
<a href="./streaming-service-worker">Episode 10. Streaming ServiceWorker</a>
9898
(<a href="https://www.youtube.com/watch?v=3Tr-scf7trE">Live-coding session</a>)
9999
</li>
100+
<li>
101+
<a href="./webgl-image-processing">Episode 11. WebGL Image Processing</a>
102+
(<a href="https://www.youtube.com/watch?v=_ZQOUQsw_YI">Live-coding session</a>)
103+
</li>
104+
<li>
105+
<a href="./web-workers">Episode 12. WebWrokers</a>
106+
(<a href="https://www.youtube.com/watch?v=X57mh8tKkgE">Live-coding session</a>)
107+
</li>
100108
</ul>
101109
</li>
102110
<li>

web-workers/index.html

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<!--
2+
Copyright 2017 Google Inc. All Rights Reserved.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
-->
13+
14+
<!DOCTYPE html>
15+
<html>
16+
<head>
17+
<title>SuperCharged 💫</title>
18+
<style type="text/css">
19+
#input {
20+
width:0px;
21+
height:0px;
22+
overflow: hidden;
23+
}
24+
#input + label {
25+
display:inline-block;
26+
background-color: #777;
27+
font-family: sans-serif;
28+
padding:5px;
29+
font-size:2em;
30+
color: #f3f3f3;
31+
}
32+
#input:focus +label {
33+
outline: 5px solid teal;
34+
}
35+
</style>
36+
</head>
37+
<body>
38+
<div>
39+
<input type="file" accept="image/*" name="input" id="input">
40+
<label for="input" >Choose File</label>
41+
<br>
42+
<br>
43+
<br>
44+
</div>
45+
46+
<canvas id="preview"></canvas>
47+
48+
<script type="text/javascript">
49+
const $input = document.getElementById('input')
50+
const $preview = document.getElementById('preview')
51+
const previewCtx = $preview.getContext('2d')
52+
const worker = new Worker('worker.js')
53+
54+
worker.addEventListener('message', (d) => {
55+
const imageData = d.data
56+
previewCtx.putImageData(imageData, 0, 0)
57+
});
58+
59+
function applyFilter() {
60+
const imageData = previewCtx.getImageData(0,0, $preview.width, $preview.height)
61+
worker.postMessage(imageData, [imageData.data.buffer])
62+
}
63+
64+
$input.addEventListener('change', (e) => {
65+
const file = e.target.files[0]
66+
createImageBitmap(file)
67+
.then((bitmap) => {
68+
$preview.width = bitmap.width
69+
$preview.height = bitmap.height
70+
previewCtx.drawImage(bitmap, 0, 0)
71+
applyFilter()
72+
})
73+
})
74+
</script>
75+
</body>
76+
</html>

web-workers/worker.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
Copyright 2017 Google Inc. All Rights Reserved.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
addEventListener('message', (d) => {
15+
const imageData = d.data
16+
const w = imageData.width
17+
const h = imageData.height
18+
const data = imageData.data
19+
20+
for (let x = 0; x < w; x++) {
21+
for (let y = 0; y < h; y++) {
22+
let index = (x + (y * w)) * 4
23+
data[index] = data[index] * 1.2
24+
}
25+
}
26+
postMessage(imageData, [imageData.data.buffer])
27+
})

0 commit comments

Comments
 (0)