算法艺术Skill algorithmic-art

算法艺术技能专注于使用p5.js库通过编程代码创建生成式艺术,强调种子随机性以确保作品可复现,并支持交互式参数探索。适用于代码艺术、生成艺术、流场模拟、粒子系统等创意编码项目,关键词包括:算法艺术、生成艺术、p5.js、代码艺术、流场、粒子系统、创意编码、可视化。

前端开发 0 次安装 0 次浏览 更新于 3/8/2026

名称: 算法艺术 描述: 使用p5.js创建算法艺术,具有种子随机性以实现可复现性,并支持交互式参数探索。当用户请求使用代码、生成艺术、算法艺术、流场或粒子系统创建艺术时使用。 来源: anthropics/skills 许可证: Apache-2.0

算法艺术

使用p5.js通过代码创建生成艺术,具有种子随机性以确保可复现性。

核心概念

种子随机性

// 使用种子以获得可复现的结果
function setup() {
  randomSeed(42);
  noiseSeed(42);
}

噪声函数

// 使用Perlin噪声生成有机图案
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);
  }
}

颜色调色板

// 定义调色板
const palette = ['#264653', '#2a9d8f', '#e9c46a', '#f4a261', '#e76f51'];

// 从调色板随机选择
fill(random(palette));

最佳实践

  • 使用 noLoop() 用于静态作品,用 save('art.png') 保存
  • 实验混合模式:blendMode(ADD)
  • 使用透明度图层增加深度
  • 使用 frameCount 进行动画