-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
init.js
60 lines (54 loc) · 1.77 KB
/
init.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import p5 from '../core/main';
import { initialize as initTranslator } from './internationalization';
/**
* _globalInit
*
* TODO: ???
* if sketch is on window
* assume "global" mode
* and instantiate p5 automatically
* otherwise do nothing
*
* @private
* @return {Undefined}
*/
const _globalInit = () => {
// Could have been any property defined within the p5 constructor.
// If that property is already a part of the global object,
// this code has already run before, likely due to a duplicate import
if (typeof window._setupDone !== 'undefined') {
console.warn(
'p5.js seems to have been imported multiple times. Please remove the duplicate import'
);
return;
}
if (!window.mocha) {
const p5ReadyEvent = new Event('p5Ready');
window.dispatchEvent(p5ReadyEvent);
// If there is a setup or draw function on the window
// then instantiate p5 in "global" mode
if (
((window.setup && typeof window.setup === 'function') ||
(window.draw && typeof window.draw === 'function')) &&
!p5.instance
) {
new p5();
}
}
};
// make a promise that resolves when the document is ready
const waitForDocumentReady = () =>
new Promise((resolve, reject) => {
// if the page is ready, initialize p5 immediately
if (document.readyState === 'complete') {
resolve();
// if the page is still loading, add an event listener
// and initialize p5 as soon as it finishes loading
} else {
window.addEventListener('load', resolve, false);
}
});
// only load translations if we're using the full, un-minified library
const waitingForTranslator =
typeof IS_MINIFIED === 'undefined' ? initTranslator() : Promise.resolve();
Promise.all([waitForDocumentReady(), waitingForTranslator]).then(_globalInit);