cart253-2018

Core / CART 253 / Fall 2018 / Pippin Barr

Variables and scope


In this module


Where to declare variables?


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

At the top


Inside a function


Declared at the top, assigned later

???


Declaring variables without 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);
}

Fin.