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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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);
                

chevron_right


Output:

Example 2: This examples uses arrayCopy() function to copy source array elements to destination array.

filter_none

edit
close

play_arrow

link
brightness_4
code

function setup() { 
   
    // Creating Canvas of given size
    createCanvas(500, 90); 
   
function draw() { 
       
    // Set the background color 
    background(220); 
       
    // Initializing the source array
    let src = ['geeks', '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 destnation array
    text("New destination array is : " + dst, 50, 30);

chevron_right


Output:

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



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.