Preloading (p5.js) • tim rodenbröker creative coding

Preloading (p5.js)

Courses / Images and Pixels / Preloading (p5.js)

In p5.js there is a third function besides setup() and draw() that becomes important when working with assets like images, fonts or other data. It is called preload().
The preload() function is invoked before setup() and ensures that the required data is available before the sketch starts to play.

function preload(){
…
}
function setup(){
…
}
function draw(){
…
}

All functions that load something should be called within preload()

let data; // this could be a text file, an image, a font, etc.

function preload(){
   data = loadImage('assets/filename.jpg')
   //data = loadFont('assets/filename.ttf');
   //data = loadStrings(('assets/filename.txt'))
   //data = loadModel('assets/filename.obj');
   //data = loadSound('assets/filename.mp3');
}

Language Comparison

Processingp5.js
preload()
Published by Tim on Monday November 14, 2022

Last modified on January 17th, 2023 at 10:22

  1. Intro
  2. Preloading (p5.js)
  3. Asset Management
  4. PImage
  5. Displaying an image
  6. Resizing an image
  7. Scaling
  8. Image Filters
  9. Masking
  10. Picking colors
  11. Pixelation
  12. Brightness
  13. Threshold
  14. Rasterization
  15. Rasterization in 3D / Extrusion
  16. Rasterization combined with Wave-Motion
  17. Wrapping Up