Created
November 9, 2019 16:53
-
-
Save ayourtch/dd372ddcfa55f0aab226e92a919e36c6 to your computer and use it in GitHub Desktop.
Nannou workshop at RustFest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use nannou::prelude::*; | |
fn main() { | |
nannou::app(model).update(update).view(view).run(); | |
} | |
struct Model { | |
a: WindowId, | |
thing_x: f32, | |
thing_y: f32, | |
thing_x0: f32, | |
thing_y0: f32, | |
ticker: f32, | |
cx: f32, | |
cy: f32, | |
} | |
fn model(app: &App) -> Model { | |
let a = app | |
.new_window() | |
.with_title("window a") | |
.event(event_a) | |
.build() | |
.unwrap(); | |
app.set_loop_mode(LoopMode::rate_fps(60.0)); | |
Model { a, thing_x: 0.0, thing_y: 0.0, thing_x0: 0.0, thing_y0: 0.0, ticker: 0.0, cx: 0.0, cy: 0.0 } | |
} | |
fn update(_app: &App, model: &mut Model, _update: Update) { | |
model.thing_x0 = model.thing_x0 + model.thing_y0.cos()/10.0; | |
model.thing_y0 = model.thing_y0 + model.thing_x0.cos()/10.0; | |
model.ticker = model.ticker + 0.05; | |
model.cx = model.cx + model.thing_x0.cos()* 10.0; | |
model.cy = model.cy + model.thing_y0.cos()* 10.0; | |
model.cx = model.cx + model.cy.cos()* 2.0; | |
model.cy = model.cy + model.cx.cos()* 2.0; | |
} | |
fn event_a(_app: &App, model: &mut Model, event: WindowEvent) { | |
use nannou::prelude::MouseScrollDelta::LineDelta; | |
println!("window a: {:?}", event); | |
if let MouseMoved(coord) = event { | |
model.thing_x = coord.x; | |
model.thing_y = coord.y; | |
} | |
if let MouseWheel(LineDelta(x, y), _phase) = event { | |
model.thing_x0 = x; | |
model.thing_y0 = y; | |
} | |
} | |
fn view(app: &App, model: &Model, frame: &Frame) { | |
use std::f32; | |
match frame.window_id() { | |
id if id == model.a => { | |
frame.clear(BLACK); | |
let draw = app.draw(); | |
for i in (1..10).into_iter().rev() { | |
// println!("Iteration {}", i); | |
let color = match i { | |
_ => RED, | |
}; | |
let color = RED; | |
let i_f: f32 = i as f32; | |
let thing = model.thing_y0 * 2.0; | |
let c_r = ((i_f / 10.0) + thing).sin(); | |
let c_g = (1.0 + i_f / 10.0 + thing + model.thing_x/10.0).sin(); | |
let c_b = (0.5 + i_f / 10.0 + thing + model.thing_y/10.0).sin(); | |
let c_r = c_r.abs(); | |
let c_g = c_g.abs(); | |
let c_b = c_b.abs(); | |
let mut color = rgb(c_r, c_g, c_b); | |
let d = model.ticker; // thing_x0 + model.thing_y0; | |
let dx = d.sin(); | |
let dy = d.cos(); | |
draw.ellipse() | |
.x_y((i_f*dx + model.thing_x/20.0) * 5.0 - 50.0 + dx*20.0 + model.cx, model.thing_y/4.0 + dy * i_f * 10.0 - 100.0 + dy*20.0 + model.cy) | |
.radius(i_f * 6.0 * (1.0 + model.thing_x0+ model.thing_y0)) | |
.color(color); | |
} | |
draw.to_frame(app, &frame).unwrap(); | |
} | |
_ => (), | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment