Comments (6)
This is kinda rough, but should show you what I mean. Thanks!
/*
this is how I want to write it
*/
class SomeP5Component extends React.Component {
render() {
return (
<div
style={{
opacity: this.props.value || 0,
}}
>
<P5Wrapper sketch={this.props.src} />
</div>
);
}}
class App extends React.Component {
state = {
value: 0,
}
render() {
return (
<React.Fragment>
<input type='range' min='0' max='100' value={this.state.value} onChange={(value) => this.setState({ value, })} />
<SomeP5Component value={this.state.value / 100} src={'./path/to/sketch'}/>
</React.Fragment>
)
}
}
/*
this is how I currently write it
*/
class SomeP5Component extends React.PureComponent {
render() {
return (
<div>
<P5Wrapper sketch={this.props.src} />
</div>
);
}}
class App extends React.Component {
state = {
value: 0,
}
render() {
return (
<React.Fragment>
<input type='range' min='0' max='100' value={this.state.value} onChange={(value) => this.setState({ value, })} />
<div
style={{
opacity: this.state.value,
}}
>
<SomeP5Component src={'./path/to/sketch'} />
</div>
</React.Fragment>
)
}
}
from react.
I thought I'd be a little bit more detailed about my issue. This is the component I'm using, which automatically fits the p5 canvas to its parent element, and changes the size of the canvas as the parent element changes. Changing the size of the canvas will always cause it to re-initialise, but if I were to update the props of my component (say, set the wrapper style for example...) the canvas will also always re-initialise.
At the moment, I've gotten around this by making my component a purecomponent, so I can change the elements it is contained within, but I would like to be able to update the props of my component and not have the p5 canvas re-render.
Please disregard the comment in my code - this component was written for someone who doesn't know how to use react, and my goal was to keep the sketch.js file as clean as possible. Although I left the comment there, I do believe I solved my issue, with your help of course!
from react.
@lewiswolf your code looks reasonable to me, perhaps you could attach an example of what you would prefer the usage to look like in an "example of what I want" code suggestion? Happy to support you if I can.
from react.
Hey @lewiswolf, could you do something like this or am I misunderstanding the issue?
app.js
import React, { useState } from 'react';
import P5Wrapper from 'react-p5-wrapper';
import { sketch } from './sketch';
function SomeP5Component(props) {
return (
<div style={{ opacity: props.opacity || 0 }}>
<P5Wrapper sketch={props.sketch} />
</div>
);
}
function App() {
const [state, setState] = useState(0);
return (
<React.Fragment>
<input
type="range"
min={0}
max={100}
value={state}
onChange={event => setState(event.target.value)}
/>
<SomeP5Component opacity={state / 100} sketch={sketch} />
</React.Fragment>
)
}
export default App;
sketch.js
export function sketch(p5) {
p5.setup = () => p5.createCanvas(300, 300);
p5.draw = () => {
p5.background(100);
p5.ellipseMode(p5.CENTER);
p5.ellipse(p5.width / 2, p5.height / 2, 100, 100);
};
};
If this is the kind of solution you are looking for then I would also suggest skipping the second component in this case and trying this instead for app.js:
import React, { useState } from 'react';
import P5Wrapper from 'react-p5-wrapper';
import { sketch } from './sketch';
function App() {
const [state, setState] = useState(0);
return (
<React.Fragment>
<input
type="range"
min={0}
max={100}
value={state}
onChange={event => setState(event.target.value)}
/>
<div style={{ opacity: state / 100 }}>
<P5Wrapper sketch={sketch} />
</div>
</React.Fragment>
)
}
export default App;
The sketch re-rendering depends on it's containing component and so if the containing component must re-render then the sketch must but you can pass props into the sketch and use the myCustomRedrawAccordingToNewPropsHandler
function as detailed here to accept the new state and for you to do with as you need to in such a case. I have a comment on this issue which relates somewhat to this point but not sure when I will make such a change, either way, it is just a bit of sugar to what you can already do like I mentioned above anyway if and when I implement the idea in the comment.
If this is not the solution you are looking for, do tell me and I will try to support further 👍
from react.
I'm pretty set on class components for this project, although I don't quite know if that effects react's render method. I haven't tried anything yet, but will give a whirl and get back to you. However, 'The sketch re-rendering depends on it's containing component and so if the containing component must re-render then the sketch must', this is what I mean. If P5Wrapper was a PureComponent, it wouldn't update if it's parent element updated.
from react.
Closing for now since I don't think it makes sense after testing locally with functional, class and pure components. The render process doesn't seem to differ much and doesn't unneccessarily re-render without a shared state change from what I've seen.
from react.
Related Issues (20)
- Migrate to Vite and Vitest
- Migrate to the @P5-wrapper npm org
- Loading image P5-react
- React's strict mode causes the canvas to be created twice HOT 4
- An example of exposing imperative methods from the sketch? HOT 9
- unexpected array behavior inside p5 sketch function HOT 1
- 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 3
- 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
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.