Skip to content

Commit 08fd4ff

Browse files
jprochazkhacknus
authored andcommitted
Use canvas directly (emilk#4780)
1 parent 702a30b commit 08fd4ff

File tree

10 files changed

+22
-27
lines changed

10 files changed

+22
-27
lines changed

crates/eframe/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,10 @@
8585
//!
8686
//! /// Call this once from JavaScript to start your app.
8787
//! #[wasm_bindgen]
88-
//! pub async fn start(&self, canvas_id: &str) -> Result<(), wasm_bindgen::JsValue> {
88+
//! pub async fn start(&self, canvas: web_sys::HtmlCanvasElement) -> Result<(), wasm_bindgen::JsValue> {
8989
//! self.runner
9090
//! .start(
91-
//! canvas_id,
91+
//! canvas,
9292
//! eframe::WebOptions::default(),
9393
//! Box::new(|cc| Ok(Box::new(MyEguiApp::new(cc))),)
9494
//! )

crates/eframe/src/web/app_runner.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ impl AppRunner {
3131
/// # Errors
3232
/// Failure to initialize WebGL renderer, or failure to create app.
3333
pub async fn new(
34-
canvas_id: &str,
34+
canvas: web_sys::HtmlCanvasElement,
3535
web_options: crate::WebOptions,
3636
app_creator: epi::AppCreator,
3737
text_agent: TextAgent,
3838
) -> Result<Self, String> {
39-
let painter = super::ActiveWebPainter::new(canvas_id, &web_options).await?;
39+
let painter = super::ActiveWebPainter::new(canvas, &web_options).await?;
4040

4141
let system_theme = if web_options.follow_system_theme {
4242
super::system_theme()

crates/eframe/src/web/mod.rs

-11
Original file line numberDiff line numberDiff line change
@@ -122,17 +122,6 @@ fn theme_from_dark_mode(dark_mode: bool) -> Theme {
122122
}
123123
}
124124

125-
fn get_canvas_element_by_id(canvas_id: &str) -> Option<web_sys::HtmlCanvasElement> {
126-
let document = web_sys::window()?.document()?;
127-
let canvas = document.get_element_by_id(canvas_id)?;
128-
canvas.dyn_into::<web_sys::HtmlCanvasElement>().ok()
129-
}
130-
131-
fn get_canvas_element_by_id_or_die(canvas_id: &str) -> web_sys::HtmlCanvasElement {
132-
get_canvas_element_by_id(canvas_id)
133-
.unwrap_or_else(|| panic!("Failed to find canvas with id {canvas_id:?}"))
134-
}
135-
136125
/// Returns the canvas in client coordinates.
137126
fn canvas_content_rect(canvas: &web_sys::HtmlCanvasElement) -> egui::Rect {
138127
let bounding_rect = canvas.get_bounding_client_rect();

crates/eframe/src/web/web_painter.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use wasm_bindgen::JsValue;
55
/// therefore this trait is merely there for specifying and documenting the interface.
66
pub(crate) trait WebPainter {
77
// Create a new web painter targeting a given canvas.
8-
// fn new(canvas_id: &str, options: &WebOptions) -> Result<Self, String>
8+
// fn new(canvas: HtmlCanvasElement, options: &WebOptions) -> Result<Self, String>
99
// where
1010
// Self: Sized;
1111

crates/eframe/src/web/web_painter_glow.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ impl WebPainterGlow {
1818
self.painter.gl()
1919
}
2020

21-
pub async fn new(canvas_id: &str, options: &WebOptions) -> Result<Self, String> {
22-
let canvas = super::get_canvas_element_by_id_or_die(canvas_id);
23-
21+
pub async fn new(canvas: HtmlCanvasElement, options: &WebOptions) -> Result<Self, String> {
2422
let (gl, shader_prefix) =
2523
init_glow_context_from_canvas(&canvas, options.webgl_context_option)?;
2624
#[allow(clippy::arc_with_non_send_sync)]

crates/eframe/src/web/web_painter_wgpu.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,10 @@ impl WebPainterWgpu {
8383
}
8484

8585
#[allow(unused)] // only used if `wgpu` is the only active feature.
86-
pub async fn new(canvas_id: &str, options: &WebOptions) -> Result<Self, String> {
86+
pub async fn new(
87+
canvas: web_sys::HtmlCanvasElement,
88+
options: &WebOptions,
89+
) -> Result<Self, String> {
8790
log::debug!("Creating wgpu painter");
8891

8992
let mut backends = options.wgpu_options.supported_backends;
@@ -162,7 +165,6 @@ impl WebPainterWgpu {
162165
}
163166
}
164167

165-
let canvas = super::get_canvas_element_by_id_or_die(canvas_id);
166168
let surface = instance
167169
.create_surface(wgpu::SurfaceTarget::Canvas(canvas.clone()))
168170
.map_err(|err| format!("failed to create wgpu surface: {err}"))?;

crates/eframe/src/web/web_runner.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ impl WebRunner {
5757
/// Failing to initialize graphics, or failure to create app.
5858
pub async fn start(
5959
&self,
60-
canvas_id: &str,
60+
canvas: web_sys::HtmlCanvasElement,
6161
web_options: crate::WebOptions,
6262
app_creator: epi::AppCreator,
6363
) -> Result<(), JsValue> {
@@ -67,7 +67,7 @@ impl WebRunner {
6767

6868
let text_agent = TextAgent::attach(self)?;
6969

70-
let runner = AppRunner::new(canvas_id, web_options, app_creator, text_agent).await?;
70+
let runner = AppRunner::new(canvas, web_options, app_creator, text_agent).await?;
7171

7272
{
7373
// Make sure the canvas can be given focus.

crates/egui_demo_app/src/web.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,13 @@ impl WebHandle {
3232

3333
/// Call this once from JavaScript to start your app.
3434
#[wasm_bindgen]
35-
pub async fn start(&self, canvas_id: &str) -> Result<(), wasm_bindgen::JsValue> {
35+
pub async fn start(
36+
&self,
37+
canvas: web_sys::HtmlCanvasElement,
38+
) -> Result<(), wasm_bindgen::JsValue> {
3639
self.runner
3740
.start(
38-
canvas_id,
41+
canvas,
3942
eframe::WebOptions::default(),
4043
Box::new(|cc| Ok(Box::new(WrapApp::new(cc)))),
4144
)

web_demo/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@
166166

167167
check_for_panic();
168168

169-
handle.start("the_canvas_id").then(on_app_started).catch(on_error);
169+
handle.start(document.getElementById("the_canvas_id")).then(on_app_started).catch(on_error);
170170
}
171171

172172
function on_app_started(handle) {

web_demo/multiple_apps.html

+4-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,10 @@
138138
const handle_one = new wasm_bindgen.WebHandle();
139139
const handle_two = new wasm_bindgen.WebHandle();
140140

141-
Promise.all([handle_one.start("canvas_id_one"), handle_two.start("canvas_id_two")]).then((handles) => {
141+
Promise.all([
142+
handle_one.start(document.getElementById("canvas_id_one")),
143+
handle_two.start(document.getElementById("canvas_id_two")),
144+
]).then((handles) => {
142145
on_apps_started(handle_one, handle_two)
143146
}).catch(on_error);
144147
}

0 commit comments

Comments
 (0)