Skip to content

Commit 3b1fe4f

Browse files
committed
Implementing loadBytes() processing#2674
This commit adds the loadBytes() function to p5.js as demonstrated during a livestream on The Coding Train. There are some remaining issues here. For example, there are no tests. I am also not using httpDo() which would be more of the convention here for p5 as discussed in processing#2674. I am also handling errors like 404 or CORS in perhaps an odd way and would be happy to any feedback.
1 parent 4c2f42b commit 3b1fe4f

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

src/core/core.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,7 @@ p5.prototype._preloadMethods = {
590590
loadImage: p5.prototype,
591591
loadStrings: p5.prototype,
592592
loadXML: p5.prototype,
593+
loadBytes: p5.prototype,
593594
loadShape: p5.prototype,
594595
loadTable: p5.prototype,
595596
loadFont: p5.prototype,

src/io/files.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,68 @@ p5.prototype.loadXML = function() {
683683
return ret;
684684
};
685685

686+
/**
687+
* Loads a binary file from a file or a URL, and returns an Object
688+
* with a bytes property containing a Uint8Array.
689+
*
690+
* This method is asynchronous, meaning it may not finish before the next
691+
* line in your sketch is executed.
692+
*
693+
* @method loadBytes
694+
* @param {String} path name of the file or url to load
695+
* @param {function} [callback] function to be executed after
696+
* loadBytes() completes, data is passed
697+
* in as first argument
698+
* @param {function} [errorCallback] function to be executed if
699+
* there is an error, response is passed
700+
* in as first argument
701+
* @return {Object} contains a Uint8Array bytes property
702+
* @example
703+
*
704+
* <p>Calling loadBytes() inside preload() guarantees to complete the
705+
* operation before setup() and draw() are called.</p>
706+
*
707+
* <div><code>
708+
* // TODO: Add Example with preload
709+
* </code></div>
710+
*
711+
* <p>Outside of preload(), you may supply a callback function to handle the
712+
* data:</p>
713+
* <div><code>
714+
* // TODO: Add Example with callback
715+
* </code></div>
716+
*
717+
* @alt
718+
* 50x50 ellipse that changes from black to white depending on the current humidity
719+
* 50x50 ellipse that changes from black to white depending on the current humidity
720+
*
721+
*/
722+
p5.prototype.loadBytes = function(path, callback, errorCallback) {
723+
var self = this;
724+
var ret = {};
725+
var oReq = new XMLHttpRequest();
726+
oReq.open('GET', path, true);
727+
oReq.responseType = 'arraybuffer';
728+
oReq.onload = function(oEvent) {
729+
if (oReq.status >= 400) {
730+
return;
731+
}
732+
var arrayBuffer = oReq.response;
733+
if (arrayBuffer) {
734+
ret.bytes = new Uint8Array(arrayBuffer);
735+
if (callback) {
736+
callback(ret);
737+
}
738+
self._decrementPreload();
739+
}
740+
};
741+
if (errorCallback) {
742+
oReq.onerror = errorCallback;
743+
}
744+
oReq.send(null);
745+
return ret;
746+
};
747+
686748
/**
687749
* Method for executing an HTTP GET request. If data type is not specified,
688750
* p5 will try to guess based on the URL, defaulting to text. This is equivalent to

0 commit comments

Comments
 (0)