OLD | NEW |
(Empty) | |
| 1 <img id="png"/> |
| 2 <img id="jpeg-high"/> |
| 3 <img id="jpeg-low"/> |
| 4 <img id="webp-high"/> |
| 5 <img id="webp-low"/> |
| 6 <script id="myWorker" type="text/worker"> |
| 7 self.onmessage = function (e) { |
| 8 var offCanvas = new OffscreenCanvas(50, 50); |
| 9 var offctx = offCanvas.getContext("2d"); |
| 10 offctx.fillStyle = "red"; |
| 11 offctx.fillRect(0, 0, 25, 25); |
| 12 offctx.fillStyle = "green"; |
| 13 offctx.fillRect(25, 0, 25, 25); |
| 14 offctx.fillStyle = "blue"; |
| 15 offctx.fillRect(0, 25, 25, 25); |
| 16 offctx.fillStyle = "black"; |
| 17 offctx.fillRect(25, 25, 25, 25); |
| 18 offctx.strokeStyle = "yellow"; |
| 19 offctx.strokeRect(0, 0, 50, 50); |
| 20 |
| 21 offCanvas.convertToBlob() |
| 22 .then(function(blob) { |
| 23 self.postMessage({version: "png", data:blob}); |
| 24 }); |
| 25 |
| 26 offCanvas.convertToBlob({type: "image/jpeg"}) |
| 27 .then(function(blob) { |
| 28 self.postMessage({version: "jpeg-high", data:blob}); |
| 29 }); |
| 30 |
| 31 offCanvas.convertToBlob({type: "image/jpeg", quality: 0.2}) |
| 32 .then(function(blob) { |
| 33 self.postMessage({version: "jpeg-low", data:blob}); |
| 34 }); |
| 35 |
| 36 offCanvas.convertToBlob({type: "image/webp"}) |
| 37 .then(function(blob) { |
| 38 self.postMessage({version: "webp-high", data:blob}); |
| 39 }); |
| 40 |
| 41 offCanvas.convertToBlob({type: "image/webp", quality: 0.2}) |
| 42 .then(function(blob) { |
| 43 |
| 44 self.postMessage({version: "webp-low", data:blob}); |
| 45 }); |
| 46 } |
| 47 </script> |
| 48 <script> |
| 49 if (window.testRunner) { |
| 50 testRunner.waitUntilDone(); |
| 51 } |
| 52 |
| 53 var pngImage = document.getElementById('png'); |
| 54 var jpegImageHigh = document.getElementById('jpeg-high'); |
| 55 var jpegImageLow = document.getElementById('jpeg-low'); |
| 56 var webpImageHigh = document.getElementById('webp-high'); |
| 57 var webpImageLow = document.getElementById('webp-low'); |
| 58 var numTestCount = 5; |
| 59 function imageLoaded() { |
| 60 numTestCount--; |
| 61 if (numTestCount == 0 && window.testRunner) { |
| 62 window.testRunner.notifyDone(); |
| 63 } |
| 64 } |
| 65 pngImage.addEventListener('load', imageLoaded); |
| 66 jpegImageHigh.addEventListener('load', imageLoaded); |
| 67 jpegImageLow.addEventListener('load', imageLoaded); |
| 68 webpImageHigh.addEventListener('load', imageLoaded); |
| 69 webpImageLow.addEventListener('load', imageLoaded); |
| 70 |
| 71 var workerBlob = new Blob([document.getElementById('myWorker').textContent]); |
| 72 var worker = new Worker(URL.createObjectURL(workerBlob)); |
| 73 worker.addEventListener("message", function(msg) { |
| 74 var blob = msg.data.data; |
| 75 switch (msg.data.version) { |
| 76 case 'png': |
| 77 pngImage.src = URL.createObjectURL(blob); |
| 78 break; |
| 79 case 'jpeg-high': |
| 80 jpegImageHigh.src = URL.createObjectURL(blob); |
| 81 break; |
| 82 case 'jpeg-low': |
| 83 jpegImageLow.src = URL.createObjectURL(blob); |
| 84 break; |
| 85 case 'webp-high': |
| 86 webpImageHigh.src = URL.createObjectURL(blob); |
| 87 break; |
| 88 case 'webp-low': |
| 89 webpImageLow.src = URL.createObjectURL(blob); |
| 90 break; |
| 91 } |
| 92 }); |
| 93 worker.postMessage(""); |
| 94 </script> |
| 95 |
OLD | NEW |