I have a problem when want to use p5.js in my Angular project.
I use Angular CLI. Include p5.js in my .angular-cli.json file:
"scripts": [
"../node_modules/p5/lib/p5.min.js",
"../node_modules/p5/lib/addons/p5.sound.js",
"../node_modules/p5/lib/addons/p5.dom.js"
],
Then in my component I write:
import { Component, OnInit } from '@angular/core';
import * as p5 from 'p5';
@Component({
selector: 'app-player',
templateUrl: './player.component.html',
styleUrls: ['./player.component.scss']
})
export class PlayerComponent implements OnInit {
play: boolean = false;
constructor() { }
ngOnInit() {
const s = (p) => {
let song;
let canvas;
p.preload = () => {
console.log('preload');
song = p.loadSound('assets/music/Thunderstruck.mp3');
}
p.setup = () => {
canvas = p.createCanvas(595, 100);
canvas.parent('equalizer');
p.background(0);
}
}
let player = new p5(s);
}
onPlay() {
this.play = !this.play;
}
}
And then when I want load my song and use loadSound(), JS tells me ERROR TypeError: p.loadSound is not a function
Ok, JS can't find this method in my file, but in browser console I can write something like this:
let song = p5.prototype.loadSound('assets/music/Thunderstruck.mp3');
song.play();
and everything works (screenshot of my console)!
I tried to write in my component:
song = p.prototype.loadSound('assets/music/Thunderstruck.mp3');
It doesn't work.
What am I doing wrong?