Open In App

p5.js loadStrings() Function

Last Updated : 22 Aug, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The loadStrings() function is used to read the contents of a file and create an array of strings with each of the lines of the file. The file to be read must be located at the sketch directory if the file name is used, otherwise, a URL to the file can be specified.

This function is recommended to be called in the preload() function to ensure that the function is executed before the other functions.

Syntax:

loadStrings( filename, callback, errorCallback )

Parameters: This function accept three parameter as mentioned above and described below:

  • filename: This is a string which denotes the name of the file or the url to load the file from.
  • callback: This is a function which is called after the function is successfully executed. The first argument for this function is the strings array.
  • errorCallback: This is a function which is called if there is any error in executing the function. The first argument for this function is the error response.

Below examples illustrate the loadStrings() function in p5.js:

Example 1:




let result;
   
function preload() {
    result = loadStrings("test_file.txt");
}
   
function setup() {
    createCanvas(600, 300);
    textSize(22);
}
   
function draw() {
    clear();
    text("The contents of the file "
        + "are shown below:", 20, 20);
   
    // Check if the strings array
    // is non-empty before displaying
    // the contents
    if (result.length > 0) {
        for (let i = 0; i < result.length; i++) {
            text(result[i], 20, 60 + i * 20);
        }
    }
    else {
        text("File is empty", 20, 60);
    }
}


Output:
loadString-preload

Example 2:




let result;
   
function setup() {
    createCanvas(600, 300);
    textSize(22);
   
    text("The file would be loaded"
            + " below...", 20, 20);
   
    result = loadStrings(
            "test_file.txt", fileLoaded);
}
   
function fileLoaded() {
    text("The contents of the file "
        + "are shown below:", 20, 60);
   
    // Check if the strings array
    // is non-empty before 
    // displaying the contents
    if (result.length > 0) {
        for (let i = 0; i < result.length; i++) {
            text(result[i], 20, 100 + i * 20);
        }
    }
    else {
        text("File is empty", 20, 60);
    }
}


Output:
loadStrings-callback

Online editor: https://editor.p5js.org/

Environment Setup: https://www.geeksforgeeks.org/p5-js-soundfile-object-installation-and-methods/

Reference: https://p5js.org/reference/#/p5/loadStrings



Previous Article
Next Article

Similar Reads

How to get the function name from within that function using JavaScript ?
Given a function and the task is to get the name of the function from inside the function using JavaScript. There are basically two methods to get the function name from within that function in JavaScript. These are: Using String substr() MethodUsing Function prototype name propertyGet the Function name using String substr() MethodThis method gets
2 min read
How to get the function name inside a function in PHP ?
To get the function name inside the PHP function we need to use Magic constants(__FUNCTION__). Magic constants: Magic constants are the predefined constants in PHP which is used on the basis of their use. These constants are starts and end with a double underscore (__). These constants are created by various extensions. Syntax: $string = __FUNCTION
1 min read
How to Check a Function is a Generator Function or not using JavaScript ?
Given an HTML document containing some JavaScript function and the task is to check whether the given function is generator function or not with the help of JavaScript. There are two examples to solve this problem which are discussed below: Example 1: In this example, we will use functionName.constructor.name property. It functionName.constructor.n
2 min read
How to implement a function that enable another function after specified time using JavaScript ?
Let us assume that we have been given a function add() which takes in two parameters a and b, and returns their sum. We are supposed to implement a function that should be able to call any function after the given delay amount of time. This can be done with the approaches as given below: Approach 1: Create a function that takes in the amount of del
2 min read
How to create a function that invokes function with partials appended to the arguments in JavaScript ?
In this article, we will learn to create a function that invokes a function with partials appended to the arguments it receives in JavaScript. Approach: We want to implement a function that invokes another function and provides the arguments it received. We can get the result by using (...) the spread/rest operator. Explanation: Let's say, we creat
3 min read
How to create a function that invokes the provided function with its arguments transformed in JavaScript ?
In programming, functions are used to reduce the effort of writing the same instance of code repeatedly. In this article let us see how we can create a function that invokes the provided function with its arguments transformed in JavaScript. In this article, the function transformer invokes the function scaling with its arguments, where the functio
2 min read
How to create a function that invokes each provided function with the arguments it receives using JavaScript ?
In this article, we will see how to create a function that invokes each provided function with the arguments it receives using JavaScript. It helps in reducing the effort of creating the same instance of code again and again. In this article let us see how to create a function that invokes each provided function with the arguments it receives and r
3 min read
How to create a function that invokes function with partials prepended arguments in JavaScript ?
In this article, we will see how to create a function that invokes functions with partials prepended to the arguments it receives in JavaScript. Before understanding the problem statement and approaching the solution for the same, let's, first of all, know what a function (also called a method) is and how a function gets executed. A function (or a
5 min read
Difference between Function.prototype.apply and Function.prototype.call
JavaScript treats everything as an object, even functions, and every object has its own properties and methods. Function objects have both apply() and call() methods on them. However, there is confusion about the two functions in JavaScript. The main difference between them is how they handle function arguments. There is no difference between these
2 min read
Explain the differences on the usage of foo between function foo() {} and var foo = function() {}
Here we see the differences on the usage of foo between function foo() {} and var foo = function() {} types of function declarations in JavaScript. function foo() {} is a Normal Function or traditional general way of Function Declaration which every user or developer finds it simpler to declare and use it every ever required and var foo = function(
4 min read