Skip to content
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

docs: render.js preload fix #4227

Merged
merged 1 commit into from
Jan 3, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions docs/yuidoc-p5-theme/assets/js/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,19 @@ var renderCode = function(sel) {
'touchStarted', 'touchMoved', 'touchEnded',
'keyPressed', 'keyReleased', 'keyTyped'];
var _found = [];
// p.preload is an empty function created by the p5.sound library in
// order to use the p5.js preload system to load AudioWorklet modules
// before a sketch runs, even if that sketch doesn't have its own
// preload function.
// However, this causes an error in the eval code below because the
// _found array will always contain "preload", even if the sketch in
// question doesn't have a preload function. To get around this, we
// delete p.preload before eval-ing the sketch and add it back
// afterwards if the sketch doesn't contain its own preload function.
// For more info, see: https://github.com/processing/p5.js-sound/blob/master/src/audioWorklet/index.js#L22
if (p.preload) {
delete p.preload;
}
with (p) {
// Builds a function to detect declared functions via
// them being hoisted past the return statement. Does
Expand Down Expand Up @@ -204,7 +217,7 @@ var renderCode = function(sel) {
].join('\n'));
}
// If we haven't found any functions we'll assume it's
// just a setup body.
// just a setup body with an empty preload.
if (!_found.length) {
p.setup = function() {
p.createCanvas(100, 100);
Expand All @@ -221,10 +234,12 @@ var renderCode = function(sel) {
_found.forEach(function(name) {
p[name] = eval(name);
});
// Ensure p.preload exists even if sketch doesn't have the function.
p.preload = p.preload || function() {};
p.setup = p.setup || function() {
p.createCanvas(100, 100);
p.background(200);
}
};
}
};
}
Expand Down