-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathanimation.rs
103 lines (88 loc) · 3.37 KB
/
animation.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
use instant::Instant;
#[cfg(not(target_arch = "wasm32"))]
use rayon::prelude::*;
use std::f64::consts::PI;
use std::num::NonZeroU32;
use winit::event::{Event, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::window::WindowBuilder;
fn main() {
let event_loop = EventLoop::new();
let window = WindowBuilder::new().build(&event_loop).unwrap();
#[cfg(target_arch = "wasm32")]
{
use winit::platform::web::WindowExtWebSys;
web_sys::window()
.unwrap()
.document()
.unwrap()
.body()
.unwrap()
.append_child(&window.canvas())
.unwrap();
}
let context = unsafe { softbuffer::Context::new(&window) }.unwrap();
let mut surface = unsafe { softbuffer::Surface::new(&context, &window) }.unwrap();
let mut old_size = (0, 0);
let mut frames = pre_render_frames(0, 0);
let start = Instant::now();
event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Poll;
match event {
Event::RedrawRequested(window_id) if window_id == window.id() => {
let elapsed = start.elapsed().as_secs_f64() % 1.0;
let (width, height) = {
let size = window.inner_size();
(size.width, size.height)
};
if (width, height) != old_size {
old_size = (width, height);
frames = pre_render_frames(width as usize, height as usize);
};
let frame = &frames[((elapsed * 60.0).round() as usize).clamp(0, 59)];
surface
.resize(
NonZeroU32::new(width).unwrap(),
NonZeroU32::new(height).unwrap(),
)
.unwrap();
let mut buffer = surface.buffer_mut().unwrap();
buffer.copy_from_slice(frame);
buffer.present().unwrap();
}
Event::MainEventsCleared => {
window.request_redraw();
}
Event::WindowEvent {
event: WindowEvent::CloseRequested,
window_id,
} if window_id == window.id() => {
*control_flow = ControlFlow::Exit;
}
_ => {}
}
});
}
fn pre_render_frames(width: usize, height: usize) -> Vec<Vec<u32>> {
let render = |frame_id| {
let elapsed = ((frame_id as f64) / (60.0)) * 2.0 * PI;
let coords = (0..height).flat_map(|x| (0..width).map(move |y| (x, y)));
coords
.map(|(x, y)| {
let y = (y as f64) / (height as f64);
let x = (x as f64) / (width as f64);
let red =
((((y + elapsed).sin() * 0.5 + 0.5) * 255.0).round() as u32).clamp(0, 255);
let green =
((((x + elapsed).sin() * 0.5 + 0.5) * 255.0).round() as u32).clamp(0, 255);
let blue =
((((y - elapsed).cos() * 0.5 + 0.5) * 255.0).round() as u32).clamp(0, 255);
blue | (green << 8) | (red << 16)
})
.collect::<Vec<_>>()
};
#[cfg(target_arch = "wasm32")]
return (0..60).map(render).collect();
#[cfg(not(target_arch = "wasm32"))]
(0..60).into_par_iter().map(render).collect()
}