From 31f496d2ba3bc7f6fae0cf074fb705f98907a1c9 Mon Sep 17 00:00:00 2001 From: annieversary Date: Tue, 17 Aug 2021 16:26:37 +0200 Subject: [PATCH] subtitled #4 --- Cargo.lock | 8 +++++ README.md | 1 + crates/subtitled4/Cargo.toml | 8 +++++ crates/subtitled4/src/main.rs | 59 +++++++++++++++++++++++++++++++++++ 4 files changed, 76 insertions(+) create mode 100644 crates/subtitled4/Cargo.toml create mode 100644 crates/subtitled4/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index 5c548bd..032ecc4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2364,6 +2364,14 @@ dependencies = [ "utils", ] +[[package]] +name = "subtitled4" +version = "0.1.0" +dependencies = [ + "nannou", + "utils", +] + [[package]] name = "syn" version = "1.0.74" diff --git a/README.md b/README.md index 237aff3..646d74b 100644 --- a/README.md +++ b/README.md @@ -7,3 +7,4 @@ this is a bunch of nannou projects - subtitled #1: some spinning circles - subtitled #2: idk spinning colors or something - subtitled #3: small flashing circles +- subtitled #4: more spinning circles wow what a surprise diff --git a/crates/subtitled4/Cargo.toml b/crates/subtitled4/Cargo.toml new file mode 100644 index 0000000..752e173 --- /dev/null +++ b/crates/subtitled4/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "subtitled4" +version = "0.1.0" +edition = "2018" + +[dependencies] +nannou = "0.17" +utils = { path = "../utils" } diff --git a/crates/subtitled4/src/main.rs b/crates/subtitled4/src/main.rs new file mode 100644 index 0000000..f5bf37c --- /dev/null +++ b/crates/subtitled4/src/main.rs @@ -0,0 +1,59 @@ +use nannou::{color::Mix, prelude::*}; + +use utils::*; + +fn main() { + nannou::app(model).update(update).simple_window(view).run(); +} + +struct Model { + points: Vec, +} + +fn model(_app: &App) -> Model { + let r = 300.0; + + Model { + points: (0..100) + .map(|_| { + let r = r * random::().sqrt(); + let theta = random::() * TAU; + let x = r * theta.cos(); + let y = r * theta.sin(); + vec2(x, y) + }) + .collect(), + } +} + +fn update(_app: &App, model: &mut Model, _update: Update) { + for p in &mut model.points { + *p += vec2(p.y, -p.x) * 0.01; + } +} + +fn view(app: &App, model: &Model, frame: Frame) { + let t = frame.nth() as f32 / 60.0; + + let draw = app.draw(); + let bg_color = srgb(1.0f32, 250.0 / 255.0, 250.0 / 255.0); + let fg_color = srgb(189.0 / 255.0, 178.0 / 255.0, 1.0); + + if frame.nth() == 1 { + draw.background().color(bg_color); + } else { + let win = app.window_rect(); + draw.rect() + .wh(win.wh()) + .color(srgba(1., 250.0 / 255.0, 250.0 / 255.0, 0.001)); + } + + for p in &model.points { + let m = map_sin(t * 1.2 + p.length() / 100.0, 0.0, 1.0).powi(3); + + let color = fg_color.into_linear().mix(&bg_color.into_linear(), m); + draw.ellipse().xy(*p).radius(10.0).color(color); + } + + draw.to_frame(app, &frame).unwrap(); +}