p5.js | arrayCopy()函数
p5.js 中的 arrayCopy()函数用于将源数组元素的一部分复制到另一个目标数组。此函数在未来版本中已被贬低。
句法:
arrayCopy( src, src_pos, dst, dst_pos, length )
参数:该函数接受上面提到的五个参数,如下所述:
- src:这是源数组,其元素要复制到另一个数组中。
- src_pos:这是复制元素的源数组元素的位置。
- dst:这是要粘贴复制的源数组元素的目标数组。
- dst_pos:这是要粘贴源数组元素的目标数组的位置。
- 长度:这是要从源数组复制的元素数。
返回值:它返回一个新复制的目标数组。
下面的程序说明了 p5.js 中的 arrayCopy()函数:
示例 1:此示例使用 arrayCopy()函数将源数组元素复制到目标数组。
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);
}
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 = ['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 destination array
text("New destination array is : " + dst, 50, 30);
}
输出:
示例 2:此示例使用 arrayCopy()函数将源数组元素复制到目标数组。
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 = ['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 destination array
text("New destination array is : " + dst, 50, 30);
}
输出:
参考: https://p5js.org/reference/#/p5/arrayCopy