| 62 | } |
| 63 | |
| 64 | fn dance(frame: &mut Frame, area: Rect, elapsed: Duration) { |
| 65 | if area.width < 4 || area.height < 3 { |
| 66 | return; |
| 67 | } |
| 68 | let lines: Vec<_> = ART.lines().collect(); |
| 69 | let source_width = lines.iter().map(|line| line.len()).max().unwrap_or(1); |
| 70 | let scale = ((area.width.saturating_sub(6)) as f64 / source_width as f64) |
| 71 | .min(area.height.saturating_sub(4) as f64 / lines.len() as f64) |
| 72 | .clamp(0.01, 1.0); |
| 73 | let width = (source_width as f64 * scale).max(1.0) as usize; |
| 74 | let height = (lines.len() as f64 * scale).max(1.0) as usize; |
| 75 | let t = elapsed.as_secs_f64() * std::f64::consts::TAU / 2.4; |
| 76 | let origin_x = i32::from(area.x) + (i32::from(area.width) - width as i32) / 2; |
| 77 | let origin_y = i32::from(area.y) + (i32::from(area.height) - height as i32) / 2; |
| 78 | for y in 0..height { |
| 79 | let source_y = ((y as f64 / scale) as usize).min(lines.len() - 1); |
| 80 | let line = lines[source_y].as_bytes(); |
| 81 | for x in 0..width { |
| 82 | let source_x = (x as f64 / scale) as usize; |
| 83 | let ch = char::from(*line.get(source_x).unwrap_or(&b' ')); |
| 84 | if ch == ' ' { |
| 85 | continue; |
| 86 | } |
| 87 | let nx = x as f64 / width as f64; |
| 88 | let ny = y as f64 / height as f64; |
| 89 | let leg = if nx < 0.5 { -1.0 } else { 1.0 }; |
| 90 | let feet = ((ny - 0.68) / 0.25).clamp(0.0, 1.0); |
| 91 | let hand = (-((nx - 0.16) / 0.18).powi(2) - ((ny - 0.62) / 0.15).powi(2)).exp() |
| 92 | - (-((nx - 0.85) / 0.18).powi(2) - ((ny - 0.62) / 0.15).powi(2)).exp(); |
| 93 | let dx = (t.sin() * 2.0 * (1.0 - ny) + feet * leg * t.sin()).round() as i32; |
| 94 | let dy = ((2.0 * t).cos() * 0.7 + hand * t.sin() * 2.0 + feet * leg * t.sin()).round() |
| 95 | as i32; |
| 96 | let px = origin_x + x as i32 + dx; |
| 97 | let py = origin_y + y as i32 + dy; |
| 98 | if px < i32::from(area.x) |
| 99 | || px >= i32::from(area.right()) |
| 100 | || py < i32::from(area.y) |
| 101 | || py >= i32::from(area.bottom()) |
| 102 | { |
| 103 | continue; |
| 104 | } |
| 105 | let color = if ny > 0.78 { |
| 106 | Color::LightMagenta |
| 107 | } else if ny > 0.48 && !(0.28..=0.73).contains(&nx) { |
| 108 | Color::White |
| 109 | } else { |
| 110 | Color::Yellow |
| 111 | }; |
| 112 | frame.buffer_mut()[(px as u16, py as u16)] |
| 113 | .set_char(ch) |
| 114 | .set_fg(color); |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | struct Screen(Terminal<CrosstermBackend<std::io::Stderr>>); |
| 120 | |