I am using an npm, webpack, babel environment to write an application with p5.js. To be able to have the sketch as a module, I have the sketch in instance mode and import the library and add-ons as modules:
import p5 from 'p5';
import 'p5/lib/addons/p5.sound';
import 'p5/lib/addons/p5.dom';
Then I load them to the window inside my sketch:
const sketch = (p5) => {
window.p5 = p5;
...
}
new p5(sketch);
When I try to use:
amp = new p5.Amplitude()
I get a 'p5.Amplitude is not a constructor' error. My prediction is that there is a conflict between naming the library p5 on the window and using the constructors from the library that use p5.something like p5.Amplitude, p5.Vector, p5.Soundfile. I have not been able to find a workaround to using these objects or constructors within instance mode. I am however able to use the methods from these objects that do not require a constructor. For example, loadSound()
is a method of p5.Soundfile. The following works:
sound = p5.loadSound('assets/sound.wav)
but when I try console.log(p5.SoundFile)
I get undefined.
I am lost!
window.p5 = p5
to be able to load the library using es6 modules. With the latter I am making the library available in the window after usingimport p5 from '....'
. I am able to use the rest of the functionality just like instance mode using p5 instead of p. Def not a javascript expert either, but I based this decision based on this project which seems to pull it off smoothly. Cheers! – Divisible