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

It creates new canvas once state is changed #415

Open
nijatmursali opened this issue Jun 24, 2024 · 2 comments
Open

It creates new canvas once state is changed #415

nijatmursali opened this issue Jun 24, 2024 · 2 comments

Comments

@nijatmursali
Copy link

nijatmursali commented Jun 24, 2024

I'm having a problem with my code where I am using P5-wrapper/react to show the 2D model I created. However, whenever React states changes it re-creates the canvas again and again it gives warning that WARNING: Too many active WebGL contexts. Oldest context will be lost..

My code is a bit complex, thus I have created a simple example in this codesandbox, but I will also give a bit information.

I got Measurement class where I am using to set up specific measurement objects.

class Measurement {
  constructor(p5, initVariables, onChangeDraw, _ref) {
    this.p5 = p5;
    this.initVariables = initVariables;
    this.onChangeDraw = onChangeDraw;
    this.ref = _ref;
    this.pointActive = false;
    this.activePoint = this.p5.createVector(0, 0);
  }

  checkPoints() {
    if (
      this.p5.dist(
        this.p5.mouseX,
        this.p5.mouseY,
        this.ref.x + 300,
        this.ref.y + 250
      ) < 26
    ) {
      this.activePoint = this.ref.x;
      this.pointActive = true;

      if (this.initVariables.mP) {
        this.ref.x = this.p5.mouseX - 300;
        this.ref.y = this.p5.mouseY - 250;
      }
    } else {
      this.pointActive = false;
    }

    if (this.initVariables.mP && this.onChangeDraw && this.pointActive) {
      this.onChangeDraw();
    }
  }

  drawMeasurement() {
    if (this.pointActive) {
      this.p5.noStroke();
      this.p5.fill(0, 20);
      this.p5.circle(this.ref.x, this.ref.y, 13);
    }
    this.p5.stroke(255, 0, 0);
    this.p5.fill(255, 0, 0);
    this.p5.strokeWeight(2);
    this.p5.circle(this.ref.x, this.ref.y, 5);
    this.p5.noFill();
  }
}

and some variables and function:

const mM = [];
let dataDimensions = [];
let setDataDimensions;

let initVariables = {
  mP: false,
};

const onChangeDraw = () => {
  setDataDimensions([...dataDimensions]);
};

and I have this sketch function I am using:

const sketch = (p5, measurements, setMeasurements) => {
  p5.setup = () => {
        measurements.map((measurement) => {
      const name = new Measurement(
        p5,
        initVariables,
        onChangeDraw,
        measurement.ref
      );
      mM.push(name);
    });
    dataDimensions = measurements;
    setDataDimensions = setMeasurements;
    p5.createCanvas(600, 500, p5.WEBGL);
  };

  p5.draw = () => {
    p5.background(237, 239, 237);
    p5.stroke(0);
    p5.strokeWeight(2);
    p5.circle();
    p5.beginShape();

    for (let m of mM) {
      m.drawMeasurement();
      m.checkPoints();
    }

    mM.map((measurement) => p5.vertex(measurement.ref.x, measurement.ref.y));
    p5.endShape(p5.CLOSE);
  };

  p5.mousePressed = () => {
    initVariables.mP = true;
  };
  p5.mouseReleased = async () => {
    initVariables.mP = false;
  };
};

problem seems to be once I click and drag any point where state changes because of onChangeDraw where I set the measurements into new measurements I get from p5 side.

So, also including measurements for reference:

const [measurements, setMeasurements] = useState([
    {
      id: 1,
      ref: {
        x: 100,
        y: 100,
      },
    },
    {
      id: 2,
      ref: {
        x: 200,
        y: 100,
      },
    },
    {
      id: 3,
      ref: {
        x: 200,
        y: 200,
      },
    },
    {
      id: 4,
      ref: {
        x: 100,
        y: 200,
      },
    },
  ]);

and wrapper:

<div className="App">
      <ReactP5Wrapper
        sketch={(p5) => sketch(p5, measurements, setMeasurements)}
      />
    </div>
@Phenelo
Copy link

Phenelo commented Jun 27, 2024

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>
  )
}

@jamesrweb
Copy link
Collaborator

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.

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