Week 9: Int­e­r­a­c­t­i­o­n

by Gawshika Aumkaran

Reacting to Mouse Position

  • Show Sketch
/** @peep sketchcode */
color lightBlue = color(66, 168, 237);
color darkBlue = color(0, 102, 153);
color orange = color(204, 102, 0);
 
void setup() {
  size(200, 200);
  noStroke();
}
 
void draw() {
  background(255-16);
  float radius1 = 20 + mouseX / 2;
  float radius2 = 20 + (width - mouseX) / 2;
  fill(orange, 128);
  ellipse(mouseX, height/2, radius1, radius1);
  fill(lightBlue, 128);
  ellipse(mouseX, height/2, radius2, radius2);
 
  float radius3 = 20 + mouseY / 2;
  float radius4 = 20 + (width - mouseY) / 2;
  fill(72, 209, 204, 128);
  ellipse(height/2, mouseY, radius1, radius1);
  fill(171, 130, 255, 128);
  ellipse(height/2, mouseY, radius2, radius2);
}

Comparing the Mouse Position

  • Show Sketch
/** @peep sketchcode */
color lightBlue = color(66, 168, 237);
color darkBlue = color(0, 102, 153);
color orange = color(204, 102, 0);
 
int spacing = 20;
int radius = 8;
 
void setup() {
  size(200, 200);
  noStroke();
}
 
void draw() {
  background(255-16);
  for (int x = 10; x < width; x += spacing) {
    for (int y = 10; y < width; y += spacing) {
      float distance = dist(mouseX, mouseY, x, y);
      if (distance < radius) {
        fill(darkBlue);
      } else {
        fill(lightBlue);
      }
      float factor = map(distance, 0, width, 1.3, 0.3);
      pushMatrix();
      translate(x, y);
      rotate(factor*2);
      scale(factor);
      rect(0, 0, radius*2, radius*2);
      popMatrix();
    }
  }
}

keyboard

  • Show Sketch
/** @peep sketchcode */
 
color mint = color(64, 224, 208);
color cyan = color(0, 139, 139);
color purple = color(171, 130, 255);
 
void setup() {
  size(200, 200);
  strokeWeight(4);
}
 
void draw() {
  background(204);
  if (keyPressed == true) {
    fill(purple);
  } else {
    fill(mint);
  }
  ellipse(width/2, height/2, 80, 80);
}

The value of a key

  • Show Sketch
/** @peep sketchcode */
float angle = 30;
color cyan = color(0, 139, 139);
color purple = color(171, 130, 255);
 
void setup() {
  size(200, 200);
  rectMode(CENTER);
  strokeWeight(4);
}
 
void draw() {
  background(255);
  if (keyPressed == true) {
    angle += PI/90;
  }
    translate(width/2, height/2);
    if (keyPressed == true) {
    fill(purple);
  } else {
    fill(cyan);
  }
  rotate(angle);
  ellipse(30,30,40,40);
}

Comments

Nobody has said anything yet.