Simple example that displays a canvas window and renders a spinning image from a png file
()
| 9 | /// Simple example that displays a canvas window and renders a spinning image from a png file |
| 10 | /// |
| 11 | pub fn main() { |
| 12 | // 'with_2d_graphics' is used to support operating systems that can't run event loops anywhere other than the main thread |
| 13 | with_2d_graphics(|| { |
| 14 | // Load a png file |
| 15 | let flo_bytes: &[u8] = include_bytes!["flo_drawing_on_window.png"]; |
| 16 | |
| 17 | // Create a window |
| 18 | let canvas = create_drawing_window("Flo trying not to get too dizzy"); |
| 19 | |
| 20 | // Load the texture into it |
| 21 | let mut flo_w = 0; |
| 22 | let mut flo_h = 0; |
| 23 | canvas.draw(|gc| { |
| 24 | // Clear the canvas and set up the coordinates |
| 25 | gc.clear_canvas(Color::Rgba(1.0, 1.0, 1.0, 1.0)); |
| 26 | gc.canvas_height(1000.0); |
| 27 | gc.center_region(0.0, 0.0, 1000.0, 1000.0); |
| 28 | |
| 29 | // Set up the texture |
| 30 | let (w, h) = gc.load_texture(TextureId(0), io::Cursor::new(flo_bytes)).unwrap(); |
| 31 | flo_w = w; |
| 32 | flo_h = h; |
| 33 | }); |
| 34 | |
| 35 | let mut angle = 0.0; |
| 36 | |
| 37 | loop { |
| 38 | // Render the png to the window |
| 39 | canvas.draw(|gc| { |
| 40 | // Draw on layer 0 |
| 41 | gc.layer(LayerId(0)); |
| 42 | gc.clear_layer(); |
| 43 | |
| 44 | let ratio = (flo_w as f32)/(flo_h as f32); |
| 45 | let height = 1000.0 / ratio; |
| 46 | let y_pos = (1000.0-height)/2.0; |
| 47 | |
| 48 | let mid_x = 500.0; |
| 49 | let mid_y = y_pos+(height/2.0); |
| 50 | |
| 51 | // Draw a circle... |
| 52 | gc.new_path(); |
| 53 | gc.circle(mid_x, mid_y, height/2.0); |
| 54 | |
| 55 | // Fill with the texture we just loaded |
| 56 | gc.fill_texture(TextureId(0), 0.0, y_pos+height as f32, 1000.0, y_pos); |
| 57 | |
| 58 | gc.fill_transform(Transform2D::translate(-mid_x, -mid_y)); |
| 59 | gc.fill_transform(Transform2D::rotate_degrees(angle)); |
| 60 | gc.fill_transform(Transform2D::scale(1.0/3.0, 1.0/3.0)); |
| 61 | gc.fill_transform(Transform2D::translate(mid_x, mid_y)); |
| 62 | gc.fill(); |
| 63 | |
| 64 | // Draw another couple of circles to demonstrate that it's the texture that's spinning and not the whole canvas |
| 65 | gc.fill_color(Color::Rgba(0.0, 0.0, 0.0, 1.0)); |
| 66 | |
| 67 | gc.new_path(); |
| 68 | gc.circle(mid_x - height/2.5, mid_y - height/2.5, 32.0); |
nothing calls this directly
no test coverage detected