49
1
/*
2
Random Functions
3
This example demonstrates how to use an array
4
to call random functions. The array theFunctions will
5
hold the names of two functions which will be called
6
randomly to place objects at random locations on the
7
canvas. We use the animation loop to draw many shapes
8
*/
9
10
function drawCircle(x,y){
11
//Draw a circle with radius 10 at (x,y)
12
fillcolor('red');
13
circle(x,y,10);
14
fill();
15
}
16
function drawSquare(x,y){
17
//Draw a 20x20 square at (x,y)
18
fillcolor('blue');
19
rect(x-10,y-10,20,20);
20
fill();
21
}
22
23
//An array with the function names
24
var theFunctions=[drawCircle, drawSquare];
25
26
function setup(){
27
loop(); //start the animation loop
28
}
29
function draw(){
30
//variables for center of shape
31
var x,y;
32
33
/*
34
A variable for the name of a random function.
35
This is not necessary, but it saves some awkward
36
parentheses.
37
*/
38
var f;
39
40
//get random coordinates
41
x=width*random();
42
y=height*random();
43
44
//call random function
45
f=theFunctions.random();
46
f(x,y);
47
48
}
49