-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathdraw_transform.rs
63 lines (51 loc) · 1.56 KB
/
draw_transform.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
use notan::draw::*;
use notan::math::{Mat3, Vec2};
use notan::prelude::*;
const COLORS: [Color; 8] = [
Color::WHITE,
Color::MAGENTA,
Color::ORANGE,
Color::RED,
Color::YELLOW,
Color::AQUA,
Color::MAROON,
Color::PINK,
];
#[derive(AppState, Default)]
struct State {
rot: f32,
}
#[notan_main]
fn main() -> Result<(), String> {
notan::init_with(State::default)
.add_config(DrawConfig)
.draw(draw)
.build()
}
fn draw(app: &mut App, gfx: &mut Graphics, state: &mut State) {
let mut draw = gfx.create_draw();
draw.clear(Color::BLACK);
// Push to the transformation stack a translation matrix
draw.transform()
.push(Mat3::from_translation(Vec2::new(350.0, 250.0)));
// Calculate the matrix that we use for each object
let translation = Mat3::from_translation(Vec2::new(30.0, 20.0));
let rotation = Mat3::from_angle(state.rot.to_radians());
let matrix = translation * rotation;
for (i, c) in COLORS.iter().enumerate() {
let n = (i * 7) as f32;
let size = 100.0 - n;
// Push to the stack the same matrix on each draw
// The new matrices will be multiplied by the latest on the stack
draw.transform().push(matrix);
// Create a rect
draw.rect((0.0, 0.0), (size, size))
// Using different color for each rect
.color(*c);
}
// Reset the transformation stack
draw.transform().clear();
// Render the frame
gfx.render(&draw);
state.rot = (state.rot + app.timer.delta_f32() * 25.0) % 360.0;
}