Skip to content

Commit 1946523

Browse files
authored
Merge pull request #7694 from perminder-17/patch-9
Adding loadBlob() docs
2 parents 34cd557 + 0aa030c commit 1946523

File tree

1 file changed

+44
-1
lines changed

1 file changed

+44
-1
lines changed

src/io/files.js

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -782,7 +782,50 @@ function files(p5, fn){
782782
}
783783
}
784784
};
785-
785+
786+
/**
787+
* Loads a file at the given path as a Blob, then returns the resulting data or
788+
* passes it to a success callback function, if provided. On load, this function
789+
* returns a `Promise` that resolves to a Blob containing the file data.
790+
*
791+
* @method loadBlob
792+
* @param {String|Request} path - The path or Request object pointing to the file
793+
* you want to load.
794+
* @param {Function} [successCallback] - Optional. A function to be called if the
795+
* file successfully loads, receiving the
796+
* resulting Blob as its only argument.
797+
* @param {Function} [errorCallback] - Optional. A function to be called if an
798+
* error occurs during loading; receives the
799+
* error object as its only argument.
800+
* @returns {Promise<Blob>} A promise that resolves with the loaded Blob.
801+
*
802+
* @example
803+
* <div>
804+
* <code>
805+
* let myBlob;
806+
*
807+
* async function setup() {
808+
* createCanvas(200, 200);
809+
* background(220);
810+
* try {
811+
* // 1. Load an image file as a Blob.
812+
* myBlob = await loadBlob('assets/flower-1.png');
813+
*
814+
* // 2. Convert the Blob into an object URL.
815+
* const objectUrl = URL.createObjectURL(myBlob);
816+
*
817+
* // 3. Load that object URL into a p5.Image.
818+
* loadImage(objectUrl, (img) => {
819+
* // 4. Display the loaded image.
820+
* image(img, 0, 0, width, height);
821+
* });
822+
* } catch (err) {
823+
* console.error('Error loading blob:', err);
824+
* }
825+
* }
826+
* </code>
827+
* </div>
828+
*/
786829
fn.loadBlob = async function(path, successCallback, errorCallback) {
787830
try{
788831
const { data } = await request(path, 'blob');

0 commit comments

Comments
 (0)