Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trying to include vanilla .js class #30

Closed
vennsoh opened this issue Jun 3, 2019 · 6 comments
Closed

Trying to include vanilla .js class #30

vennsoh opened this issue Jun 3, 2019 · 6 comments

Comments

@vennsoh
Copy link

vennsoh commented Jun 3, 2019

Hi, first of all I'd like to say this is the best p5 react wrapper so far! @and-who
I'm new to all these, so do give me some guidance here.

I'm trying to create create a simple particle system here.

My particle class
`
import p5 from "p5";

export default class Particle {
constructor() {
this.x = random(width);
this.y = random(height);
this.diameter = random(10, 30);
this.speed = 1;
}

move() {
this.x += random(-this.speed, this.speed);
this.y += random(-this.speed, this.speed);
}

display() {
ellipse(this.x, this.y, this.diameter, this.diameter);
}
}
`

My p5 sketch
`
import p5 from "p5";
import Particle from "./Particle";
global.p5 = p5;

export default function sketch(p) {
let bug1;
let bug2;

p.setup = () => {
p.createCanvas(710, 400);
bug1 = new Particle();
bug2 = new Particle();
};

p.draw = () => {
p.background(50, 89, 100);
bug1.move();
bug1.display();
bug2.move();
bug2.display();
};
}
`

But I can't seem to get it working...

@and-who
Copy link
Contributor

and-who commented Jun 3, 2019

Can you provide a bit more Information about what is not working?
Do you get any Errors? Can you see the Canvas?

@vennsoh
Copy link
Author

vennsoh commented Jun 3, 2019

Hi @and-who I've uploaded my code to https://github.com/vennsoh/react-p5
I'm getting...
Uncaught ReferenceError: random is not defined at new Particle (particle.js:20) at p5.p.setup (sketch3.js:14) at p5.eval (p5.js:49814) at p5.eval (p5.js:49751)

I suspect my Particle.js class doesn't know how to read the methods/functions from p5 lib. But I'm not sure how to make it work.

@shtrih
Copy link

shtrih commented Jun 3, 2019

@vennsoh Provide p5 context from sketch as an argument of Particle constructor. Next use functions from this context like p5context.ellipse(...).

Particle {
constructor(p5context) {
  this.p5context = p5context;
  ...
}

display() {
  this.p5context.ellipse(this.x, this.y, this.diameter, this.diameter);
}
bug1 = new Particle(p);

And you no need use import p5 from "p5"; global.p5 = p5; in your sketch.

@vennsoh
Copy link
Author

vennsoh commented Jun 3, 2019

@shtrih Thanks so much! It helps and it works!

@vennsoh vennsoh closed this as completed Jun 3, 2019
@vennsoh
Copy link
Author

vennsoh commented Jun 3, 2019

@shtrih However when I try
export default class Particle { constructor(p5) { this.p5 = p5 this.x = this.p5.random(width) <-- The random method doesn't work?
I tried this.p5.random(), p5.random() both don't work?

@vennsoh vennsoh reopened this Jun 3, 2019
@vennsoh
Copy link
Author

vennsoh commented Jun 3, 2019

Ah sorry ignore this, obviously it doesn't work, the "width" isn't a number.

@vennsoh vennsoh closed this as completed Jun 3, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants