-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implementation of loadBytes #2674
Comments
you'll only run into #2568 if you're using p5 in instance mode. probably the reason you're not seeing i was working on something similar a while back using an augmented version of p5.prototype.loadBytes(file, callback, errorCallback) {
let data = {};
var self = this;
this.httpDo(
file,
'GET',
'arrayBuffer',
function(arrayBuffer) {
data.bytes = new Uint8Array(arrayBuffer);
if (typeof callback !== 'undefined') {
callback(data);
}
self._decrementPreload();
},
errorCallback
);
return data;
}; note the call to |
@Spongman |
For reference here is my latest "working" version. I have on my list to pull request this to p5. Any comments before I do so? Is it preferable to use p5.prototype.registerPreloadMethod('loadBytes');
p5.prototype.loadBytes = function(file, callback) {
var self = this;
var data = {};
var oReq = new XMLHttpRequest();
oReq.open("GET", file, true);
oReq.responseType = "arraybuffer";
oReq.onload = function(oEvent) {
var arrayBuffer = oReq.response;
if (arrayBuffer) {
data.bytes = new Uint8Array(arrayBuffer);
if (callback) {
callback(data);
}
self._decrementPreload();
}
}
oReq.send(null);
return data;
} |
@shiffman Using |
i dug up the httpDo implementation i did, if you're interested. it's here: master...Spongman:loadBytes |
FYI I am writing this while streaming. I would like to make a video tutorial where I walk though the steps of how to contribute this "enhancement" but I did not get it today. I think this would be useful (?) so I am holding off on submitting until another recording session. |
@shiffman this would be great, thank you! |
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.
I am opening a new issue since #40 about file i/o is a long thread and this relates specifically to
loadBytes()
. I am working on an example that loads binary data and thought I would take a crack at addingloadBytes()
to p5. I have something working and am happy to pull request an implementation, however, I have a couple questions I'd love thoughts or advice on:The working new method is towards the bottom.
As discussed in Example of async function for callback and preload() on the libraries wiki, it's required that an original object pointer is retained. I've done this by placing the
Uint8Array
into avalues
property of a blank object. Thoughts on how this could be improved? Can I set the values of aUint8Array
if I don't know the size in advance?I think I am running into registerPreloadMethod mechanism is broken #2568 as I can only get this to work in 0.5.11 (no later versions).
The text was updated successfully, but these errors were encountered: