-
Notifications
You must be signed in to change notification settings - Fork 46
Add foundational support for loading p5 addons in preview-frame.ts. #63
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,22 +5,47 @@ require("../css/preview-frame.css"); | |
interface PreviewFrameWindow extends PreviewFrame.Runner { | ||
// This is exported by p5 when it's in global mode. | ||
noLoop: () => void; | ||
|
||
// This is the p5 constructor. An undocumented feature is that | ||
// even the first argument is actually *optional*; if omitted, | ||
// p5 will initialize itself in global mode. | ||
p5: (sketch?: Function, node?: HTMLElement, sync?: boolean) => void; | ||
} | ||
|
||
let global = window as PreviewFrameWindow; | ||
|
||
function loadP5(version: string, cb?: () => void) { | ||
let url = '//cdnjs.cloudflare.com/ajax/libs/p5.js/' + version + '/p5.js'; | ||
function loadScript(url, cb?: () => void) { | ||
let script = document.createElement('script'); | ||
|
||
cb = cb || (() => {}); | ||
|
||
script.onload = cb; | ||
script.onerror = () => { | ||
console.log("Failed to load script: " + url); | ||
}; | ||
script.setAttribute('src', url); | ||
|
||
document.body.appendChild(script); | ||
} | ||
|
||
function loadScripts(urls: string[], cb?: () => void) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 you could get fancy with promises, but looping over the scripts and loading each should get the job done There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I really like promises, but I'm not actually sure if we include the polyfill in the preview-frame... and I'd like to keep the preview frame as light as possible in terms of external dependencies and stuff, hrm. Lemme look into it! |
||
cb = cb || (() => {}); | ||
|
||
let i = 0; | ||
let loadNextScript = () => { | ||
if (i === urls.length) { | ||
return cb(); | ||
} | ||
loadScript(urls[i++], loadNextScript); | ||
}; | ||
|
||
loadNextScript(); | ||
} | ||
|
||
function p5url(version: string) { | ||
return `//cdnjs.cloudflare.com/ajax/libs/p5.js/${version}/p5.js`; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. might be worth it to allow a custom URL for self-hosting? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, this is actually filed in #48 actually--I'd prefer to keep this PR nice and small, and add support for that in a future one. 😄 |
||
} | ||
|
||
function LoopChecker(sketch: string, funcName: string, maxRunTime: number) { | ||
let self = { | ||
wasTriggered: false, | ||
|
@@ -92,9 +117,14 @@ function startSketch(sketch: string, p5version: string, maxRunTime: number, | |
errorCb(message, line); | ||
}); | ||
|
||
document.body.appendChild(sketchScript); | ||
|
||
loadP5(p5version); | ||
loadScripts([ | ||
p5url(p5version), | ||
], () => { | ||
document.body.appendChild(sketchScript); | ||
if (document.readyState === 'complete') { | ||
new global.p5(); | ||
} | ||
}); | ||
} | ||
|
||
global.startSketch = startSketch; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
o hai :-) just a drive-by suggestion here: might be nice to listen for script load errors and log something to the console for debugging
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah good idea! I'll add that--this will also be useful for when we allow people to specify arbitrary URLs to third-party add-on libraries, since we want to provide good feedback if they misspell any of those URLs or something.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool! Glad my drive-by review was helpful ^_^
On Monday, August 29, 2016, Atul Varma notifications@github.com wrote: