基础
setup(), draw(), circle(), rect()
algorithmic-art 是使用 p5.js 创建算法艺术的技能。它的核心是:通过代码生成独特的算法艺术,包括粒子系统、流场、噪点等。
作为创作者,你可能遇到过:
algorithmic-art 就是解决这些的。
算法艺术 = 计算美学运动
特点:- 计算过程生成- 涌现行为- 数学之美- 有机系统1. 创建算法哲学 - 定义美学运动 - 编写宣言 - 规划生成逻辑
2. 用代码表达 - p5.js sketches - 90% 算法生成 - 10% 参数调整function setup() { createCanvas(800, 600); background(0);}
function draw() { // 绘制逻辑}function draw() { // 圆形 circle(x, y, diameter);
// 矩形 rect(x, y, width, height);
// 线 line(x1, y1, x2, y2);
// 颜色 fill(255, 0, 0); stroke(255);}let x = 0;
function setup() { createCanvas(800, 600); background(0);}
function draw() { background(0, 10); // 轨迹效果
circle(x, height/2, 50); x += 2;
if (x > width) x = 0;}class Particle { constructor() { this.x = random(width); this.y = random(height); this.vx = random(-1, 1); this.vy = random(-1, 1); this.size = random(2, 8); }
update() { this.x += this.vx; this.y += this.vy;
// 边界反弹 if (this.x < 0 || this.x > width) this.vx *= -1; if (this.y < 0 || this.y > height) this.vy *= -1; }
display() { noStroke(); fill(255, 100); circle(this.x, this.y, this.size); }}
let particles = [];
function setup() { createCanvas(800, 600); for (let i = 0; i < 100; i++) { particles.push(new Particle()); }}
function draw() { background(0, 20); for (let p of particles) { p.update(); p.display(); }}function draw() { background(0); noLoop();
let scale = 0.01; for (let x = 0; x < width; x += 5) { for (let y = 0; y < height; y += 5) { let n = noise(x * scale, y * scale); let size = map(n, 0, 1, 2, 20); fill(255, n * 255); circle(x, y, size); } }}function setup() { createCanvas(800, 600);}
function draw() { background(0); translate(width/2, height/2);
let num = 12; for (let i = 0; i < num; i++) { push(); rotate(TWO_PI * i / num);
// 绘制形状 fill(255, 100); noStroke(); rect(-50, -10, 100, 20);
pop(); }}function draw() { background(0, 10);
let d = dist(mouseX, mouseY, width/2, height/2);
fill(255, d); circle(width/2, height/2, d);}let param = 50;
function setup() { createCanvas(800, 600);}
function draw() { background(0); translate(width/2, height/2);
for (let i = 0; i < 100; i++) { let angle = i * param * 0.01; let r = i * 2; let x = r * cos(angle); let y = r * sin(angle);
fill(255, 100); circle(x, y, 3); }}
function mousePressed() { param += 10;}创建算法哲学文档:
1. 美学运动名称2. 核心理念3. 生成规则4. 参数变量p5.js 实现:
1. 设置画布2. 实现核心算法3. 添加交互4. 优化性能生成文件:
1. .md - 算法哲学2. .html - 交互查看器3. .js - 生成算法// 可重现的随机randomSeed(42);noiseSeed(42);// 减少计算noStroke();simpleShapes();
// 使用 offscreen bufferlet pg;function setup() { pg = createGraphics(width, height);}避免:- 随机堆砌- 缺乏主题- 过于复杂
追求:- 有意义的模式- 数学之美- 简洁优雅基础
setup(), draw(), circle(), rect()
算法
粒子系统、噪点场、旋转对称
交互
mouseX, mouseY, mousePressed()
优化
randomSeed(), noStroke(), pg
查看源文件: GitHub原始文件