رفتن به محتواCREATIVE·CODE·ART
p5.js

Sunseed

A sunflower, drawn by maths. 2,600 seeds placed one by one at the golden angle — 137.5 degrees — the same rule real sunflowers grow by. Nudge the Angle slider a fraction of a degree and the whole head rewrites itself into spirals, spokes or stars.

مجوز: CC BY-NC-SA 4.0: ریمیکس با ذکر نام، غیرتجاری، با همین مجوز

دیدن کد
// Sunseed — a phyllotaxis study in p5.js
// Seeds placed by the golden angle (137.5°) — the same maths a sunflower uses.
// Tiny changes to the angle completely rewrite the pattern.
// Palette: charcoal #0D0D12, bone #e9e5da, signal orange #ffb454
// Click the canvas for a new seed · Press S to save a frame
// Live sliders: Angle (golden-angle detune) · Spread · Spin · Pulse

const BONE = [233, 229, 218];
const ORANGE = [255, 180, 84];
const BG = [13, 13, 18];
const COUNT = 2600;

let rot = 0;
let sAngle, sSpread, sSpin, sPulse, uiBox;

function setup() {
  createCanvas(windowWidth, windowHeight);
  buildControls();
}

function buildControls() {
  uiBox = createDiv();
  uiBox.style('position', 'absolute');
  uiBox.style('left', '14px');
  uiBox.style('background', 'rgba(13,13,18,0.55)');
  uiBox.style('padding', '10px 12px');
  uiBox.style('border-radius', '10px');
  uiBox.style('font-family', "'JetBrains Mono', monospace");
  uiBox.style('color', '#e9e5da');
  uiBox.style('z-index', '5');
  const row = (label, slider) => {
    const r = createDiv();
    r.parent(uiBox);
    r.style('display', 'flex');
    r.style('align-items', 'center');
    r.style('gap', '10px');
    r.style('margin', '3px 0');
    const lab = createSpan(label);
    lab.parent(r);
    lab.style('font-size', '11px');
    lab.style('width', '52px');
    lab.style('letter-spacing', '0.05em');
    slider.parent(r);
    slider.style('width', '130px');
  };
  sAngle  = createSlider(136.5, 138.5, 137.5, 0.01);  row('Angle', sAngle);
  sSpread = createSlider(6, 16, 10.5, 0.1);           row('Spread', sSpread);
  sSpin   = createSlider(0, 3, 1, 0.1);               row('Spin', sSpin);
  sPulse  = createSlider(0, 10, 4, 0.5);              row('Pulse', sPulse);
  placeControls();
}

function placeControls() {
  if (uiBox) uiBox.position(14, height - 138);
}

function draw() {
  background(...BG);
  translate(width / 2, height / 2);
  rot += 0.0016 * sSpin.value();
  const pulse = 1 + (sPulse.value() / 100) * sin(frameCount * 0.02);
  const c = sSpread.value() * pulse * (min(width, height) / 1080);
  const ang = radians(sAngle.value());
  const maxR = c * sqrt(COUNT);
  noStroke();
  for (let n = COUNT; n >= 1; n--) {
    const r = c * sqrt(n);
    const a = n * ang + rot;
    const x = r * cos(a);
    const y = r * sin(a);
    const m = n / COUNT;
    const cr = lerp(ORANGE[0], BONE[0], m * 0.55);
    const cg = lerp(ORANGE[1], BONE[1], m * 0.55);
    const cb = lerp(ORANGE[2], BONE[2], m * 0.55);
    const s = map(r, 0, maxR, 2.2, 10.5);
    fill(cr, cg, cb, 42);
    ellipse(x, y, s * 2.1);
    fill(cr, cg, cb, 225);
    ellipse(x, y, s);
  }
}

function mousePressed(e) {
  if (e && e.target && e.target.tagName !== 'CANVAS') return;
  sAngle.value(137.5 + random(-0.9, 0.9));
}

function keyPressed() {
  if (key === 's' || key === 'S') saveCanvas('sunseed', 'png');
}

function windowResized() {
  resizeCanvas(windowWidth, windowHeight);
  placeControls();
}