Images, Text, and Sound
This section will demonstrate how to display images, render text, and play sounds.
Table of Contents
- Persistent Variables
- Loading Assets
- Adding Images to a Sketch
- Loading Images
- Processing Image Pixels
- Simple Text
- Text and Fonts
- p5.js Sounds
- Loading and Playing a Sound
Persistent Variables
The variables weβve dealt with so far have been function parameters and local variables. Variables created inside a function can only be accessed within that function, and local variables in draw() are recreated each frame.
To preserve state across frames, or to share a value between functions, we can define global variables outside of the setup() and draw() functions.
The next sections give us the opportunity to work with global variables defined in this manner.
β‘ Warning:
Global variables can be a source of hard to find bugs. Use them sparingly.
Loading Assets
Images, fonts, sounds, and other external files take time to load.
In p5.js assets are loaded using await inside an async setup() function. The sketch waits at the await statement until the asset has finished loading.
For example:
let ramImage;
async function setup() {
createCanvas(300, 300);
ramImage = await loadImage("assets/goat.png");
}
π΅ Note:
Older p5.js v1 sketches often use a special preload() function instead.
Adding Images to a Sketch
Images should be placed into an assets folder within your p5.js project.
If you are using the p5.js web editor you will need to expand the βSketch Filesβ area (See #8 of the p5.js Tour) and then upload the image:

Loading Images
With p5.js we can load, display, resize, and manipulate images in PNG, JPG, or GIF format.
First we define a global variable at the top of the file:
let ramImage;
Then we load the image using await inside an async setup() function:
async function setup() {
createCanvas(300, 300);
ramImage = await loadImage("assets/goat.png");
// Scale the image by one third:
ramImage.resize(ramImage.width / 3, ramImage.height / 3);
frameRate(1); // One frame per second please.
}
And then draw it from within draw():
function draw() {
background(255); // White background.
let xPos = random(0, width - ramImage.width);
let yPos = random(0, height - ramImage.height);
image(ramImage, xPos, yPos); // Place image randomly within canvas.
}
Edit Code Using p5.js Web Editor
The Result:
Resources
- π
loadImage()- Load an image file. - π
image()- Draw an image to the canvas. - π
p5.ImageClass - π
background()- Can also use ap5.Imageas the canvas background. - π
tint()- Apply color or transparency to an image. - π
p5.Image.mask()- Use another image as an alpha mask.
Processing Image Pixels
The RGBA color value of any image pixel can be retrieved and changed:
let pixelColor = ramImage.get(45, 55); // Get the color at x = 45 and y = 55.
ramImage.set(5, 10, color("red")); // Set the pixel at (5, 10) to red.
ramImage.updatePixels(); // set() must be paired with updatePixels().
Resources
- π
p5.Image.get()- Get an image pixel or region. - π
p5.Image.set()- Set an image pixel or region. - π
p5.Image.pixels- Theget()andset()operations are slow, so we can request access to the raw pixel array.
Simple Text
We can draw text to the screen with a default font using:
textSize(30); // Set the text size.
text("Hello Whirled", 100, 200); // Write text at x = 100, y = 200.
fill(0, 102, 153); // Text uses the current fill color.
text("Hello Whirled", 100, 240); // Write more text.
Resources
- π
text()- Draw text to the canvas. - π
textSize()and πtextAlign()- Change text size and alignment.
Text and Fonts
p5.js includes default fonts, but we can also load a custom TrueType (.ttf) or OpenType (.otf) font.
Grab a font from your c:\windows\fonts\ folder or a free font source like fontlibrary.org and put it in the assets folder in your project.
For the sake of example, letβs say you grabbed lemon.ttf.
let lemon;
async function setup() {
createCanvas(200, 200);
lemon = await loadFont("assets/lemon.ttf");
textFont(lemon); // Use our loaded font.
textSize(width / 8); // Set the font size.
textAlign(CENTER, CENTER); // Center horizontally and vertically.
fill(255); // Draw the text in white.
}
function draw() {
background(0); // Clear the background in black.
translate(width / 2, height / 2); // Translate to the middle of the canvas.
rotate(frameCount / 100); // Rotate based on the frame count.
text("upsidedown", 0, 0); // Display our text string.
}
Edit Code Using p5.js Web Editor
The Result:
Resources
- π
loadFont()and πtextFont()- Load and set a font. - π
textWidth()- Measure the tight visual width of text. In p5.js v2, leading and trailing spaces are ignored. - π
fontWidth()- Measure text using the fontβs normal spacing.
p5.js Sounds
p5.js sketches can optionally support the loading and playing of sound files using the separate p5.sound library.
If you are developing locally, make sure your index.html includes a current version of p5.sound in addition to p5.js. Older versions of p5.sound bundled with p5.js v1 are not compatible with p5.js v2.
See the p5.js Download page for the current p5.sound download and CDN information.
A typical setup using the CDN looks like this:
<script src="https://cdn.jsdelivr.net/npm/p5@2/lib/p5.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/p5.sound@0.4.1/dist/p5.sound.min.js"></script>
Different browsers support different audio formats. If you want to provide the same sound in multiple formats, loadSound() can accept an array of files:
kaChing = await loadSound([
"assets/ka-ching.mp3",
"assets/ka-ching.ogg"
]);
Loading and Playing a Sound
Like images and fonts, sounds are loaded using await inside an async setup() function.
Thereβs so much you can do with sounds in p5.js, but here weβll simply show how to load and play an MP3 file in the assets folder:
let kaChing;
async function setup() {
createCanvas(200, 200);
kaChing = await loadSound("assets/ka-ching.mp3");
}
function draw() {
if (kaChing.isPlaying()) {
background(0, 255, 0); // Green while sound is playing.
} else {
background(255, 0, 0); // Red while sound is not playing.
}
}
function mousePressed() {
kaChing.play(); // Play sound on mouse click.
}
Edit Code Using p5.js Web Editor
The Result:
Resources
- π
loadSound() - π
p5.SoundFile - π
play() - π
pause() - π
stop() - π
loop() - π The Full
p5.soundAPI - π·οΈ Playback Rate Example
- π·οΈ Frequency Analysis Example
- π·οΈ Sound Generation with Oscillator Example