
📝 Rust Code
use nannou::prelude::*;
use nannou::rand::random_f32;
fn main() {
nannou::app(model).update(update).run();
}
struct PointData {
position: Point2,
color: Rgba,
}
struct Model {
points: Vec<PointData>,
}
fn model(app: &App) -> Model {
app.new_window()
.size(800, 600)
.view(view)
.mouse_pressed(mouse_pressed)
.build()
.unwrap();
Model {
points: Vec::new(),
}
}
fn update(_app: &App, model: &mut Model, _update: Update) {
if random_f32() < 0.05 {
let x = random_range(-300.0, 300.0);
let y = random_range(-300.0, 300.0);
let color = rgba(
random_f32(),
random_f32(),
random_f32(),
0.8,
);
model.points.push(PointData {
position: pt2(x, y),
color,
});
}
if model.points.len() > 150 {
model.points.remove(0);
}
}
fn mouse_pressed(app: &App, model: &mut Model, button: MouseButton) {
if button == MouseButton::Left {
let mouse_pos = app.mouse.position();
let color = rgba(
random_f32(),
random_f32(),
random_f32(),
0.8,
);
model.points.push(PointData {
position: mouse_pos,
color,
});
}
}
fn view(app: &App, model: &Model, frame: Frame) {
let draw = app.draw();
draw.rect()
.wh(app.window_rect().wh())
.color(rgba(0.0, 0.0, 0.0, 0.03));
for point_data in &model.points {
draw.ellipse()
.xy(point_data.position)
.radius(10.0)
.color(point_data.color);
}
draw.to_frame(app, &frame).unwrap();
}
use nannou::prelude::*;
use nannou::rand::random_f32;
fn main() {
nannou::app(model).update(update).run();
}
struct PointData {
position: Point2,
color: Rgba,
}
struct Model {
points: Vec<PointData>,
}
fn model(app: &App) -> Model {
app.new_window()
.size(800, 600)
.view(view)
.mouse_pressed(mouse_pressed)
.build()
.unwrap();
Model {
points: Vec::new(),
}
}
fn update(_app: &App, model: &mut Model, _update: Update) {
if random_f32() < 0.05 {
let x = random_range(-300.0, 300.0);
let y = random_range(-300.0, 300.0);
let color = rgba(
random_f32(),
random_f32(),
random_f32(),
0.8,
);
model.points.push(PointData {
position: pt2(x, y),
color,
});
}
if model.points.len() > 150 {
model.points.remove(0);
}
}
fn mouse_pressed(app: &App, model: &mut Model, button: MouseButton) {
if button == MouseButton::Left {
let mouse_pos = app.mouse.position();
let color = rgba(
random_f32(),
random_f32(),
random_f32(),
0.8,
);
model.points.push(PointData {
position: mouse_pos,
color,
});
}
}
fn view(app: &App, model: &Model, frame: Frame) {
let draw = app.draw();
draw.rect()
.wh(app.window_rect().wh())
.color(rgba(0.0, 0.0, 0.0, 0.03));
for point_data in &model.points {
draw.ellipse()
.xy(point_data.position)
.radius(10.0)
.color(point_data.color);
}
draw.to_frame(app, &frame).unwrap();
}