Simple example that displays a canvas window and renders an image from a png file
()
| 7 | /// Simple example that displays a canvas window and renders an image from a png file |
| 8 | /// |
| 9 | pub fn main() { |
| 10 | // 'with_2d_graphics' is used to support operating systems that can't run event loops anywhere other than the main thread |
| 11 | with_2d_graphics(|| { |
| 12 | // Load a png file |
| 13 | let flo_bytes: &[u8] = include_bytes!["flo_drawing_on_window.png"]; |
| 14 | |
| 15 | // Create a window |
| 16 | let canvas = create_drawing_window("Flo drawing on a window"); |
| 17 | |
| 18 | // Render the png to the window |
| 19 | canvas.draw(|gc| { |
| 20 | // Clear the canvas and set up the coordinates |
| 21 | gc.clear_canvas(Color::Rgba(1.0, 1.0, 1.0, 1.0)); |
| 22 | gc.canvas_height(1000.0); |
| 23 | gc.center_region(0.0, 0.0, 1000.0, 1000.0); |
| 24 | |
| 25 | // Set up the texture |
| 26 | let (flo_w, flo_h) = gc.load_texture(TextureId(0), io::Cursor::new(flo_bytes)).unwrap(); |
| 27 | |
| 28 | let ratio = (flo_w as f32)/(flo_h as f32); |
| 29 | let height = 1000.0 / ratio; |
| 30 | let y_pos = (1000.0-height)/2.0; |
| 31 | |
| 32 | // Draw a rectangle... |
| 33 | gc.new_path(); |
| 34 | gc.rect(0.0, y_pos, 1000.0, y_pos+height); |
| 35 | |
| 36 | // Fill with the texture we just loaded |
| 37 | gc.fill_texture(TextureId(0), 0.0, y_pos+height as f32, 1000.0, y_pos); |
| 38 | gc.fill(); |
| 39 | }); |
| 40 | }); |
| 41 | } |
nothing calls this directly
no test coverage detected