๐ŸŽ‡ Vanishing Image with Particles

BamgasiJMยท2026๋…„ 1์›” 8์ผ

p5.js Art

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

๐Ÿ“ p5.js

let img;
const detail = 6;
let particles = [];
let grid = [];
let particleImage;
let ctx;

function preload() {
  img = loadImage(
    "/image_01.jpg"
  );
}

class Particle {
  constructor(x, y) {
    this.x = x || random(width);
    this.y = y || random(height);
    this.prevX = this.x;
    this.speed = 0;
    this.v = random(0, 0.7);
  }

  update() {
    if (grid.length) {
      this.speed = grid[floor(this.y / detail)][floor(this.x / detail)] * 0.97;
    }
    this.x += (1 - this.speed) * 3 + this.v;

    if (this.x > width) {
      this.x = 0;
    }
  }

  draw() {
    image(particleImage, this.x, this.y);
  }
}

function setup() {
  const canvas = createCanvas(100, 100);
  ctx = canvas.drawingContext;
  pixelDensity(1);

  // ํŒŒํ‹ฐํด ์ด๋ฏธ์ง€ ์ƒ์„ฑ
  particleImage = createGraphics(6, 6);
  particleImage.fill(255, 200, 60);
  particleImage.noStroke();
  particleImage.circle(4, 4, 4);

  windowResized();
}

function windowResized() {
  const imgRatio = img.width / img.height;
  if (windowWidth / windowHeight > imgRatio) {
    resizeCanvas(floor(windowHeight * imgRatio), floor(windowHeight));
  } else {
    resizeCanvas(floor(windowWidth), floor(windowWidth / imgRatio));
  }

  initializeEffect();
}

function initializeEffect() {
  clear();
  ctx.globalAlpha = 1;

  // ์ด๋ฏธ์ง€๋ฅผ ๋กœ๋“œํ•˜๊ณ  ํ”ฝ์…€ ๋ฐ์ดํ„ฐ ์ถ”์ถœ
  image(img, 0, 0, width, height);
  loadPixels();
  clear();
  noStroke();

  // ๋ฐ๊ธฐ ๊ทธ๋ฆฌ๋“œ ์ƒ์„ฑ
  grid = [];
  for (let y = 0; y < height; y += detail) {
    let row = [];
    for (let x = 0; x < width; x += detail) {
      const r = pixels[(y * width + x) * 4];
      const g = pixels[(y * width + x) * 4 + 1];
      const b = pixels[(y * width + x) * 4 + 2];
      const _color = color(r, g, b);
      const _brightness = brightness(_color) / 100;
      row.push(_brightness);
    }
    grid.push(row);
  }

  // ํŒŒํ‹ฐํด ์ƒ์„ฑ (๊ฐœ์ˆ˜๊ฐ€ ๋ณ€ํ•ด๋„ ๊ท ๋“ฑ ๋ถ„ํฌ))
  const particleCount = 8000;
  particles = [];
  for (let i = 0; i < particleCount; i++) {
    particles.push(new Particle(null, (i / particleCount) * height));
  }
}

// ํŒŒํ‹ฐํด ์•Œ๊ฐฑ์ด๋กœ ๋ณด์ด๊ฒŒ ํ•˜๊ธฐ
function draw() {
  // ๋งค ํ”„๋ ˆ์ž„๋งˆ๋‹ค ์™„์ „ํžˆ ํด๋ฆฌ์–ด
  clear();

  // ํŒŒํ‹ฐํด ์—…๋ฐ์ดํŠธ ๋ฐ ๊ทธ๋ฆฌ๊ธฐ
  // ctx.globalAlpha =1;
  particles.forEach((p) => {
    p.update();
    ctx.globalAlpha = p.speed * 0.8;
    p.draw();
  });
}

// ํŒŒํ‹ฐํด์— trail effect
// function draw() {
//   // ํŽ˜์ด๋“œ ํšจ๊ณผ๋ฅผ ์œ„ํ•œ ๋ฐ˜ํˆฌ๋ช… ๊ฒ€์€์ƒ‰ ๋ ˆ์ด์–ด
//   ctx.globalAlpha = 0.05;
//   fill(0);
//   rect(0, 0, width, height);

//   // ํŒŒํ‹ฐํด ์—…๋ฐ์ดํŠธ ๋ฐ ๊ทธ๋ฆฌ๊ธฐ
//   ctx.globalAlpha = 0.2;
//   particles.forEach((p) => {
//     p.update();
//     ctx.globalAlpha = p.speed * 0.3;
//     p.draw();
//   });
// }

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

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