본문 바로가기
데모

좋아하는 호랑이형님 픽셀레이팅

by hansoo.labs 한수댁 2019. 12. 16.

 


var face;
var cells = [];
var prevX = 0;
var prevY = 0;

function setup() {
  
  face = loadImage("barkhan.png");
  
  createCanvas(640, 720);
  frameRate(20);
  colorMode(RGB, 255);
}

function draw() {

  var x;
  var y;
  const boxWidth = 10;
  const acptW = boxWidth + 2;
  const acptH = boxWidth + 2;

  if (cells.length < 100) {
    for (y = 0; y < face.height; y = y + acptW) {
      for (x = 0; x < face.width; x = x + acptH) {
        var colorOf = face.get(x, y);
        cells.push({
          x: x,
          y: y,
          c: colorOf,
          w: boxWidth,
          vw: 0,
          speed: 0.11,
          bounce: 0.88
        });
      }
    }
    print(cells.length);
  }

  const lanton = 150.0;
  background(20);
  
  var wasMoved = (abs(prevX - mouseX) > 2 || abs(prevY - mouseY) > 2)
  var cellsLength = cells.length;
  for (var i = 0; i < cellsLength; i++) {
    var cell = cells[i];
    var dis = sqrt(pow(mouseX - cell.x, 2) + pow(mouseY - cell.y, 2));
    var w = cell.w;
    var vw = cell.vw;
    vw = (vw + (boxWidth - w) * cell.speed) * cell.bounce;
    
    if (dis < lanton && wasMoved) {
      w = boxWidth * dis / lanton;
    } else {
      w = w + vw;  
    }
    cell.w = w;
    cell.vw = vw;
    
    fill(cell.c);
    rect(cell.x, cell.y, w, boxWidth);
  }
  prevX = mouseX;
  prevY = mouseY;
}

댓글0