egui/example_wasm/src/lib.rs

60 lines
2.1 KiB
Rust
Raw Normal View History

2019-02-09 22:00:07 +00:00
#![deny(warnings)]
2020-05-10 17:04:10 +00:00
#![warn(clippy::all)]
use egui::{label, widgets::Separator, Align, TextStyle, *};
2019-02-09 22:00:07 +00:00
use wasm_bindgen::prelude::*;
2020-04-12 10:07:51 +00:00
// ----------------------------------------------------------------------------
/// This is the entry-point for all the web-assembly.
#[wasm_bindgen]
pub fn start(canvas_id: &str) -> Result<(), JsValue> {
let app = Box::new(MyApp::default());
let runner = egui_web::AppRunner::new(canvas_id, app)?;
egui_web::run(runner)?;
Ok(())
}
// ----------------------------------------------------------------------------
#[derive(Default)]
pub struct MyApp {
example_app: egui::examples::ExampleApp,
2019-02-09 22:00:07 +00:00
}
impl egui_web::App for MyApp {
fn ui(&mut self, ui: &mut egui::Ui, info: &egui_web::BackendInfo) {
self.example_app.ui(ui, &info.web_location_hash);
2019-02-09 22:00:07 +00:00
let mut ui = ui.centered_column(ui.available().width().min(480.0));
ui.set_layout(Layout::vertical(Align::Min));
ui.add(label!("Egui!").text_style(TextStyle::Heading));
ui.label("Egui is an immediate mode GUI written in Rust, compiled to WebAssembly, rendered with WebGL.");
2020-05-30 07:51:57 +00:00
ui.label(
"Everything you see is rendered as textured triangles. There is no DOM. There are no HTML elements."
2020-04-20 21:33:16 +00:00
);
2020-05-30 07:51:57 +00:00
ui.label("This is not JavaScript. This is Rust, running at 60 FPS. This is the web page, reinvented with game tech.");
ui.label("This is also work in progress, and not ready for production... yet :)");
2020-05-08 20:42:31 +00:00
ui.horizontal(|ui| {
2020-05-30 07:51:57 +00:00
ui.label("Project home page:");
ui.hyperlink("https://github.com/emilk/emigui/");
2020-04-23 17:15:17 +00:00
});
2020-05-08 20:42:31 +00:00
ui.add(Separator::new());
2020-04-12 10:07:51 +00:00
2020-05-30 07:51:57 +00:00
ui.label("WebGl painter info:");
2020-05-08 20:42:31 +00:00
ui.indent("webgl region id", |ui| {
ui.label(&info.painter_debug_info);
2019-02-09 22:00:07 +00:00
});
2020-05-08 20:42:31 +00:00
ui.add(
label!(
2020-05-03 11:28:47 +00:00
"CPU usage: {:.2} ms (excludes painting)",
1e3 * info.cpu_time
)
.text_style(TextStyle::Monospace),
2020-04-12 10:07:51 +00:00
);
ui.add(label!("FPS: {:.1}", info.fps).text_style(TextStyle::Monospace));
2019-02-09 22:00:07 +00:00
}
}