Instantiate Particle

Using ES6 I need to instantiate Particle constructor. Invoking constructor through default export I’m getting an error -> p.Particle is not a constructor

I’ve scaffolded playground – link

'use strict';
import P5 from 'p5';

const sketch = (p5) => {
    const AMOUNT = 100;
    const p = p5;
    let particles = [];

    p.setup = () => {
        p.createCanvas(p.windowWidth, p.windowHeight);
        p.pixelDensity(1);

        const x = Array(AMOUNT).fill().map((x, index) => new p.Particle(320, 240));

        p.background(51);
    };

    p.draw = () => {
        p.background(p.mouseX, 100, 100);

        particles.forEach((x) => {
            x.update();
            x.show();
        });
    };
};

new P5(sketch);


I doubt that p5.js is compatible w/ import. Have you checked that out yourself? :question:

  • Variable p is an instance of class p5.
  • However, in order to access a nested class inside of it, we need to directly use the p5 reference, not 1 of its instances.
  • In your sketch above, variable P5 is supposed to point to class p5.
  • But there’s yet another problem: AFAIK, class p5 doesn’t have any nested class named Particle! :expressionless:

If you wish, you can study this sketch, which uses instance mode w/ multiple classes: :man_student:

GoSubRoutine.github.io/Ball-in-the-Chamber/instance/

GitHub.com/GoSubRoutine/Ball-in-the-Chamber/tree/master/instance

1 Like

My bad, class Particle is not from P5