I'm trying to get p5.sound into my React app, after having successfully gotten p5.js canvas functionality working.
I have a p5Wrapper for my p5 sketch. And as I said the canvas functionality works great(ish). But, I'm unable to get the sound functionality into either the parent component, the wrapper, or the sketch itself.
Any help is greatly appreciated!
I have p5.js, p5.sound, and p5.dom all local in my repo. And here are the component, wrapper, and sketch I'm working with....
THE COMPONENT
import React, { Component } from "react";
import ReactDOM from "react-dom";
import Style from "../oogles/oogles.scss";
import P5Wrapper from "../../../wrappers/P5Wrapper";
import Oogle from "../oogles/sketch.js";
class Oogles extends Component {
constructor(props) {
super(props);
this.state = {
sketch : Oogle
};
}
componentDidMount() {
console.log("oogles");
}
render() {
return (
<div className="oogle">
<h1>Oogles</h1>
<P5Wrapper sketch={this.state.sketch} />
</div>
)
}
}
export default Oogles;
THE WRAPPER:
import React, { Component } from "react";
import ReactDOM from "react-dom";
import Style from "../wrappers/wrapper.scss";
import p5 from 'p5';
import "../../p5/addons/p5.sound.js";
class P5Wrapper extends Component {
constructor(props){
super(props);
this.state = {
parentW: window.innerWidth,
parentH: '',
};
this.updateCanvasDimensions = this.updateCanvasDimensions.bind(this);
}
componentDidMount(){
this.canvas = new p5(this.props.sketch, this.el);
// window.addEventListener("resize", this.updateCanvasDimensions);
}
componentWillReceiveProps() {
}
updateCanvasDimensions(){
this.setState({ parentW: window.innerWidth })
}
render() {
return <div className="sketch" ref={(el)=>{this.el = el}}></div>;
}
}
export default P5Wrapper;
THE SKETCH
export default function Oogle(p){
let parentW = document.querySelector(".oogle").clientWidth;
let parentH = document.querySelector(".oogle").clientHeight;
let cnv = null;
p.setup = function(){
cnv = p.createCanvas(parentW, parentH);
// mic = new p5.AudioIn();
// mic.start();
// fft = p.FFT();
// fft.setInput(mic);
}
p.draw = function(){
let backgroundColor = p.color('rgb(111, 77, 111)');
// window.addEventListener("resize", updateCanvasDimensions);
parentW = document.querySelector(".oogle").clientWidth;
parentH = document.querySelector(".oogle").clientHeight;
p.background(backgroundColor);
// var spectrum = fft.analyze();
// p.print(spectrum);
// p.ellipse(spectrum[20] += 20, spectrum[20] += 20, spectrum[0] += 200, spectrum[15]);
// p.beginShape();
// for (i = 0; i < spectrum.length; i++) {
// p.vertex(i, map(spectrum[i] += 20, 0, 255, height, 0));
// }
// p.endShape();
}
}
After running create-react-app , Install the p5 package using npm install p5 . import './App. css'; import { useEffect, useRef } from 'react'; import p5 from 'p5'; function sketch(p) { // p is a reference to the p5 instance this sketch is attached to p. setup = function() { p.
This seems to be a problem faced by many... I'm using react-p5-wrapper and have similar problems too.
https://github.com/and-who/react-p5-wrapper/issues/61
I have a workaround that only works on function calls.
I added window.p5 = p5
after calling import * as p5 from "p5"
import * as ml5 from "ml5"
window.p5 = p5
import "p5/lib/addons/p5.sound"
Then I'll be able to use the p5.loadSound() function. However if I try to use new p5.Envelope() constructor it'll still throw me an error.
export default function sketch(p5) {
p5.setup = () => {
p5.createCanvas(400, 400)
song = p5.loadSound(url("./assets/hurry.mp3")) // Will work
env = new p5.Envelope() // Will error
...
So if it's just simple load and play a sound then this "hack" will work but if you need to use the constructor then it'll fail.
Update: you just need to change p5.Envelope() to p5.constructor.Envelope()
!!!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With