‹ pakt01pakt03 ›

pakt02

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

use nannou::lyon;
use nannou::prelude::*;

const N: u16 = 50;

fn main() {
  nannou::sketch(view).size(640, 480).run();
}

fn view(app: &App, frame: Frame) {
  let draw = app.draw();
  let win = app.window_rect();
  frame.clear(BLACK);

  let index = app.time * 60.0;
  let x = win.w() * -0.5 + 10.0 + ((index * 0.016).sin() * 2.0);
  let y = (index * 0.010).sin() * 50.0;
  let x3 = win.w() * 0.5 - 10.0 + ((index * 0.012).sin() * 2.0);
  let y3 = (index * 0.014).sin() * 50.0;
  let spread = map_range((index * 0.0005).sin(), -1.0, 1.0, 0.5, 1.5);

  let rad = win.h();
  for angle in 0..360 {
    let angle = angle as f32;
    let mut points = Vec::with_capacity(3);
    points.push((pt2(0.0, 0.0), rgb(0.5, 0.6, 0.7)));

    let vx = angle.to_radians().cos();
    let vy = angle.to_radians().sin();
    points.push((pt2(vx, vy) * rad, rgb(0.0, 0.0, 0.0)));

    let next_vx = (angle + 1.0).to_radians().cos();
    let next_vy = (angle + 1.0).to_radians().sin();
    points.push((pt2(next_vx, next_vy) * rad, rgb(0.0, 0.0, 0.0)));

    draw.polygon().points_colored(points);
  }

  let mut builder = nannou::geom::path::Builder::new().with_svg();
  for i in 0..N {
    let i = i as f32;
    let x1 = map_range((index * 0.135 + (i * spread)).sin(), -1.0, 1.0, 0.1, 0.5);
    let y1 = ((index + i) * 0.130 + (i * spread)).cos();
    let x2 = map_range((index * 0.125 + (i * spread)).sin(), -1.0, 1.0, 0.5, 0.9);
    let y2 = ((index + i) * 0.120 + (i * spread)).cos();
    builder.move_to(lyon::math::point(x, y));
    builder.cubic_bezier_to(
      lyon::math::point(x1 * win.w() * 0.5, y1 * win.h() * 0.5),
      lyon::math::point(x2 * win.w() * 0.5, y2 * win.h() * 0.5),
      lyon::math::point(x3, y3),
    );
  }
  let path = builder.build();
  draw.scale_axes(vec3(1.0, -1.0, 1.0))
    .path()
    .stroke()
    .color(rgba(1.0, 1.0, 0.0, 0.8))
    .weight(1.0)
    .events(path.iter());

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