๐ŸŽ‡ Tile Grid Animation

BamgasiJMยท2025๋…„ 12์›” 22์ผ

p5.js Art

๋ชฉ๋ก ๋ณด๊ธฐ
40/65
post-thumbnail

๐Ÿ“ p5.js

let tiles = [];
let cols = 25;
let rows = 25;
let t = 0; // ์‹œ๊ฐ„ ๋ณ€์ˆ˜

function setup() {
  createCanvas(1000, 1000);
  frameRate(60);

  const tileWidth = width / cols;
  const tileHeight = height / rows;

  for (let y = 0; y < rows; y++) {
    for (let x = 0; x < cols; x++) {
      const pos = {
        x: x * tileWidth,
        y: y * tileHeight,
        w: tileWidth,
        h: tileHeight,
      };
      tiles.push(new Tile(pos));
    }
  }
}

function draw() {
  background(10);

  for (const tile of tiles) {
    tile.update(t);
    tile.display();
  }

  t += 0.01; // ์ฃผ๊ธฐ ์†๋„
}

function keyPressed() {
  if (key === " ") {
    for (const tile of tiles) {
      tile.randomizeColor();
    }
  }
}

class Tile {
  constructor(position) {
    this.position = position;

    // ํƒ€์ผ๋ณ„ ๋žœ๋ค ์œ„์ƒ ์˜คํ”„์…‹
    this.phase = random(TWO_PI);

    // ์ดˆ๊ธฐ ์ƒ‰์ƒ ๊ณ ์ •
    this.randomizeColor();
  }

  randomizeColor() {
    this.color = {
      r: int(random(20, 100)),
      g: int(random(100, 200)),
      b: int(random(170, 220)),
    };
  }

  /* ์˜ค๋ฅธ์ชฝ์—์„œ ์™ผ์ชฝ์œผ๋กœ ํŒŒ์žฅ ์ „๋‹ฌ */
  // ์ตœ์†Œ ํฌ๊ธฐ๊ฐ€ ํ•„์š”ํ•˜๋ฉด ์ˆซ์ž๋ฅผ ๋”ํ•˜๊ณ , ์ž‘์€ ์ˆ˜๋ฅผ ๊ณฑํ•จ
  // ํฐ ์ˆซ์ž๋กœ ๊ฒน์น˜๋ฉด ๋” ์žฌ๋ฏธ์žˆ๋Š” ํŒจํ„ด ์ƒ์„ฑ๋จ
  // update(time) {
  //   const { x, w } = this.position;
  //   const nx = x / width; // 0~1 ์ •๊ทœํ™”

  //   this.scale = 0.4 + (sin(time + nx * TWO_PI) + 1) * 0.3;
  // }

  /* ์ค‘์•™์—์„œ ๋ฐ”๊นฅ์œผ๋กœ ํŒŒ์žฅ ์ „๋‹ฌ */
  // time ๋’ค ๋ถ€ํ˜ธ๋ฅผ ๋ฐ”๊พธ๋ฉด ๋ฐฉํ–ฅ ๋ฐ˜์ „
  // nd ๋์˜ ์ˆซ์ž๋ฅผ ํ‚ค์šฐ๋ฉด ํŒŒ์žฅ ๊ฐ„๊ฒฉ์ด ์ปค์ง

  // update(time) {
  //   const { x, y, w, h } = this.position;

  //   const cx = x + w * 0.5;
  //   const cy = y + h * 0.5;

  //   const d = dist(cx, cy, width * 0.5, height * 0.5);
  //   const nd = d / (sqrt(width * width + height * height) * 0.7);

  //   this.scale = (sin(time - nd * TWO_PI) + 0.5) * 0.65;
  // }

  /* ๋žœ๋คํ•œ ํŒŒ๋™ */
  update(time) {
    // ์‚ฌ์ธ ๊ณก์„  ๊ธฐ๋ฐ˜ ํฌ๊ธฐ ๋น„์œจ (0~1)
    // ๊ณฑํ•˜๋Š” ์ˆซ์ž๋ฅผ ํ‚ค์šฐ๋ฉด ์‚ฌ์ด์ฆˆ ์ฆ๊ฐ€
    this.scale = (sin(time + this.phase) + 1) * 0.5;
    // ์ตœ์†Œ ํฌ๊ธฐ ๋ณด์žฅ
    //  this.scale = 0.42 + 0.7 * (sin(time + this.phase) + 1) * 0.4;
  }


  display() {
    const { x, y, w, h } = this.position;
    const { r, g, b } = this.color;

    // ์ค‘์•™ ๊ธฐ์ค€ ํฌ๊ธฐ ๋ณ€ํ™”
    const sw = w * this.scale;
    const sh = h * this.scale;

    const cx = x + w * 0.5;
    const cy = y + h * 0.5;

    stroke(10);
    strokeWeight(1);
    fill(r, g, b, 255);
    rectMode(CENTER);
    rect(cx, cy, sw, sh);
    rectMode(CORNER);
  }
}

๐Ÿ“ ๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น

๊ฐ์ฒด ๋‚ด๋ถ€์˜ x, y ๊ฐ’์„ ๊บผ๋‚ด์–ด ์ง€์—ญ ๋ณ€์ˆ˜๋กœ ๋งŒ๋“œ๋Š” ๋ฌธ๋ฒ•

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์—”์ง„์€ ๋‹ค์Œ ๊ณผ์ •์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

  • ์šฐ๋ณ€์˜ ๊ฐ์ฒด(this.position)๋ฅผ ํ‰๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.
  • ์ขŒ๋ณ€์˜ { x, y }๋ฅผ ๋ณด๊ณ , ๋™์ผํ•œ ์ด๋ฆ„์˜ ํ”„๋กœํผํ‹ฐ๋ฅผ ์ฐพ์Šต๋‹ˆ๋‹ค.
  • ์ฐพ์€ ๊ฐ’์„ ๊ฐ™์€ ์ด๋ฆ„์˜ ๋ณ€์ˆ˜์— ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค.
  • ์ด๋•Œ ์ด๋ฆ„์ด ๋ฐ˜๋“œ์‹œ ์ผ์น˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
let tiles = [];
let cols = 10;
let rows = 10;

function setup() {
  createCanvas(1000, 1000);
  background(10);
  frameRate(4);

  const tileWidth = width / cols;
  const tileHeight = height / cols;

  for (let y = 0; y < rows; y++) {
    for (let x = 0; x < cols; x++) {
      const pos = {
        x: x * tileWidth,
        y: y * tileHeight,
        w: tileWidth,
        h: tileHeight,
      };
      tiles.push(new Tile(pos));
    }
  }
}

function draw() {
  background(10);

  for (const tile of tiles) {
    tile.display();
  }
}

class Tile {
  constructor(position) {
    this.position = position;
  }

  display() {
    const { x, y, w, h } = this.position; // ๊ตฌ์กฐ๋ถ„ํ•ดํ• ๋‹น

    // let r = int(random(50, 70));
    // let g = int(random(200, 230));
    // let b = int(random(200, 250));
    strokeWeight(1);
    stroke(10);
    fill(150,150,150,255);
    rect(x, y, w, h);
  }
}

profile
Coding Art with Blender / oF / Processing / p5.js / nannou

0๊ฐœ์˜ ๋Œ“๊ธ€