use std::thread; use std::time::Duration; extern crate rand; use rand::seq::SliceRandom; use rand::Rng; extern crate sdl2; use sdl2::event::Event; use sdl2::gfx::primitives::DrawRenderer; use sdl2::pixels::Color; #[cfg(target_os = "emscripten")] pub mod emscripten; fn find_origin() -> (i16, i16) { let mut rng = rand::thread_rng(); (rng.gen_range(100..400), rng.gen_range(100..400)) } struct Line { x1: i16, y1: i16, x2: i16, y2: i16, } struct Pixel { x: i16, y: i16, } fn main() { let context = sdl2::init().unwrap(); let video = context.video().unwrap(); let (height, width) = (500, 500); let window = video.window("web", height, width).opengl().build().unwrap(); let mut canvas = window.into_canvas().build().unwrap(); let mut events = context.event_pump().unwrap(); canvas.set_draw_color(Color::RGB(0, 0, 0)); canvas.clear(); canvas.present(); let (x, y) = find_origin(); let mut lines = Vec::new(); for i in (0..(height as i16 + 1)).step_by(50) { lines.push(Line { x1: i, y1: 0, x2: x, y2: y, }); lines.push(Line { x1: i, y1: height as i16, x2: x, y2: y, }); lines.push(Line { x1: 0, y1: i, x2: x, y2: y, }); lines.push(Line { x1: width as i16, y1: i, x2: x, y2: y, }); } let mut rng = rand::thread_rng(); lines.shuffle(&mut rng); let a = rng.gen_range(-7.0..7.0); let b = rng.gen_range(-7.0..7.0); let mut pixels = Vec::new(); for i in (1000..200000).step_by(10) { let i = i as f64 * 0.001; let x = (a + b * i) * i.cos() + x as f64; let y = (a + b * i) * i.sin() + y as f64; pixels.push(Pixel { x: x as i16, y: y as i16, }); } let mut main_loop = || { let mut i = 0; let mut run = true; while run { for event in events.poll_iter() { if let Event::Quit { .. } = event { run = false }; } if i < lines.len() { let line = &lines[i]; canvas .line( line.x1, line.y1, line.x2, line.y2, Color::RGB(255, 255, 255), ) .unwrap(); } if i < pixels.len() { let pixel = &pixels[i]; canvas .pixel(pixel.x, pixel.y, Color::RGB(255, 255, 255)) .unwrap(); } canvas.present(); thread::sleep(Duration::from_millis(10)); i += 1; } }; #[cfg(target_os = "emscripten")] use emscripten::emscripten; #[cfg(target_os = "emscripten")] emscripten::set_main_loop_callback(main_loop); #[cfg(not(target_os = "emscripten"))] main_loop(); }