Week 8: Tut 15, 15.1

by Sarah Shultz

Tut 15: Interaction 1 - Mouse

  1. Mouse Position: angled sqaures!
  • Show Code
/** @peep sketch */
 
color lightBlue = color(random(0,255), random(0,255), random(0,255));
color darkBlue = color(random(0,255), random(0,255), random(0,255));
color orange = color(random(0,255), random(0,255), random(0,255));
 
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);
      scale(factor);
      rotate(PI/3.0);
      quad(0, 20, 0, 10, 10, 10, 10, 20);
 
      popMatrix();
    }
  }
}
  1. Mouse Movement:
  • Show Code
/** @peep sketch */
 
color randomColour = color(random(0,255), random(0,255), random(0,255),random(100,255));
 
float radius = random(5,10);
 
void setup() {
  size(200, 200);
  background(255-16);
  stroke(randomColour);
}
 
void draw() {
  float d = dist(pmouseX, pmouseY, mouseX, mouseY);
  if (d <= 40) {
    strokeWeight(constrain(10-d, 2, 10));
    line(pmouseX, pmouseY, mouseX, mouseY);
    ellipse(pmouseX, pmouseY, radius, radius);
  }
}
  • Show Code
/** @peep sketch */
void setup() {
  size(255, 255);
  background(255-16);
  stroke(0);
}
 
void draw() {
  float d = dist(pmouseX, pmouseY, mouseX, mouseY);
  if (d <= 40) {
    float r = (mouseX + mouseY)*4;
    float g = max(mouseX, mouseY)/2;
    float b = 255 - max(mouseX, mouseY)+10;
    float s = constrain(20-d, 2, 10);
    stroke(r, g, b, 255-16);
    strokeWeight(s);
    line(pmouseX, pmouseY, mouseX, mouseY);
  }
}
  1. Mouse Buttons
  • Show Code
/** @peep sketch */
 
color startingCircle = color(random(0, 100), random(50, 80), random (0, 255));
 
color bgColour = color(random(0,255), random(0,255), random(0,255), random(100, 255));
 
 
int radius = 100;
color fillColor = startingCircle;
 
void setup() {
  size(200, 200);
 
}
 
void draw() {
  background(255-16);
  strokeWeight(4);
  float distance = dist(mouseX, mouseY, width/2, height/2);
  if (mousePressed && distance < radius/2) {
    fill(random(0,255), random(0,255), random(0,255), random(100,255));
    println('The button has been pressed.');
    background(bgColour);
 
  } else {
    fill(startingCircle);
 
  }
  ellipse(width/2, height/2, radius, radius);
}

Tutorial 15.1: Keyboard Interactions

  • Show Code
/** @peep sketch */
 
color lightPurple = color(218, 174, 232);
color purple = color(92, 13, 255);
color white = color(255);
color purpleish = color(127,101,135);
void setup() {
  size(200, 200);
  strokeWeight(4);
}
 
void draw() {
  background(204);
  if (keyPressed == true) {
 
    fill(purple);
    stroke(purpleish);
 
  } else {
    fill(lightPurple);
    stroke(white);
  }
  ellipse(width/2, height/2, 80, 80);
  ellipse(width/2, height/2, 40, 40);
 
}
  1. Coded keys
  • Show Code
/** @peep sketch */
float angle = 0;
float x = 0;
float y = 0;
float radius = 20;
 
void setup() {
  size(200, 200);
  strokeWeight(2);
}
 
void draw() {
  background(204);
  if (keyPressed == true) {
    if (key == CODED) {
      if (keyCode == LEFT) {
        angle -= PI/180;
      } else if (keyCode == RIGHT) {
        angle += PI/180;
      } else if (keyCode == UP) {
        x += cos(angle);
        y += sin(angle);
      } else if (keyCode == DOWN) {
        x -= cos(angle);
        y -= sin(angle);
      }
    } 
  }
  x = constrain(x, -(width/2 - radius), (width/2 - radius));
  y = constrain(y, -(height/2 - radius), (height/2 - radius));
  translate(width/2 + x, height/2 + y);
  rotate(angle);
  ellipse(0, 0, radius*2, radius*2);
  line(0, 0, radius, 0);
}

Comments

Nobody has said anything yet.