I use p5.js but with typescript.
The structure of a p5 project:
let sketch = (p: p5) => {
p.setup = () => {setup(p)
};
p.draw = () => {draw(p)
};
};
new p5(sketch);
After reading this I defined my function setup and draw like this:
let slider: p5.Element;
function setup(p: p5): void {
p.createCanvas(400, 300);
slider = p.createSlider(0, 50, 50 / 2);
}
function draw(p: p5): void {
p.background(50);
p.fill(255);
p.noStroke();
p.ellipse(slider.value(), 50 / 2, 50, 50);
}
the code works but vs code underline slider.value() in red. This is normal because inside the the function draw we don’t know if slider is instantiated or not.
I would like to know if there a better way to writ this code. A way that doesn’t make VS code print this error.