var x;
var y;
var speed;
var circleSize = 50;
function setup() {
createCanvas(500,500);
x = width/2;
y = height/2;
speed = random(1,10);
var circleColor = random(255);
fill(circleColor);
}
function draw() {
x += speed;
ellipse(x,y,circleSize,circleSize);
}
x
, y
, and circleSize
setup()
and draw()
use x
and y
circleSize
up there because it’s something about the circle we want to remember and be able to changecircleColor
is declared inside setup()
circleColor
inside draw()
, for example, it won’t exist!setup()
finishes, circleColor
is gone forevercircleColor
right at the beginning, we don’t need it again in this programx
and y
are declared at the top of the program with no valuesetup()
- why?setup()
that width
and height
actually have values (thanks to createCanvas()
which specifies them)???
speed
- we wait until setup()
to calculate its random valuevar
var
function setup() {
createCanvas(500,500);
x = width/2;
y = height/2;
circleColor = random(255);
fill(circleColor);
}
function draw() {
x = x + 1;
ellipse(x,y,50,50);
}
var
to create a new variable, JavaScript creates it as a global variable (like the ones declared at the top of the script)