use std::error::Error;
use std::f32::consts::{PI, TAU};
use emblema::{
BackendPreference, BlendMode, Canvas, Color, Context, Dash, Extent2D, GradientStop, Layer,
LineCap, LineJoin, Paint, Path, PathBuilder, PixelFormat, PointMode, Rect, StrokeStyle, Style,
Surface, Vec2,
};
const WIDTH: u32 = 1280;
const HEIGHT: u32 = 640;
const SIZE: Extent2D = Extent2D {
width: WIDTH,
height: HEIGHT,
};
type Outcome<T = ()> = Result<T, Box<dyn Error>>;
const MAX_FRAMES: u32 = 1000;
fn main() {
let mut args = std::env::args().skip(1);
let path = args.next().unwrap_or_else(|| "showcase.ppm".into());
let frames = match args.next().map(|n| n.parse::<u32>()) {
None => None,
Some(Ok(n)) if (1..=MAX_FRAMES).contains(&n) => Some(n),
Some(_) => {
eprintln!("showcase: the frame count must be a whole number from 1 to {MAX_FRAMES}");
std::process::exit(2);
}
};
if let Err(e) = run(&path, frames) {
eprintln!("showcase: {e}");
std::process::exit(1);
}
}
fn run(path: &str, frames: Option<u32>) -> Outcome {
let mut ctx = Context::new(BackendPreference::Auto)
.map_err(|e| format!("no usable rendering backend: {e}"))?;
eprintln!("{:?} on {}", ctx.backend(), ctx.capabilities().device_name);
let mut surface = ctx.create_surface(SIZE, PixelFormat::Rgba8Unorm)?;
let drawn = render_all(&mut ctx, &mut surface, path, frames);
ctx.destroy_surface(surface);
drawn
}
fn render_all(
ctx: &mut Context,
surface: &mut Surface,
path: &str,
frames: Option<u32>,
) -> Outcome {
let Some(count) = frames else {
return render(ctx, surface, 0.0, path);
};
let stem = path.strip_suffix(".ppm").unwrap_or(path);
for frame in 0..count {
let phase = frame as f32 / count as f32;
render(ctx, surface, phase, &format!("{stem}-{frame:03}.ppm"))?;
}
Ok(())
}
fn render(ctx: &mut Context, surface: &mut Surface, phase: f32, path: &str) -> Outcome {
let recording = compose(phase)?;
ctx.draw(surface, &recording)?;
let pixels = ctx.read(surface)?;
write_ppm(path, SIZE, &pixels).map_err(|e| format!("writing {path}: {e}"))?;
eprintln!("wrote {path}");
Ok(())
}
fn wobble(phase: f32, offset: f32, amplitude: f32) -> f32 {
amplitude * ((TAU * phase + offset).sin() - offset.sin())
}
fn rgb(hex: u32) -> Color {
rgba(hex, 1.0)
}
fn rgba(hex: u32, alpha: f32) -> Color {
let c = |shift: u32| ((hex >> shift) & 0xff) as f32 / 255.0;
Color::srgb(c(16), c(8), c(0), alpha)
}
fn stops(colors: &[(u32, f32)]) -> Vec<GradientStop> {
colors
.iter()
.map(|&(hex, at)| GradientStop::new(rgb(hex), at))
.collect()
}
fn stroke(paint: Paint, width: f32) -> Paint {
paint.with_style(Style::Stroke(
StrokeStyle::new(width)
.with_cap(LineCap::Round)
.with_join(LineJoin::Round),
))
}
fn compose(phase: f32) -> Outcome<emblema::Recording> {
let mut canvas = Canvas::new(SIZE);
let (w, h) = (WIDTH as f32, HEIGHT as f32);
canvas.clear(rgb(0x0b1026));
canvas.draw_rect(
Rect::from_size(w, h),
&Paint::linear_gradient(
Vec2::new(0.0, 0.0),
Vec2::new(w, h),
stops(&[(0x0b1026, 0.0), (0x1a1446, 0.55), (0x2b0f3a, 1.0)]),
),
)?;
canvas.save_layer(Layer::opacity(0.85).with_blur(60.0));
for (i, (center, radius, hex)) in [
(Vec2::new(250.0, 170.0), 170.0, 0xff3d8b),
(Vec2::new(620.0, 520.0), 200.0, 0x3d7bff),
(Vec2::new(1010.0, 140.0), 180.0, 0x19d3c5),
(Vec2::new(1150.0, 560.0), 120.0, 0xffa53d),
]
.into_iter()
.enumerate()
{
let k = i as f32 * 1.7;
let drift = Vec2::new(
center.x + wobble(phase, k, 40.0),
center.y + wobble(phase, k + 1.3, 30.0),
);
canvas.draw_circle(drift, radius, &Paint::fill(rgb(hex)))?;
}
canvas.restore();
let grid: Vec<Vec2> = (0..HEIGHT / 32)
.flat_map(|row| (0..WIDTH / 32).map(move |col| (row, col)))
.map(|(row, col)| Vec2::new(20.0 + col as f32 * 32.0, 20.0 + row as f32 * 32.0))
.collect();
canvas.draw_points(
PointMode::Points,
&grid,
&stroke(Paint::fill(rgba(0xffffff, 0.08)), 2.4),
)?;
let ring = Rect::new(60.0, 80.0, 400.0, 560.0);
let chart = Rect::new(440.0, 80.0, 900.0, 400.0);
let tiles = Rect::new(440.0, 440.0, 900.0, 560.0);
let side = Rect::new(940.0, 80.0, 1220.0, 560.0);
for panel in [ring, chart, tiles, side] {
glass(&mut canvas, panel)?;
}
progress_ring(&mut canvas, ring, phase)?;
line_chart(&mut canvas, chart, phase)?;
bars(&mut canvas, tiles, phase)?;
controls(&mut canvas, side, phase)?;
Ok(canvas.finish())
}
fn glass(canvas: &mut Canvas, panel: Rect) -> Outcome {
let radius = 28.0;
canvas.draw_shadow(
&panel.to_rounded_path(radius),
rgba(0x000000, 0.6),
18.0,
true,
)?;
canvas.save();
canvas.clip_path(&panel.to_rounded_path(radius))?;
canvas.save_layer_bounds(Layer::opacity(1.0).with_backdrop_blur(24.0), panel);
canvas.draw_rect(
panel,
&Paint::linear_gradient(
Vec2::new(panel.left, panel.top),
Vec2::new(panel.right, panel.bottom),
vec![
GradientStop::new(rgba(0xffffff, 0.16), 0.0),
GradientStop::new(rgba(0xffffff, 0.05), 1.0),
],
),
)?;
canvas.restore();
canvas.restore();
canvas.draw_rrect(panel, radius, &Paint::stroke(rgba(0xffffff, 0.22), 1.5))?;
Ok(())
}
fn arc_path(center: Vec2, radius: f32, start: f32, sweep: f32) -> Path {
let mut b = PathBuilder::new();
b.move_to(Vec2::new(
center.x + radius * start.cos(),
center.y + radius * start.sin(),
));
b.arc(center, Vec2::new(radius, radius), start, sweep);
b.build()
}
fn progress_ring(canvas: &mut Canvas, panel: Rect, phase: f32) -> Outcome {
let center = Vec2::new((panel.left + panel.right) / 2.0, panel.top + 190.0);
let radius = 110.0;
let start = -PI / 2.0;
let sweep = TAU * (0.72 + wobble(phase, 0.0, 0.16));
canvas.draw_circle(
center,
radius,
&stroke(Paint::fill(rgba(0xffffff, 0.10)), 22.0),
)?;
let lead = 0.2;
canvas.save();
canvas.translate(center.x, center.y);
canvas.rotate(start - lead);
let origin = Vec2::new(0.0, 0.0);
let gradient = Paint::sweep_gradient(
origin,
0.0,
lead + sweep,
stops(&[
(0x19d3c5, 0.0),
(0x3d7bff, 0.45),
(0xb54dff, 0.8),
(0xff3d8b, 1.0),
]),
);
canvas.draw_path(
&arc_path(origin, radius, lead, sweep),
&stroke(gradient, 22.0),
)?;
let tip = Vec2::new(radius * (lead + sweep).cos(), radius * (lead + sweep).sin());
canvas.save_layer(Layer::opacity(0.9).with_blur(10.0));
canvas.draw_circle(tip, 18.0, &Paint::fill(rgb(0xff3d8b)))?;
canvas.restore();
canvas.draw_circle(tip, 8.0, &Paint::fill(rgb(0xffffff)))?;
canvas.restore();
canvas.draw_circle(
center,
70.0,
&Paint::radial_gradient(
Vec2::new(center.x - 20.0, center.y - 25.0),
90.0,
vec![
GradientStop::new(rgba(0xffffff, 0.14), 0.0),
GradientStop::new(rgba(0xffffff, 0.02), 1.0),
],
),
)?;
for (i, hex) in [0x19d3c5u32, 0x3d7bff, 0xff3d8b].into_iter().enumerate() {
let top = panel.top + 350.0 + i as f32 * 36.0;
canvas.draw_rrect(
Rect::new(panel.left + 40.0, top, panel.left + 60.0, top + 20.0),
6.0,
&Paint::fill(rgb(hex)),
)?;
let len = [170.0, 120.0, 200.0][i];
canvas.draw_rrect(
Rect::new(
panel.left + 76.0,
top + 5.0,
panel.left + 76.0 + len,
top + 15.0,
),
5.0,
&Paint::fill(rgba(0xffffff, 0.25)),
)?;
}
Ok(())
}
fn line_chart(canvas: &mut Canvas, panel: Rect, phase: f32) -> Outcome {
let inner = Rect::new(
panel.left + 36.0,
panel.top + 40.0,
panel.right - 36.0,
panel.bottom - 36.0,
);
for i in 0..4 {
let y = inner.top + i as f32 * inner.height() / 3.0;
let mut b = PathBuilder::new();
b.move_to(Vec2::new(inner.left, y));
b.line_to(Vec2::new(inner.right, y));
canvas.draw_path(
&b.build(),
&Paint::stroke(rgba(0xffffff, 0.14), 1.0).with_dash(Dash::new(vec![6.0, 6.0], 0.0)),
)?;
}
let values = [0.35f32, 0.55, 0.42, 0.78, 0.60, 0.92, 0.70, 0.84];
let points: Vec<Vec2> = values
.iter()
.enumerate()
.map(|(i, v)| {
let v = (v + wobble(phase, i as f32 * 0.9, 0.08)).clamp(0.05, 0.98);
Vec2::new(
inner.left + i as f32 * inner.width() / (values.len() - 1) as f32,
inner.bottom - v * inner.height(),
)
})
.collect();
let curve = |b: &mut PathBuilder| {
for pair in points.windows(2) {
let (a, c) = (pair[0], pair[1]);
let mid = (c.x - a.x) / 2.0;
b.cubic_to(Vec2::new(a.x + mid, a.y), Vec2::new(c.x - mid, c.y), c);
}
};
let mut area = PathBuilder::new();
area.move_to(Vec2::new(inner.left, inner.bottom));
area.line_to(points[0]);
curve(&mut area);
area.line_to(Vec2::new(inner.right, inner.bottom));
area.close();
canvas.draw_path(
&area.build(),
&Paint::linear_gradient(
Vec2::new(0.0, inner.top),
Vec2::new(0.0, inner.bottom),
vec![
GradientStop::new(rgba(0x19d3c5, 0.45), 0.0),
GradientStop::new(rgba(0x3d7bff, 0.0), 1.0),
],
),
)?;
let mut line = PathBuilder::new();
line.move_to(points[0]);
curve(&mut line);
let line = line.build();
let line_paint = Paint::linear_gradient(
Vec2::new(inner.left, 0.0),
Vec2::new(inner.right, 0.0),
stops(&[(0x19d3c5, 0.0), (0x3d7bff, 0.5), (0xff3d8b, 1.0)]),
);
canvas.save_layer(Layer::opacity(0.8).with_blur(8.0));
canvas.draw_path(&line, &stroke(line_paint.clone(), 10.0))?;
canvas.restore();
canvas.draw_path(&line, &stroke(line_paint, 4.0))?;
for p in &points {
canvas.draw_circle(*p, 6.0, &Paint::fill(rgb(0x0b1026)))?;
canvas.draw_circle(*p, 6.0, &Paint::stroke(rgb(0xffffff), 2.5))?;
}
Ok(())
}
fn bars(canvas: &mut Canvas, panel: Rect, phase: f32) -> Outcome {
let heights = [0.45f32, 0.7, 0.55, 0.9, 0.6, 0.8, 0.4, 0.65, 0.75, 0.5];
let gap = 14.0;
let left = panel.left + 32.0;
let span = panel.width() - 64.0;
let width = (span - gap * (heights.len() - 1) as f32) / heights.len() as f32;
let bottom = panel.bottom - 24.0;
let tall = panel.height() - 48.0;
for (i, v) in heights.iter().enumerate() {
let x = left + i as f32 * (width + gap);
let v = (v + wobble(phase, i as f32 * 0.7, 0.15)).clamp(0.15, 1.0);
let bar = Rect::new(x, bottom - v * tall, x + width, bottom);
let (from, to) = if i % 2 == 0 {
(0xb54dff, 0x3d7bff)
} else {
(0xff3d8b, 0xffa53d)
};
canvas.draw_rrect(
bar,
width / 2.0,
&Paint::linear_gradient(
Vec2::new(0.0, bar.top),
Vec2::new(0.0, bar.bottom),
stops(&[(from, 0.0), (to, 1.0)]),
),
)?;
}
Ok(())
}
fn controls(canvas: &mut Canvas, panel: Rect, phase: f32) -> Outcome {
let center = Vec2::new((panel.left + panel.right) / 2.0, panel.top + 120.0);
let radius = 72.0;
let segments = [
(0.38f32, 0xff3d8b),
(0.27, 0xffa53d),
(0.21, 0x19d3c5),
(0.14, 0x3d7bff),
];
let mut at = -PI / 2.0 + TAU * phase;
for (share, hex) in segments {
let sweep = share * TAU - 0.12;
canvas.draw_path(
&arc_path(center, radius, at + 0.06, sweep),
&Paint::stroke(rgb(hex), 26.0),
)?;
at += share * TAU;
}
for (i, on) in [true, false, true].into_iter().enumerate() {
let top = panel.top + 250.0 + i as f32 * 56.0;
let track = Rect::new(panel.right - 104.0, top, panel.right - 40.0, top + 32.0);
let paint = if on {
Paint::linear_gradient(
Vec2::new(track.left, 0.0),
Vec2::new(track.right, 0.0),
stops(&[(0xb54dff, 0.0), (0xff3d8b, 1.0)]),
)
} else {
Paint::fill(rgba(0xffffff, 0.15))
};
canvas.draw_rrect(track, 16.0, &paint)?;
let knob_x = if on {
track.right - 16.0
} else {
track.left + 16.0
};
canvas.draw_circle(
Vec2::new(knob_x, top + 16.0),
12.0,
&Paint::fill(rgb(0xffffff)),
)?;
let len = [110.0, 80.0, 130.0][i];
canvas.draw_rrect(
Rect::new(
panel.left + 40.0,
top + 11.0,
panel.left + 40.0 + len,
top + 21.0,
),
5.0,
&Paint::fill(rgba(0xffffff, 0.25)),
)?;
}
let y = panel.bottom - 56.0;
let track = Rect::new(panel.left + 40.0, y - 4.0, panel.right - 40.0, y + 4.0);
canvas.draw_rrect(track, 4.0, &Paint::fill(rgba(0xffffff, 0.15)))?;
let knob = track.left + track.width() * (0.62 + wobble(phase, 0.0, 0.2));
canvas.draw_rrect(
Rect::new(track.left, track.top, knob, track.bottom),
4.0,
&Paint::linear_gradient(
Vec2::new(track.left, 0.0),
Vec2::new(knob, 0.0),
stops(&[(0x19d3c5, 0.0), (0x3d7bff, 1.0)]),
),
)?;
let knob_rect = Rect::new(knob - 14.0, y - 14.0, knob + 14.0, y + 14.0);
canvas.draw_shadow(
&knob_rect.to_rounded_path(14.0),
rgba(0x000000, 0.8),
6.0,
false,
)?;
canvas.draw_circle(Vec2::new(knob, y), 14.0, &Paint::fill(rgb(0xffffff)))?;
canvas.draw_circle(
Vec2::new(panel.right - 46.0, panel.top + 40.0),
22.0,
&Paint::radial_gradient(
Vec2::new(panel.right - 46.0, panel.top + 40.0),
22.0,
vec![
GradientStop::new(rgba(0xffffff, 0.9), 0.0),
GradientStop::new(rgba(0xffffff, 0.0), 1.0),
],
)
.with_blend(BlendMode::Plus),
)?;
Ok(())
}
fn write_ppm(path: &str, extent: Extent2D, pixels: &[u8]) -> std::io::Result<()> {
use std::io::Write as _;
let mut out = std::io::BufWriter::new(std::fs::File::create(path)?);
write!(out, "P6\n{} {}\n255\n", extent.width, extent.height)?;
let mut rgb = Vec::with_capacity(pixels.len() / 4 * 3);
for texel in pixels.chunks_exact(4) {
rgb.extend_from_slice(&texel[..3]);
}
out.write_all(&rgb)?;
out.flush()
}