我一直在做一个将React与p5.js集成在一起的项目。我在将我的props传递到sketch时遇到了问题。当我在sketch中引用我的props时,我得到一个props变量未定义的错误。有人知道如何在p5.js sketch中访问props吗?
import React from "react";
import p5 from 'p5';
class Sketch extends React.Component {
constructor(props) {
super(props);
this.myRef = React.createRef();
}
Sketch = (p) => {
p.setup = () => {
p.createCanvas(400, 400);
// Want to reference my props here
}
p.draw = () => {
p.background(0);
// Or reference props here
// I get an error if I try something like: p.print(props.name);
}
}
componentDidMount() {
this.myP5 = new p5(this.Sketch, this.myRef.current);
}
render() {
return (
<div ref={this.myRef}></div>
)
}
}
export default Sketch;
2条答案
按热度按时间z9ju0rcb1#
您应该考虑使用react-p5,下面是一个简单的示例:
0qx6xfy62#
我也遇到过类似的问题,我发现在componentDidMount函数中将 prop 分配给myp5可以解决这个问题,如下所示: