关于 algorithmic-art
algorithmic-art 是面向创作者的 p5.js 生成艺术技能,涵盖可复现随机性、噪声及常见图案构造,适用于静态作品和动画。
主要功能
- 01使用种子生成可复现随机效果
- 02利用噪声函数构造有机图案
- 03创建流场与粒子系统
- 04通过递归生成树形图案
- 05定义调色板并混合图层
- 06制作静态图像或动画
适用场景
- 01生成可复现的视觉作品
- 02创作流场与粒子艺术
- 03绘制递归树形图案
- 04导出静态图像或制作动画
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ moizibnyousaf/ ai-agent-skills --skill algorithmic-art安装后在 Claude Code 中输入/algorithmic-art确认已加载。
详细介绍
算法艺术
使用 p5.js 编写代码创作生成式艺术,并通过带种子的随机性确保结果可复现。
核心概念
带种子的随机性
// Use seed for reproducible results
function setup() {
randomSeed(42);
noiseSeed(42);
}
噪声函数
// Perlin noise for organic patterns
let x = noise(frameCount * 0.01) * width;
let y = noise(frameCount * 0.01 + 1000) * height;
常见模式
流场
let cols, rows, scale = 20;
let particles = [];
let flowfield;
function setup() {
createCanvas(800, 800);
cols = floor(width / scale);
rows = floor(height / scale);
flowfield = new Array(cols * rows);
for (let i = 0; i < 1000; i++) {
particles.push(new Particle());
}
}
function draw() {
let yoff = 0;
for (let y = 0; y < rows; y++) {
let xoff = 0;
for (let x = 0; x < cols; x++) {
let angle = noise(xoff, yoff) * TWO_PI * 2;
let v = p5.Vector.fromAngle(angle);
flowfield[x + y * cols] = v;
xoff += 0.1;
}
yoff += 0.1;
}
particles.forEach(p => {
p.follow(flowfield);
p.update();
p.show();
});
}
递归树
function branch(len) {
line(0, 0, 0, -len);
translate(0, -len);
if (len > 4) {
push();
rotate(PI / 6);
branch(len * 0.67);
pop();
push();
rotate(-PI / 6);
branch(len * 0.67);
pop();
}
}
粒子系统
class Particle {
constructor() {
this.pos = createVector(random(width), random(height));
this.vel = createVector(0, 0);
this.acc = createVector(0, 0);
this.maxSpeed = 4;
}
follow(flowfield) {
let x = floor(this.pos.x / scale);
let y = floor(this.pos.y / scale);
let force = flowfield[x + y * cols];
this.acc.add(force);
}
update() {
this.vel.add(this.acc);
this.vel.limit(this.maxSpeed);
this.pos.add(this.vel);
this.acc.mult(0);
}
show() {
stroke(255, 5);
point(this.pos.x, this.pos.y);
}
}
调色板
// Define palette
const palette = ['#264653', '#2a9d8f', '#e9c46a', '#f4a261', '#e76f51'];
// Random from palette
fill(random(palette));
最佳实践
- 对静态作品使用
noLoop(),并使用save('art.png')保存 - 尝试使用混合模式:
blendMode(ADD) - 叠加透明图层以营造景深
- 使用 frameCount 制作动画
评论
支持 Markdown