Animation 3

Beispiel aus dem Kapitel Processing in JavaScript.

Drücken Sie eine beliebige Taste, um den Ball neu zu starten.

Code

var x = 100;
var y = 100;
var xs;
var ys;

function setup() {
  createCanvas(200, 200);
  xs = random(-3, 3);
  ys = random(-3, 3);
}

function draw() {
  background(0);
  ellipse(x, height/2, 20, 20);
  x = x + xs;
  y = y + ys;
  if (x > width-10 || x < 10) {
    xs = -xs;
  }
  if (y > width-10 || y < 10) {
    ys = -ys;
  }
}

function keyPressed() {
  x = width/2;
  y = height/2;
  xs = random(-3, 3);
  ys = random(-3, 3);
}