preload()
preload() runs before setup() and before the first draw() frame. It is the place to load files (shaders, fonts, images) that must be ready before your sketch begins. The shader files you load here are the GPU programs that create the liquid-chrome visual effect—they run on every pixel of the canvas in parallel.
function preload() {
// Load the custom shaders
// Ensure 'vertex.glsl' and 'fragment.glsl' files are present and contain GLSL code
myShader = loadShader('vertex.glsl', 'fragment.glsl');
// Load a font for text in WEBGL mode
// Using Fontsource CDN for reliable WEBGL-compatible fonts
myFont = loadFont('https://unpkg.com/@fontsource/roboto@latest/files/roboto-latin-400-normal.woff');
// Initialize velocity variables for the first frame
prevMouseX = mouseX;
prevMouseY = mouseY;
velocityX = 0;
velocityY = 0;
velocityMagnitude = 0;
}
Line-by-line explanation (4 lines)
🔧 Subcomponents:
myShader = loadShader('vertex.glsl', 'fragment.glsl');
Loads the vertex and fragment shader files from disk and compiles them for GPU execution
myFont = loadFont('https://unpkg.com/@fontsource/roboto@latest/files/roboto-latin-400-normal.woff');
Fetches a WEBGL-compatible font from a CDN so text renders crisp in GPU mode
prevMouseX = mouseX;
prevMouseY = mouseY;
velocityX = 0;
velocityY = 0;
velocityMagnitude = 0;
Sets up mouse tracking variables so the first frame's velocity calculation has a valid starting point
myShader = loadShader('vertex.glsl', 'fragment.glsl');- Loads two GLSL files from disk (vertex.glsl and fragment.glsl), compiles them, and stores the compiled shader program in myShader so it can be used later in draw()
myFont = loadFont('https://unpkg.com/@fontsource/roboto@latest/files/roboto-latin-400-normal.woff');- Fetches a web-safe font from a CDN that works reliably in WEBGL mode, storing it in myFont for later text rendering
prevMouseX = mouseX;- Stores the current mouse X position so that in the first frame of draw(), we can calculate how far the mouse has moved since preload ran
velocityX = 0;- Sets horizontal velocity to zero before draw() starts, so the sketch doesn't interpret the first mouse movement as infinite velocity