Comments (9)
Im not sure if I understand your Problem.
You want to have the testValue
from the props to be available in the p.setup
Function?
e.g.:
p.setup = function () {
console.log(testValue)
};
This is currently not Possible, since the 'setup' Function is called via the p5 constructor and at this point the testValue
is not yet set by the myCustomRedrawAccordingToNewPropsHandler
from react.
Yes, I'm trying to figure how can I pass the prop value over into setup().
As it's fairly common to generate a bunch of particles off a .js class.
I created my UI in React so that I can manipulate the parameters (propValue)
The value is needed in the .js class.
If this is currently not possible, do you know if there's any hacky way? Or on top of your mind any pseudo code steps you could school me so I can go investigate?
let propValue;
p.setup = () => {
p.createCanvas(640, 360)
for (let i = 0; i < 10; i++) {
flock.push(new Particle(p, propValue))
}
}
p.myCustomRedrawAccordingToNewPropsHandler = props => {
if (props.propValue) {
propValue = props.propValue
}
}
It's interesting that it works in draw() but not setup() ?
from react.
You can write your own init()
Function and call this in the draw
function.
e.g.:
initialized = false;
init = function () {
console.log(propValue);
}
...
p.draw = function () {
if( !initialized ) {
init();
initialized = true;
}
...
};
from react.
Thanks @and-who
I tried but it didn't work. The prop value is able to get passed into init function scope but not into the setup(). I believe setup() is only run once. If I use the OOTB createSlider from p5 it'll work but I can't hook it up with my own React UI.
import Particle from "./Particle"
import "p5/lib/addons/p5.dom.min"
export default function sketch(p) {
let bug1, bug2, diameter
diameter = 10
const init = () => {
console.log("here0 " + diameter)
p.setup = () => {
p.createCanvas(710, 400)
// diameter = p.createSlider(10, 30, 15, 1)
bug1 = new Particle(p, diameter)
bug2 = new Particle(p, diameter)
}
}
init()
p.myCustomRedrawAccordingToNewPropsHandler = function(props) {
if (props.diameter) {
diameter = props.diameter
}
}
p.draw = () => {
init()
p.background(50, 89, 100)
bug1.move()
bug1.display()
bug2.move()
bug2.display()
}
}
Particle.js
export default class Particle {
constructor(p5, diameter) {
this.p5 = p5
this.diameter = diameter
this.x = this.p5.random(300)
this.y = this.p5.random(300)
this.speed = 1
}
move() {
this.x += this.p5.random(-this.speed, this.speed)
this.y += this.p5.random(-this.speed, this.speed)
}
display() {
this.p5.ellipse(
this.x,
this.y,
// this.diameter.value(),
// this.diameter.value()
this.diameter,
this.diameter
)
}
}
from react.
p.myCustomRedrawAccordingToNewPropsHandler = function(props) {
if (props.diameter) {
bug1.diameter = props.diameter;
bug2.diameter = props.diameter;
}
}
from react.
@shtrih Oh awesome! I didn't know you can call your particles in the p.myCustom... function.
Now my slider is able to manipulate the value. 1 step closer!
Sketch.tsx
import Particle from "./Particle";
export default p => {
let bug1;
p.setup = () => {
p.createCanvas(710, 400);
bug1 = new Particle(p);
};
p.myCustomRedrawAccordingToNewPropsHandler = function(props) {
if (props.diameter) {
bug1.diameter = props.diameter;
}
console.log(bug1);
};
p.draw = () => {
p.background(50, 89, 100);
bug1.move();
bug1.display();
};
};
Particle.js
export default class Particle {
constructor(p5) {
this.p5 = p5
this.x = this.p5.random(300)
this.y = this.p5.random(300)
this.speed = 1
this.diameter = 10
}
move() {
this.x += this.p5.random(-this.speed, this.speed)
this.y += this.p5.random(-this.speed, this.speed)
}
display() {
this.p5.ellipse(
this.x,
this.y,
this.diameter,
this.diameter
)
}
}
The ellipse is this weird shape?
from react.
The newly adjusted value is passed to the particle (the shape is getting bigger)
When first initialise, the particle is getting the default diameter value that I've defined in React.
But not sure why it's getting a weird look (squashed)
from react.
Weirdly when I do the same for bug1.speed (just a single digit number) it works nicely. The React slider is able to adjust the speed for the particle.
from react.
Okay, figure that out, because it's returning a string, the string should be converted --> number.
bug1.diameter = Number(props.diameter);
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.