nitai/projects
noxstrap / src / crates / nox-gfx / examples / bench.rs
73 lines · 2.5 KB Raw
1use nox_gfx::canvas::{Canvas, Paint, Stroke};
2use nox_gfx::color::{hex, rgba};
3use nox_gfx::geom::{pt, Rect, IDENTITY};
4use nox_gfx::path::Path;
5use std::time::Instant;
6
7fn time(name: &str, n: usize, mut f: impl FnMut()) {
8 f();
9 let t0 = Instant::now();
10 for _ in 0..n {
11 f();
12 }
13 let el = t0.elapsed().as_secs_f64() * 1000.0 / n as f64;
14 println!("{:<28} {:>8.3} ms", name, el);
15}
16
17fn main() {
18 let mut c = Canvas::new(1100, 680);
19 let full = c.full();
20
21 time("clear", 200, || c.clear(hex(0x101018)));
22
23 let mut bg = Path::new();
24 bg.rrect(full, 14.0);
25 time("fullscreen rrect vgrad", 200, || {
26 c.fill(&bg, &IDENTITY, &Paint::vgrad(full, hex(0x141420), hex(0x0a0a10)))
27 });
28
29 let mut glow = Path::new();
30 glow.circle(pt(210.0, 90.0), 320.0);
31 time("radial glow r=320", 200, || {
32 c.fill(&glow, &IDENTITY, &Paint::glow(pt(210.0, 90.0), 320.0, rgba(124, 92, 255, 80), rgba(124, 92, 255, 0)))
33 });
34
35 let hero = Rect::xywh(256.0, 40.0, 808.0, 232.0);
36 let mut heropath = Path::new();
37 heropath.rrect(hero, 16.0);
38 time("card rrect fill", 200, || {
39 c.fill(&heropath, &IDENTITY, &Paint::vgrad(hero, hex(0x241f45), hex(0x141726)))
40 });
41 time("shadow blur=26 (hero)", 100, || {
42 c.shadow(&heropath, &IDENTITY, 26.0, rgba(0, 0, 8, 176), pt(0.0, 14.0))
43 });
44 c.push_clip(Rect::xywh(0.0, 0.0, 2.0, 2.0));
45 time(" shadow mask only (clipped)", 100, || {
46 c.shadow(&heropath, &IDENTITY, 26.0, rgba(0, 0, 8, 176), pt(0.0, 14.0))
47 });
48 c.pop_clip();
49 let small = Rect::xywh(14.0, 120.0, 196.0, 38.0);
50 let mut smallp = Path::new();
51 smallp.rrect(small, 10.0);
52 time("shadow blur=10 (nav)", 100, || {
53 c.shadow(&smallp, &IDENTITY, 10.0, rgba(124, 92, 255, 140), pt(0.0, 3.0))
54 });
55 time("blurred_layer rail s=18", 50, || {
56 let _ = c.blurred_layer(Rect::xywh(0.0, 0.0, 224.0, 680.0), 18.0);
57 });
58 time("stroke 1px card outline", 200, || {
59 c.stroke(&heropath, &IDENTITY, &Paint::Solid(rgba(255, 255, 255, 22)), &Stroke::new(1.0))
60 });
61 let mut wave = Path::new();
62 wave.move_to(pt(270.0, 620.0));
63 for i in 0..=60 {
64 let t = i as f32 / 60.0;
65 wave.line_to(pt(270.0 + t * 620.0, 600.0 - (t * 9.0).sin() * 26.0 - t * 18.0));
66 }
67 time("stroke wave 2.5px round", 200, || {
68 c.stroke(&wave, &IDENTITY, &Paint::Solid(rgba(53, 208, 255, 255)), &Stroke::round(2.5))
69 });
70 time("to_rgba", 100, || {
71 let _ = c.to_rgba();
72 });
73}