Last active
May 17, 2023 08:38
-
-
Save JJetmar/7f509bbf258a42b1621f4ad9f053ceb0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Need to run chrome in insecure mode | |
| // - for flatpack: flatpak run com.google.Chrome --args --disable-web-security --allow-running-insecure-content --user-data-dir="/temp/chrome/123" | |
| (() => { | |
| const img = document.querySelector('img'); | |
| if (img.complete) { | |
| const canvas = document.createElement('canvas'); | |
| canvas.width = img.naturalWidth; | |
| canvas.height = img.naturalHeight; | |
| document.body.appendChild(canvas) | |
| const ctx = canvas.getContext('2d'); | |
| ctx.drawImage(img, 0, 0); | |
| const imgData = ctx.getImageData(0, 0, img.naturalWidth, img.naturalHeight) | |
| const buffer = imgData.data.buffer; | |
| console.log(img); | |
| console.log(buffer); | |
| } | |
| })() | |
| // Or blob async | |
| (async () => { | |
| const img = document.querySelector('img'); | |
| if (img.complete) { | |
| const canvas = document.createElement('canvas'); | |
| canvas.width = img.naturalWidth; | |
| canvas.height = img.naturalHeight; | |
| document.body.appendChild(canvas) | |
| const ctx = canvas.getContext('2d'); | |
| ctx.drawImage(img, 0, 0); | |
| const blob = await new Promise((resolve) => { | |
| canvas.toBlob( | |
| resolve, | |
| "image/jpeg", | |
| 0.95 | |
| ); | |
| }); | |
| console.log(blob); | |
| } | |
| })() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment