Bubble brush

edited September 2018 in Share Your Work

I was working on a drawing canvas, and messed up the coloring equation. Funnily, this created something very interesting. Scroll to change the brush size, spacebar to erase the canvas.

int gridSize = 250;
float cellSize, brushSize = 200;
ArrayList<Cell> cells;

class Cell {
  float x, y, s, a = 0;
  Cell(int i, int j) {
    x = cellSize*i+cellSize/2;
    y = cellSize*j+cellSize/2;
    s = cellSize;
  }
  void display() {
    noStroke();
    fill(255*a);
    rect(x, y, s+1, s+1);
  }
}

void setup() {
  size(800, 800);
  rectMode(CENTER);
  ellipseMode(CENTER);
  cellSize = width/float(gridSize);
  cells = new ArrayList<Cell>();
  for (int i = 0; i < gridSize; i++) {
    for (int j = 0; j < gridSize; j++) {
      cells.add(new Cell(i, j));
    }
  }
}

void draw() {
  if (mousePressed) {
    for (Cell c : cells) {
      if (dist(c.x, c.y, mouseX, mouseY) <= brushSize/2 && 1-dist(c.x, c.y, mouseX, mouseY)/(brushSize/2) >= c.a) {
        c.a = 1-dist(c.x, c.y, mouseX, mouseY)/(brushSize/2);
      }
    }
  }
  background(0);
  for (Cell c : cells) {
    c.display();
  }
  noFill();
  stroke(255);
  ellipse(mouseX, mouseY, brushSize, brushSize);
}

void mouseWheel(MouseEvent e) {
  if (e.getCount() > 0) {
    brushSize *= 1.25;
  } else {
    brushSize /= 1.25;
  }
}

void keyPressed() {
  if (key == ' ') {
    for (Cell c : cells) {
      c.a = 0;
    }
  }
}
Tagged:

Comments

Sign In or Register to comment.