p5.js | drop() Function Last Updated : 22 Jul, 2024 Comments Improve Suggest changes Like Article Like Report The drop() function is an inbuilt function which is used to register a callback function that gets called every time when a file has been dropped on the element after loading it. Every dropped file is loaded into memory and pass it as p5.File object to the callback function. When multiple files drop at the same time then it will display multiple calls to the callback function.This function requires p5.dom library. So add the following line in the head section of the index.html file. javascript <script language="javascript" type="text/javascript" src="path/to/p5.dom.js"> </script> Syntax:drop( callback, fxn )Parameters: This function accepts two parameters as mentioned above and described below: callback: This parameter is used to hold the loaded file which is called for each file drop.fxn: This parameter is used when callback function is triggered when files are dropped with the drop event.Below examples illustrate the drop() function in p5.js: Example 1: javascript function setup() { // Create Canvas of given size var cvs = createCanvas(400, 300); // Set the background color background('red'); // Set the text position textAlign(CENTER); // Set the font size textSize(24); // Set the text color fill('white'); // Display the text on the screen text('Drop file from device', width / 2, height / 2); // Function to drop the file cvs.drop(gotFile); } function gotFile(file) { // Set the background color background('green'); // Display the text on the screen text('Received file name with extension:', width / 2, height / 2); // Drop file with given position text(file.name, width / 2, height / 2 + 50); } Output:Before Drop a File: After Drop a File: Example 2: javascript var img; function setup() { // Create Canvas of given size var cvs = createCanvas(600, 400); // Set the background color background('red'); // Set the text position textAlign(CENTER); // Set the font size textSize(24); // Set the text color fill('white'); // Display the text on the screen text('Drop file from device', width / 2, height / 2); // Function to drop the file cvs.drop(gotFile); } function draw() { if (img) { image(img, 0, 0, width, height); } } function gotFile(file) { img = createImg(file.data).hide(); } Output:Before Drop a File: After Drop a File: Create Quiz Comment J jit_t Follow 0 Improve J jit_t Follow 0 Improve Article Tags : JavaScript Web Technologies JavaScript-p5.js Explore JavaScript BasicsIntroduction to JavaScript4 min readVariables and Datatypes in JavaScript6 min readJavaScript Operators5 min readControl Statements in JavaScript4 min readArray & StringJavaScript Arrays7 min readJavaScript Array Methods7 min readJavaScript Strings5 min readJavaScript String Methods9 min readFunction & ObjectFunctions in JavaScript5 min readJavaScript Function Expression3 min readFunction Overloading in JavaScript4 min readObjects in JavaScript4 min readJavaScript Object Constructors4 min readOOPObject Oriented Programming in JavaScript3 min readClasses and Objects in JavaScript4 min readWhat Are Access Modifiers In JavaScript ?5 min readJavaScript Constructor Method7 min readAsynchronous JavaScriptAsynchronous JavaScript2 min readJavaScript Callbacks4 min readJavaScript Promise4 min readEvent Loop in JavaScript4 min readAsync and Await in JavaScript2 min readException HandlingJavascript Error and Exceptional Handling6 min readJavaScript Errors Throw and Try to Catch2 min readHow to create custom errors in JavaScript ?2 min readJavaScript TypeError - Invalid Array.prototype.sort argument1 min readDOMHTML DOM (Document Object Model)8 min readHow to select DOM Elements in JavaScript ?3 min readJavaScript Custom Events4 min readJavaScript addEventListener() with Examples9 min readAdvanced TopicsClosure in JavaScript4 min readJavaScript Hoisting6 min readScope of Variables in JavaScript3 min readJavaScript Higher Order Functions7 min readDebugging in JavaScript4 min read Like