Comments (3)
This was my problem too. I ended up not using states cause it also clears the mouse event handlers on my matterjs side as the canvas gets re-rendered. I tried to use React.memo on the wrapper but I got more bad side effects. I suggest using just refs to store values and put your sketch function inside the component where the wrapper is so that it has access to the refs:
export default function App() {
const measurements = useRef([
{
id: 1,
ref: {
x: 100,
y: 100,
},
},
{
id: 2,
ref: {
x: 200,
y: 100,
},
},
]);
const sketch = (p5) => {
p5.setup = () => {
// loop thru measurements.current
// and create your Measurement objects
}
};
return (
<div className="App">
<ReactP5Wrapper sketch={sketch} />
</div>
)
}
from react.
I think this is more to do with the fact you are trying to use p5
as if it is in global mode, i.e you are assuming that the variables and methods will exist or do the cleanup for you. In this case, the error is clear when it says WARNING: Too many active WebGL contexts. Oldest context will be lost.
. This error is triggered by p5
itself when you create more WebGL instances than is required. This library will properly destroy a p5
instance but p5
itself will only clean up if certain pre-requisites are met, but in your case, there seems to be some dangling reference to a WebGL context on each cycle which is then never released and thus "too many" exist after some time.
I also don't believe that moving the variables into a ref is the answer either, in this case you actually have constant data and from what I can see, the OP never calls the setter function (which is also bad practice in React to pass around). In such as case I would just have a const
in the top level, like:
type Props = { ... }
const measurements = {...};
const sketch: Sketch<T> = p5 => {...}
export function App(props: Props) { ... }
In the Measurement
class you don't need initVariables
either since updateWithProps
is called on every render and will get all props on initial render also too.
Just some tips.
from react.
Related Issues (20)
- ReactP5Wrapper has additional pixels in height HOT 3
- Could not find a declaration file but the code works HOT 10
- Duplicate canvas when running in vite dev HOT 5
- TypeError: p5.loadSound is not a function HOT 4
- P5-wrapper size HOT 5
- Stop rerender of p5 component when redux variables update HOT 3
- Latest commits not in the npm registry HOT 5
- Issue building with Vite 5 HOT 16
- npm ERR! Usage Error: This project is configured to use npm HOT 2
- windowResized() not firing HOT 4
- Error in nextjs HOT 7
- Change p5 dependency version to minimum 1.4.1 HOT 9
- Examples directory doesn't exist, link broken on README HOT 3
- Module parse failed: Identifier 'XX' has already been declared (7762:41) HOT 3
- Is this package supported in React Native? HOT 1
- How to access custom props using abstracted updateWithProps
- Custom updaters as props for use in abstracted methods
- Missing typings for `ReactP5WrapperGuard` in 5.0.0-rc.0 HOT 2
- npm package for version 4.4.1 is empty HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react.