TL;DR:
I’m using p5.js with TypeScript + Vite.
When I try to use preload()
, I get runtime errors like:
Uncaught FESError: Stopping sketch to prevent more errors
Uncaught TypeError: Cannot read properties of undefined (reading 'pixels')
Without defining preload
, this error does not occur.
Is this error expected or a known compatibility issue when using preload()
in a TypeScript + Vite environment?
You can reproduce the issue using my minimal template repository:
GitHub - tomokikun/p5js-typescript-vite-template at f0329f9b22b82394397e66ae5d39424bbaefc9a8
Example Code
import p5 from 'p5';
const preload = (_p: p5) => {
};
const setup = (p: p5) => {
p.createCanvas(800, 600);
p.background(0);
p.ellipse(p.width / 2, p.height / 2, 100, 100);
};
const draw = (_p: p5) => {};
new p5((p: p5) => {
p.preload = () => preload(p);
p.setup = () => setup(p);
p.draw = () => draw(p);
});
What I’ve Tried
- Made sure the image path is correct (relative path, file exists).
- Verified that
loadImage()
is only called insidepreload()
. - Removed
img.pixels
access (I’m not calling it myself). - Tested with multiple browsers (Chrome and Safari) and clean builds.
- Tried to find related issues or topics but I couldn’t.
Questions
- Is this error expected or a known compatibility issue when using
preload()
in a TypeScript + Vite environment?
I’ve tried reading the code at p5.js/src/core/main.js at main · processing/p5.js · GitHub to understand what’s going on, but I’m still confused. Could someone please help explain it?
Any ideas or suggestions would be greatly appreciated. Thanks!