|
| 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> |
0 commit comments