์ ๊ทธ๋ฆฌ๊ธฐ

function setup() {
createCanvas(800, 400);
background(23);
}
function draw() {
for (let i = 20; i < width; i += 20) {
stroke(255);
line(i, 0, i, height);
}
}
- ์ด ์์ ์์๋
draw ํจ์ ๋์ setup์ ๊ทธ๋ฆฌ๋ ๊ฒ์ด ๋ ํจ์จ์ ์
๋๋ค.
draw๋ ๋งค ํ๋ ์๋ง๋ค ์คํ๋๋ฏ๋ก, ๋ผ์ธ์ ๊ณ์ ๊ทธ๋ฆฌ๊ฒ ๋ฉ๋๋ค.
- x ์ขํ๋ฅผ 20๋ถํฐ ์์ํด์ 20์ฉ ์ฆ๊ฐ์ํค๋ฉฐ ๋ฐ๋ณต
i < 800 ์ด๋ฉด x๊ฐ 780๊น์ง ๊ทธ๋ ค์ง๊ณ , i <= 800 ์ด๋ฉด x๊ฐ 800๊น์ง ๊ทธ๋ ค์ง๋๋ค.
- ์ฌ๊ธฐ์๋ 800์ ๋์ด๊ฐ์ง ์๋๋ก < 800์ผ๋ก ์ค์ ํ์ต๋๋ค.
line()๋ (x1, y1, x2, y2)๋ฅผ ์ธ์๋ก ๋ฐ์์ ์ ์ ๊ทธ๋ฆฌ๋ ํจ์์
๋๋ค.
๊ฒฉ์ ํจํด (Grid Pattern)

function setup() {
createCanvas(800, 400);
background(25);
noFill();
stroke(100, 200, 255);
strokeWeight(1);
}
function draw() {
for (let y = 20; y < height; y += 40) {
for (let x = 20; x < width; x += 40) {
rect(x, y, 30, 30);
}
}
}
์ ํจํด (Dot Pattern)

function setup() {
createCanvas(800, 400);
noLoop();
noStroke();
}
function draw() {
background(25);
let step = 20;
for (let x = 0; x < width; x += step) {
for (let y = 0; y < height; y += step) {
let r = int(random(0, 20));
let g = int(random(130, 180));
let b = int(random(120, 150));
fill(r, g, b);
ellipse(x + step / 2, y + step / 2, step - 2);
}
}
}
๋ฐฉ์ฌํ ํจํด (Radial Symmetry)

function setup() {
createCanvas(800, 400);
colorMode(HSB, 360, 100, 100, 100);
}
function draw() {
background(0, 0, 10);
translate(width / 2, height / 2);
let layers = 8;
let petals = 16;
for (let j = 0; j < layers; j++) {
let radius = map(j, 0, layers, 40, 200);
for (let i = 0; i < petals; i++) {
push();
rotate((TWO_PI * i) / petals);
let hue = map(j, 0, layers, 150, 200);
fill(hue, 80, 100, 80);
noStroke();
ellipse(radius, 0, 12, 12);
pop();
}
}
}
colorMode(HSB)๋ฅผ ์ฌ์ฉํด์ hue๊ฐ๋ง 150~200 ์ฌ์ด๋ก ๋ณํ๋๋ก ํ์ต๋๋ค. ๊ฐ ์ธ์์ ๋ค์ด๊ฐ๋ ๊ฐ์ ๋ฒ์๋ ์๋์ ๊ฐ์ต๋๋ค.
- Hue: 0~360 (์์)
- Saturation: 0~100 (์ฑ๋)
- Brightness: 0~100 (๋ช
๋)
- Alpha: 0~100 (ํฌ๋ช
๋)
๋์ ํ (Spriral)

function setup() {
createCanvas(800, 400);
angleMode(DEGREES);
noLoop();
}
function draw() {
background(25);
translate(width / 2, height / 2);
let n = 200;
let baseRadius = 2;
let angleStep = 10;
for (let i = 0; i < n; i++) {
let angle = i * angleStep;
let radius = baseRadius + i * 1.8;
let x = radius * cos(angle);
let y = radius * sin(angle);
let brightness = map(i, 0, n, 50, 255);
fill(brightness, 120, 200, 180);
noStroke();
ellipse(x, y, 15, 15);
}
}
์์๋ ์ง๋ํ๋ ๊ณต

function setup() {
createCanvas(800, 400);
background(23, 23, 23);
noStroke();
}
function draw() {
background(23, 23, 23);
for (let x = 0; x < width; x += 20) {
let y = map(
sin(x * 0.05 + frameCount * 0.05),
-1,
1,
height * 0.2,
height * 0.8
);
let circleSize = map(y, height * 0.2, height * 0.8, 20, 5);
fill(100, 200, 255);
circle(x, y, circleSize);
}
}
frameCount: p5.js๊ฐ ์์๋ ์ดํ์ ์ด ํ๋ ์ ์๋ฅผ ์ ์ฅํ๋ ๋ณ์์
๋๋ค. ์๊ฐ์ ํ๋ฆ์ ๋ํ๋ด๋ ์ฒ๋๋ก ์ฌ์ฉ๋ฉ๋๋ค.
sin(x * 0.05 + frameCount * 0.05): x ์์น์ frameCount๋ฅผ ์กฐํฉํ์ฌ ๊ฐ ์ ์ ํ๋ ๋์ด๋ฅผ ๊ณ์ฐํฉ๋๋ค. frameCount๊ฐ ๊ณ์ ์ฆ๊ฐํ๋ฏ๋ก ํ๋๊ฐ ๊ณ์ ์์ง์ด๋ ๊ฒ์ฒ๋ผ ๋ณด์
๋๋ค.
map(): sin() ํจ์๊ฐ ๋ฐํํ๋ -1~1์ ๊ฐ์ ์บ๋ฒ์ค์ ์๋จ 20% ์ง์ ์์ ํ๋จ 80% ์ง์ ์ผ๋ก, ๊ทธ๋ฆฌ๊ณ ์์ ํฌ๊ธฐ๋ 20์์ 5๋ก ๋งคํํ์ฌ ๊น์ด๊ฐ์ ์ค๋๋ค.
ํ์ ํ๋ ์ฌ๊ฐํ (rotating square)

function setup() {
createCanvas(800, 400);
}
function draw() {
background(23, 23, 23);
push();
translate(width / 2, height / 2);
rotate(frameCount * 0.01);
for (let i = 0; i < 12; i++) {
push();
let angle = (TWO_PI / 12) * i;
rotate(angle);
translate(150, 0);
fill(255, 200, 0);
noStroke();
rectMode(CENTER);
rect(0, 0, 30, 30);
pop();
}
pop();
}
push() / pop(): push()๋ ํ์ฌ์ ์ขํ๊ณ, ์์ ๋ฑ ์ํ๋ฅผ ์ ์ฅํ๊ณ , pop()์ ๋ง์ง๋ง์ผ๋ก ์ ์ฅ๋ ์ํ๋ฅผ ๋ณต์ํฉ๋๋ค. ์ด๋ฅผ ์ฌ์ฉํ๋ฉด ์ขํ ๋ณํ๊ฐ ์๋ก ์ํฅ์ ์ฃผ์ง ์์ ๋ณต์กํ ๊ทธ๋ํฝ์ ๊ทธ๋ฆฌ๊ธฐ ํธํด์ง๋๋ค.
translate(): ์ขํ์ ์์ (0,0)์ ์ง์ ํ ์์น๋ก ์ด๋์ํต๋๋ค.
rotate(): ํ์ฌ ์ขํ๊ณ๋ฅผ ์ง์ ํ ๊ฐ๋๋งํผ ํ์ ์ํต๋๋ค. frameCount๋ฅผ ์ฌ์ฉํ๋ฉด ์๊ฐ์ ๋ฐ๋ผ ์์ฐ์ค๋ฝ๊ฒ ํ์ ํฉ๋๋ค.
TWO_PI: 360๋๋ฅผ ๋ผ๋์ ๊ฐ์ผ๋ก ๋ํ๋ธ ๊ฒ์
๋๋ค(์ฝ 6.28). 12๊ฐ์ ๋ํ์ ์ํ์ผ๋ก ๋ฐฐ์นํ๊ธฐ ์ํด ๊ฐ ๋ํ๋ง๋ค TWO_PI / 12๋งํผ ํ์ ์ํต๋๋ค.
2์ฐจ์ ํ๋ ๊ทธ๋ฆฌ๋ (2D Wave Grid)

function setup() {
createCanvas(800, 400);
noLoop();
background(25);
noStroke();
let cols = 20;
let rows = 15;
let spacingX = width / cols;
let spacingY = height / rows;
for (let i = 0; i < cols; i++) {
for (let j = 0; j < rows; j++) {
let cx = i * spacingX + spacingX / 2;
let cy = j * spacingY + spacingY / 2;
let angle = i * 0.6 + j * 0.3;
let r = 20 + 8 * sin(angle);
fill(150, 80, 220, 250);
ellipse(cx, cy, r, r);
}
}
}
function setup() {
createCanvas(800, 400);
background(25);
noStroke();
}
function draw() {
background(25);
let cols = 20;
let rows = 15;
let spacingX = width / cols;
let spacingY = height / rows;
for (let i = 0; i < cols; i++) {
for (let j = 0; j < rows; j++) {
let cx = i * spacingX + spacingX / 2;
let cy = j * spacingY + spacingY / 2;
let angle = i * 0.6 + j * 0.3 + frameCount * 0.05;
let r = 20 + 8 * sin(angle);
fill(150, 80, 220, 250);
ellipse(cx, cy, r, r);
}
}
}
- ๊ท์น์ ์ธ ๊ฒฉ์ ์์์๋ sin()๋ก ์ํ์ ๋ณํ์ ์ฃผ์ด ์ ๊ธฐ์ ์ธ ํจํด์ ์์ฑํ ์ ์์ต๋๋ค.