-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
47 lines (41 loc) · 1.52 KB
/
index.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
<!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>Web Workers 😘</title>
<link rel="stylesheet" type="text/css" href="main.css">
</head>
<body>
<div>
<input type="file" accept="image/*" id="input">
<label for="input">Choose file</label>
</div>
<canvas id="preview"></canvas>
<script type="text/javascript">
const $input = document.querySelector('#input')
const $preview = document.querySelector('#preview')
const previewCtx = $preview.getContext('2d')
const fileReader = new FileReader()
const worker = new Worker('worker.js')
worker.addEventListener('message', (d) => {
const imageData = d.data
previewCtx.putImageData(imageData, 0, 0)
})
function applyFilter() {
const imageData = previewCtx.getImageData(0, 0, $preview.width, $preview.height)
worker.postMessage(imageData, [imageData.data.buffer])
}
$input.addEventListener('change', (e) => {
const file = e.target.files[0]
createImageBitmap(file).then((bitmap) => {
$preview.height = bitmap.height
$preview.width = bitmap.width
previewCtx.drawImage(bitmap, 0, 0)
applyFilter()
})
})
</script>
</body>
</html>