The arrayCopy() function in p5.js is used to copy a part of source array elements to another destination array. This function is depreciated from future versions.
Syntax:
arrayCopy( src, src_pos, dst, dst_pos, length )
Parameters: This function accepts five parameters as mentioned above and described below:
- src: This is the source array whose elements to be copied into another array.
- src_pos: This is the position of source array elements from where the element is copied.
- dst: This is the destination array where copied source array element is to be pasted.
- dst_pos: This is the position of destination array where source array element is to be pasted.
- length: This is the number of elements to be copied from source array.
Return Value: It returns a new copied destination array.
Below programs illustrate the arrayCopy() function in p5.js:
Example 1: This examples uses arrayCopy() function to copy source array elements to destination array.
javascript
function setup() { // Creating Canvas of given size createCanvas(500, 90); } function draw() { // Set the background color background(220); // Initializing the source array let src = [ 'IT' , 'CSE' , 'ECE' ]; // Initializing the source array position // from where the elements are to be copied. let src_pos = 1; // Initializing the destination array let dst = [ 'Ram' , 'Shyam' , 'Geeta' ]; // Initializing the destination position // where copied elements are to be pasted. let dst_pos = 0; // Initializing the number of source array elements // which are to be copied. let length = 2; // Calling to arrayCopy() function. arrayCopy(src, src_pos, dst, dst_pos, length); // Set the size of text textSize(16); // Set the text color fill(color( 'red' )); // Getting new destination array text( "New destination array is : " + dst, 50, 30); } |
Output:
Example 2: This examples uses arrayCopy() function to copy source array elements to destination array.
javascript
function setup() { // Creating Canvas of given size createCanvas(500, 90); } function draw() { // Set the background color background(220); // Initializing the source array let src = [ 'neveropen' , 'Students' , 'Teachers' ]; // Initializing the source array position // from where the elements are to be copied. let src_pos = 2; // Initializing the destination array let dst = [ 'A' , 'B' , 'C' ]; // Initializing the destination position // where copied elements are to be pasted. let dst_pos = 1; // Initializing the number of source array elements // which are to be copied. let length = 1; // Calling to arrayCopy() function arrayCopy(src, src_pos, dst, dst_pos, length); // Set the size of text textSize(16); // Set the text color fill(color( 'red' )); // Getting new destination array text( "New destination array is : " + dst, 50, 30); } |
Output:
Reference: https://p5js.org/reference/#/p5/arrayCopy