-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathanimation.rs
More file actions
120 lines (98 loc) · 3.9 KB
/
animation.rs
File metadata and controls
120 lines (98 loc) · 3.9 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#[cfg(not(target_family = "wasm"))]
use rayon::prelude::*;
use softbuffer::{Context, Pixel, Surface};
use std::f64::consts::PI;
use std::num::NonZeroU32;
use web_time::Instant;
use winit::event::{KeyEvent, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::keyboard::{Key, NamedKey};
mod util;
fn main() {
util::setup();
let event_loop = EventLoop::new().unwrap();
let start = Instant::now();
let context = Context::new(event_loop.owned_display_handle()).unwrap();
let app = util::WinitAppBuilder::with_init(
|event_loop| {
let window = util::make_window(event_loop, |w| w);
let old_size = (0, 0);
let frames = pre_render_frames(0, 0, 0);
(window, old_size, frames)
},
move |_elwft, (window, _old_size, _frames)| Surface::new(&context, window.clone()).unwrap(),
)
.with_event_handler(move |state, surface, window_id, event, elwt| {
let (window, old_size, frames) = state;
elwt.set_control_flow(ControlFlow::Poll);
if window_id != window.id() {
return;
}
match event {
WindowEvent::Resized(size) => {
let Some(surface) = surface else {
tracing::error!("Resized fired before Resumed or after Suspended");
return;
};
if let (Some(width), Some(height)) =
(NonZeroU32::new(size.width), NonZeroU32::new(size.height))
{
surface.resize(width, height).unwrap();
}
}
WindowEvent::RedrawRequested => {
let Some(surface) = surface else {
tracing::error!("RedrawRequested fired before Resumed or after Suspended");
return;
};
let elapsed = start.elapsed().as_secs_f64() % 1.0;
let mut buffer = surface.buffer_mut().unwrap();
let size = (buffer.width().get(), buffer.height().get());
if size != *old_size {
*old_size = size;
*frames = pre_render_frames(buffer.byte_stride().get() / 4, size.0, size.1);
}
let frame = &frames[((elapsed * 60.0).round() as usize).clamp(0, 59)];
buffer.pixels().copy_from_slice(frame);
buffer.present().unwrap();
}
WindowEvent::CloseRequested
| WindowEvent::KeyboardInput {
event:
KeyEvent {
logical_key: Key::Named(NamedKey::Escape),
..
},
..
} => {
elwt.exit();
}
_ => {}
}
})
.with_about_to_wait_handler(|state, _, _| {
let (window, _, _) = state;
window.request_redraw();
});
util::run_app(event_loop, app);
}
fn pre_render_frames(stride: u32, width: u32, height: u32) -> Vec<Vec<Pixel>> {
let render = |frame_id| {
let elapsed = ((frame_id as f64) / (60.0)) * 2.0 * PI;
let coords = (0..height).flat_map(|x| (0..stride).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 r = ((((y + elapsed).sin() * 0.5 + 0.5) * 255.0).round() as u32).clamp(0, 255);
let g = ((((x + elapsed).sin() * 0.5 + 0.5) * 255.0).round() as u32).clamp(0, 255);
let b = ((((y - elapsed).cos() * 0.5 + 0.5) * 255.0).round() as u32).clamp(0, 255);
Pixel::new_rgb(r as u8, g as u8, b as u8)
})
.collect::<Vec<_>>()
};
#[cfg(target_family = "wasm")]
return (0..60).map(render).collect();
#[cfg(not(target_family = "wasm"))]
(0..60).into_par_iter().map(render).collect()
}