"/> "/>

nannou experiment

Nikolaus Gradwohl2020-12-19T05:36:08+00:00

I started playing with nannou - a rust based creative art framework.

this is the first animation I like so I exported it as single frames and turned it into a video.

The rust code looks like this - but be warned, I'm by no means a rust expert, this is my second ever rust program, so it might be a little wonky:

use nannou::prelude::*;

fn main() {
    nannou::app(model).update(update).run()
}

struct Model {
    p: Vector2,
    s: Vector2
}

fn model(app: &App) -> Model {
    app.new_window()
        .size(600,600)
        .view(view)
        .build()
        .unwrap();
    Model {
        p:vec2(0.0,0.0),
        s:vec2(0.0,0.0)
    }
}

fn update(app: &App, model: &mut Model, update: Update) {
    let t = (app.elapsed_frames() as f32) * 0.03;
    let w = (t * 0.832).sin() * 90.0 + 100.0;
    let h = (t * 0.734).cos() * 90.0 + 100.0;
    let x = (t * 0.132).cos() * 200.0;
    let y = (t * 0.176).sin() * 200.0;

    model.p = vec2(x,y);
    model.s = vec2(w,h);
}

fn view(app: &App, model: &Model, frame: Frame) {
    if frame.nth() == 0 {
        frame.clear(BLACK);
    }
    let draw = app.draw();

    draw.rect()
        .x_y(0.0,0.0)
        .w_h(600.0,600.0)
        .color(hsla(0.0,0.0,0.0,0.005));

    draw.ellipse()
        .xy(model.p)
        .wh(model.s)
        .color(hsla(0.5,1.0,0.2,0.01))
        .stroke(hsla(0.3,1.0,0.5,0.1))
        .stroke_weight(1.0);


    draw.to_frame(app, &frame).unwrap();
    if ( frame.nth() < 2000) {
        let file_path = captured_frame_path(app, &frame);
        app.main_window().capture_frame(file_path);
    }
}

fn captured_frame_path(app: &App, frame: &Frame) -> std::path::PathBuf {
    app.project_path()
        .expect("failed to locate `project_path`")
        .join("frames")
        .join(format!("{:04}", frame.nth()))
        .with_extension("png")
}
Tweet This! submit to reddit Digg! Tags: | no comments | no trackbacks

See also:

nannou experiment - amplitude modulated rainbow sinewave
nannou experiment - particles
nannou experiment no2 - perlin noise

Trackbacks

Comments

Leave a response

Leave a comment