



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 ๊ฐ์ ๊บผ๋ด์ด ์ง์ญ ๋ณ์๋ก ๋ง๋๋ ๋ฌธ๋ฒ
์๋ฐ์คํฌ๋ฆฝํธ ์์ง์ ๋ค์ ๊ณผ์ ์ ์ํํฉ๋๋ค.
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);
}
}