‹ pakt25pakt27 ›

pakt26

See /software/p5js/pakt-februari/pakt26/ for a JavaScript version and /software/supercollider/februari-pakt/pakt26/ for accompanying sound code.

use nannou::prelude::*;

fn main() {
  nannou::app(model)
    .update(update)
    .simple_window(view)
    .size(640, 480)
    .run();
}
struct Model {
  index: f32,
  nx: f32,
  ny: f32,
  cols: f32,
  rows: f32,
}
fn model(_app: &App) -> Model {
  let index = 0.0;
  let nx = 45.0;
  let ny = 9.0;
  let cols = 0.0;
  let rows = ny;
  Model {
    index,
    nx,
    ny,
    cols,
    rows,
  }
}
fn update(app: &App, model: &mut Model, _update: Update) {
  model.index = app.time * 60.0;
  model.cols = ((model.index * 0.001 + ((model.index * 0.0001) * 10.0).sin()).sin() * 0.445
    + 0.5)
    * (model.nx - 1.0);
}
fn view(app: &App, model: &Model, frame: Frame) {
  let draw = app.draw();
  let win = app.window_rect();

  if frame.nth() == 0 {
    frame.clear(BLACK);
  } else {
    draw.rect().wh(win.wh()).color(rgba(0.0, 0.0, 0.0, 0.06));
  }

  let rx = win.w() / model.cols;
  let ry = win.h() / model.rows;
  for x in 0..model.cols as u32 {
    let x = x as f32;
    for y in 0..model.rows as u32 {
      let y = y as f32;
      let c = ((model.index + (x * model.rows) + y)
        * (model.index * 0.0012).cos()
        * (model.index * 0.00006).sin()
        * 0.5)
        .sin()
        * 0.5
        + 0.5;
      let g = c
        * ((model.index * 0.002 + (x / model.nx * 2.0 * PI)).sin()
          * (model.index * 0.0018 + (y / model.ny * 2.0 * PI)).cos())
        * 12.0
        + 9.0;
      let r = Rect::from_w_h(rx - (g * 2.0), ry - (g * 2.0));
      draw.scale_axes(vec3(1.0, -1.0, 1.0))
        .translate(vec3(win.w(), win.h(), 0.0) * -0.5)
        .rect()
        .xy(vec2(x * rx + g, y * ry + g) - r.bottom_left())
        .wh(r.wh())
        .color(rgba(1.0, c, c, c));
    }
  }

  draw.to_frame(app, &frame).unwrap();
}