Created
November 19, 2019 06:58
-
-
Save andrew-serrano/bc22de9b763049f85ff9202b3860dd5b to your computer and use it in GitHub Desktop.
Request an image, read image data, and create blog url for an image.
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
(async function() { | |
// Request | |
let response = await fetch('https://upload.wikimedia.org/wikipedia/commons/thumb/7/77/Google_Images_2015_logo.svg/1200px-Google_Images_2015_logo.svg.png'); | |
// Reader | |
let reader = response.body.getReader(); | |
// Final Image Data | |
let imageData = []; | |
// Readable Steam | |
new ReadableStream({ | |
start(controller) { | |
function push() { | |
reader.read().then(({value,done}) => { | |
if (done) { | |
// Create Blob | |
let blob = new Blob(imageData, { | |
type: "image/png" | |
}); | |
// Create Image | |
let image = new Image(); | |
// Set Blob src | |
image.src = (window.URL || window.webkitURL).createObjectURL(blob); | |
// Append DOM | |
document.getElementById("append").append(image); | |
// Close Stream | |
controller.close(); | |
return; | |
} | |
// Push image data | |
imageData.push(value); | |
// Push data | |
controller.enqueue(value); | |
// Recursively push data | |
push(); | |
}); | |
} | |
// Init | |
push(); | |
} | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment