p5.js arrayCopy() function
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() {
createCanvas(500, 90);
}
function draw() {
background(220);
let src = [ 'IT' , 'CSE' , 'ECE' ];
let src_pos = 1;
let dst = [ 'Ram' , 'Shyam' , 'Geeta' ];
let dst_pos = 0;
let length = 2;
arrayCopy(src, src_pos, dst, dst_pos, length);
textSize(16);
fill(color( 'red' ));
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() {
createCanvas(500, 90);
}
function draw() {
background(220);
let src = [ 'geeks' , 'Students' , 'Teachers' ];
let src_pos = 2;
let dst = [ 'A' , 'B' , 'C' ];
let dst_pos = 1;
let length = 1;
arrayCopy(src, src_pos, dst, dst_pos, length);
textSize(16);
fill(color( 'red' ));
text( "New destination array is : " + dst, 50, 30);
}
|
Output:
Reference: https://p5js.org/reference/#/p5/arrayCopy
Last Updated :
22 Aug, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...