Skip to content

Commit

Permalink
Image to data url
Browse files Browse the repository at this point in the history
  • Loading branch information
Explosion-Scratch committed Jan 30, 2021
1 parent e87cf44 commit 5d9685e
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions bijou.js
Original file line number Diff line number Diff line change
Expand Up @@ -2652,6 +2652,29 @@ let _temp = {
script.src = url;
document.getElementsByTagName("head")[0].appendChild(script);
},
/**
* Fetches an image and runs the callback with the data url of the image.
* @memberOf bijou
* @function
* @param {String} url The url of the image to load.
* @param {Function} callback The callback function.
* @example
* //Replaces every image's url with its respective data url.
* _$.each(document.querySelectorAll('img'), (img) => {
* _$.imageToData(img.src, (data) => {
* img.src = data;
* })
* })
*/
imageToData: (async (url, callback) => {
let blob = await fetch(url).then(r => r.blob());
let dataUrl = await new Promise(resolve => {
let reader = new FileReader();
reader.onload = () => resolve(reader.result);
reader.readAsDataURL(blob);
});
callback(dataUrl);
}),
/**
* A set of functions to set and modify cookies.
* @memberOf bijou
Expand Down

0 comments on commit 5d9685e

Please sign in to comment.