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
// Two ways to serve transparent GIF | |
var buf = new Buffer([ | |
0x47, 0x49, 0x46, 0x38, 0x39, 0x61, 0x01, 0x00, 0x01, 0x00, | |
0x80, 0x00, 0x00, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x2c, | |
0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x02, | |
0x02, 0x44, 0x01, 0x00, 0x3b]); | |
res.send(buf, { 'Content-Type': 'image/gif' }, 200); |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
img.addEventListener('load', () => { | |
/* ... some code have been truncated here ... */ | |
ctx.putImageData(imageData, 0, 0); | |
// Canvas.toBlob() creates a blob object representing the image contained in the canvas | |
// It takes a callback function as its argument whose first parameter is the | |
canvas.toBlob(blob => { | |
// Create a download link for the blob object |
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
fetch('https://picsum.photos/list') | |
.then(response => response.json()) | |
.then(data => data.filter(squareImages)) | |
.then(collectionToCSV(exportFields)) | |
.then(csv => { | |
const blob = new Blob([csv], { type: 'text/csv' }); | |
downloadBlob(blob, 'photos.csv'); | |
}) | |
.catch(console.error); |
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
function squareImages({ width = 1, height = width } = {}) { | |
return width / height === 1; | |
} | |
function collectionToCSV(keys = []) { | |
return (collection = []) => { | |
const headers = keys.map(key => `"${key}"`).join(','); | |
const extractKeyValues = record => keys.map(key => `"${record[key]}"`).join(','); | |
return collection.reduce((csv, record) => { |
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
// Blob object for the content to be download | |
const blob = new Blob( | |
[ /* CSV string content here */ ], | |
{ type: 'text/csv' } | |
); | |
// Create a download link for the blob content | |
const downloadLink = downloadBlob(blob, 'records.csv'); | |
// Set the title and classnames of the link |
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
function downloadBlob(blob, filename) { | |
// Create an object URL for the blob object | |
const url = URL.createObjectURL(blob); | |
// Create a new anchor element | |
const a = document.createElement('a'); | |
// Set the href and download attributes for the anchor element | |
// You can optionally set other attributes like `title`, etc | |
// Especially, if the anchor element will be attached to the DOM |
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
fetch('https://picsum.photos/id/6/240') | |
.then(response => response.blob()) | |
.then(blob => { | |
// Create a new FileReader innstance | |
const reader = new FileReader; | |
// Add a listener to handle successful reading of the blob | |
reader.addEventListener('load', () => { | |
const image = new 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
const data = { | |
name: 'Glad Chinda', | |
country: 'Nigeria', | |
role: 'Web Developer' | |
}; | |
// SOURCE 1: | |
// Creating a blob object from non-blob data using the Blob constructor | |
const blob = new Blob([ JSON.stringify(data) ], { type: 'application/json' }); |
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
const wrapper = document.getElementById('image-wrapper'); | |
const img = wrapper.querySelector('img'); | |
const canvas = wrapper.querySelector('canvas'); | |
img.addEventListener('load', () => { | |
canvas.width = img.width; | |
canvas.height = img.height; | |
const ctx = canvas.getContext('2d'); | |