-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathdraw_transform_local.rs
56 lines (45 loc) · 1.52 KB
/
draw_transform_local.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
use notan::draw::*;
use notan::math::{Mat3, Vec2};
use notan::prelude::*;
#[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);
let n = state.rot * 0.1;
draw.rect((0.0, 0.0), (100.0, 100.0))
// Use a helper function to translate the matrix
.translate(110.0 + n.sin() * 100.0, 10.0);
draw.rect((0.0, 0.0), (100.0, 100.0))
.color(Color::AQUA)
// Helper to pivot from a point using degrees
.rotate_degrees_from((50.0, 50.0), state.rot)
// Matrix translation
.translate(220.0, 220.0);
draw.circle(20.0)
.color(Color::ORANGE)
// Helper to scale from a point
.scale_from((0.0, 0.0), (2.0 + n.sin(), 2.0 + n.cos()))
// Matrix translation
.translate(500.0, 320.0);
// Create a matrix that we can set to the next paint
let translation = Mat3::from_translation(Vec2::new(200.0, 400.0));
let rotation = Mat3::from_angle(state.rot * 0.5_f32.to_radians());
let matrix = translation * rotation;
draw.rect((0.0, 0.0), (100.0, 100.0))
.color(Color::MAGENTA)
// Set directly a matrix for this object
.transform(matrix);
// Render the frame
gfx.render(&draw);
state.rot += app.timer.delta_f32() * 25.0;
}