p5.js loadImage() Function Last Updated : 12 Jul, 2025 Comments Improve Suggest changes Like Article Like Report The loadImage() function is used to load an image from the given path and create a p5.Image object with the image. It is advised to load an image in the preload() function as the loaded image may not be available for use immediately. The image is preferably loaded from a relative path as some browsers may prevent loading from other remote locations due to the browser's security features. Syntax: loadImage(path, successCallback, failureCallback) Parameters: This function accepts three parameter as mentioned above and described below. path: This is the path from where the image is to be loaded. successCallback: This is a function which is called if the image successfully loads and it is an optional parameter. failureCallback: This is a function which is called if the image does not load due to any error and it is an optional parameter. Below examples illustrates the loadImage() function in p5.js: Example 1: This example shows the loading of image in preload(). javascript let img; function preload() { img = loadImage('sample-image.png'); } function setup() { createCanvas(300, 200); text("The image would be loaded below:", 20, 20); image(img, 20, 40, 100, 100); } Output: Example 2: This example shows loading an image from a URL and both callbacks that may take place. javascript let img; let url = 'https://media.geeksforgeeks.org/wp-content/uploads/20190314004249/sample-image.png'; function setup() { createCanvas(400, 200); textSize(18) text("The image would be loaded below:", 20, 20); loadImage(url, img => { image(img, 20, 40, 100, 100); }, (event) => { fill("red") text("Error: The image could not be loaded.", 20, 40); console.log(event); } ); } Output: Image successfully loads Image does not load Online editor: https://editor.p5js.org/ Environment Setup: https://www.geeksforgeeks.org/javascript/p5-js-soundfile-object-installation-and-methods/ Reference: https://p5js.org/reference/#/p5/loadImage Comment More infoAdvertise with us S sayantanm19 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-p5.js Explore JavaScript Tutorial 8 min read JavaScript BasicsIntroduction to JavaScript 4 min read JavaScript Versions 2 min read How to Add JavaScript in HTML Document? 3 min read JavaScript Syntax 6 min read JavaScript Output 4 min read JavaScript Comments 2 min read JS Variables & DatatypesVariables and Datatypes in JavaScript 6 min read Global and Local variables in JavaScript 4 min read JavaScript Let 6 min read JavaScript const 5 min read JavaScript Var Statement 7 min read JS OperatorsJavaScript Operators 5 min read Operator precedence in JavaScript 2 min read JavaScript Arithmetic Operators 5 min read JavaScript Assignment Operators 5 min read JavaScript Comparison Operators 5 min read JavaScript Logical Operators 5 min read JavaScript Bitwise Operators 5 min read JavaScript Ternary Operator 4 min read JavaScript Comma Operator 2 min read JavaScript Unary Operators 4 min read JavaScript in and instanceof operators 3 min read JavaScript String Operators 3 min read JS StatementsJavaScript Statements 4 min read JavaScript if-else 3 min read JavaScript switch Statement 4 min read JavaScript Break Statement 2 min read JavaScript Continue Statement 1 min read JavaScript Return Statement 4 min read JS LoopsJavaScript Loops 3 min read JavaScript For Loop 4 min read JavaScript While Loop 3 min read JavaScript For In Loop 3 min read JavaScript for...of Loop 3 min read JavaScript do...while Loop 4 min read JS Perfomance & DebuggingJavaScript | Performance 4 min read Debugging in JavaScript 4 min read JavaScript Errors Throw and Try to Catch 2 min read JS ObjectObjects in Javascript 4 min read Object Oriented Programming in JavaScript 3 min read JavaScript Objects 6 min read Creating objects in JavaScript 5 min read JavaScript JSON Objects 3 min read JavaScript Object Reference 4 min read JS FunctionFunctions in JavaScript 4 min read How to write a function in JavaScript ? 4 min read JavaScript Function Call 2 min read Different ways of writing functions in JavaScript 3 min read Difference between Methods and Functions in JavaScript 3 min read Explain the Different Function States in JavaScript 3 min read JavaScript Function Complete Reference 3 min read JS ArrayJavaScript Arrays 7 min read JavaScript Array Methods 7 min read Best-Known JavaScript Array Methods 6 min read Important Array Methods of JavaScript 7 min read JavaScript Array Reference 4 min read Like