Quantcast
Channel: beginners - openFrameworks
Viewing all articles
Browse latest Browse all 4929

Basic Alpha Fade

$
0
0

@algeo wrote:

Hi All

I’m sure that it is quite simple, but where might I start with simple alpha fades? I’ve included some simple Processing code. How might I recreate this in OF, or is the approach different?

Any help or advice would be much appreciated.

Square square1;

void setup() {
  size(400, 400);
  noStroke();
  
  square1 = new Square(100, 100, 200, 200, 0.5);
}

void draw() {
  background(0);
  square1.fade();
  square1.display();
}


class Square {
  float x, y;
  float squareWidth;
  float squareHeight;
  float alpha = 0;
  float alphaSpeed;
  
  Square(float xpos, float ypos, float sWidth, float sHeight, float gSpeed) {
    x = xpos;
    y = ypos;
    squareWidth = sWidth;
    squareHeight = sHeight;
    alphaSpeed = gSpeed;
  }
  
  void fade() {
    alpha = alpha + alphaSpeed;
    if (alpha >= 127 || alpha <= 0) {
      alphaSpeed = alphaSpeed * -1;
    }
  }
  
  void display() {
    fill(255, alpha);
    rect(x, y, squareWidth, squareHeight);
  }
}

Posts: 4

Participants: 3

Read full topic


Viewing all articles
Browse latest Browse all 4929

Trending Articles