Skip to content
This repository was archived by the owner on Jan 10, 2023. It is now read-only.

Commit a3e8b76

Browse files
committedSep 15, 2022
Add passing canvas into lib.
1 parent a1c6e05 commit a3e8b76

File tree

3 files changed

+46
-27
lines changed

3 files changed

+46
-27
lines changed
 
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"files.insertFinalNewline": true,
3+
"files.eol": "\n",
4+
"rust-analyzer.checkOnSave.enable": true,
5+
"rust-analyzer.checkOnSave.command": "clippy",
6+
"rust-analyzer.checkOnSave.extraArgs": [
7+
"--no-deps"
8+
],
9+
// Toggle this when you intend to work on WASM.
10+
// Fair warning, rust-analyzer rebuilds from scratch every time when enabled.
11+
"rust-analyzer.cargo.target": "wasm32-unknown-unknown",
12+
"task.allowAutomaticTasks": "auto"
13+
}

‎crates/head-wasm/src/lib.rs

+31-26
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,43 @@
11
use anyhow::Error;
22
use rrr_head::{platform::platform::time::Time, query};
3+
use std::rc::Rc;
34
use wasm_bindgen::prelude::*;
5+
use wasm_bindgen::JsCast;
6+
use web_sys::{Element, HtmlCanvasElement};
7+
use winit::platform::web::WindowBuilderExtWebSys;
8+
use winit::window::WindowBuilder;
49
use winit::{dpi::LogicalSize, event_loop::EventLoop, window::Window};
510

11+
pub fn build_window(
12+
event_loop: &EventLoop<()>,
13+
canvas: Option<HtmlCanvasElement>,
14+
screen_width: u32,
15+
screen_height: u32,
16+
) -> Result<winit::window::Window, winit::error::OsError> {
17+
{
18+
let size = LogicalSize::new(screen_width, screen_height);
19+
WindowBuilder::new()
20+
.with_title("Rust Rust Revolution")
21+
.with_canvas(canvas)
22+
.with_inner_size(size)
23+
.with_resizable(false)
24+
.build(event_loop)
25+
}
26+
}
27+
628
#[wasm_bindgen(start)]
729
pub fn initialize() {
830
console_log::init().unwrap();
931
log::info!("RRR loaded.");
1032
}
1133

1234
#[wasm_bindgen]
13-
pub fn play(width: u32, height: u32) {
35+
pub fn play(canvas: Option<HtmlCanvasElement>, width: u32, height: u32) {
1436
use wasm_bindgen::UnwrapThrowExt;
1537
std::panic::set_hook(Box::new(console_error_panic_hook::hook));
1638
wasm_bindgen_futures::spawn_local(async move {
1739
let event_loop = EventLoop::new();
18-
if let Ok(window) = initialize_window(width, height, &event_loop).await {
40+
if let Ok(window) = initialize_window(canvas, width, height, &event_loop).await {
1941
let extracted_settings: Option<query::SettingsMerge> =
2042
{ Some(query::get_optional_settings()) };
2143

@@ -28,16 +50,12 @@ pub fn play(width: u32, height: u32) {
2850
}
2951

3052
async fn initialize_window(
53+
canvas: Option<HtmlCanvasElement>,
3154
width: u32,
3255
height: u32,
3356
event_loop: &EventLoop<()>,
3457
) -> Result<Window, Error> {
35-
let window = rrr_head::build_window(&event_loop, width, height)?;
36-
37-
use std::rc::Rc;
38-
use wasm_bindgen::JsCast;
39-
use web_sys::{Element, HtmlCanvasElement};
40-
use winit::platform::web::WindowExtWebSys;
58+
let window = build_window(&event_loop, canvas.clone(), width, height)?;
4159

4260
// Initialize winit window with current dimensions of browser client
4361
window.set_inner_size(LogicalSize::new(width, height));
@@ -74,24 +92,11 @@ async fn initialize_window(
7492
}
7593
}) as Box<dyn FnMut(web_sys::FocusEvent)>);
7694

77-
// Attach winit canvas to body element
78-
web_sys::window()
79-
.and_then(|win| win.document())
80-
.and_then(|doc| doc.get_element_by_id("canvas"))
81-
.and_then(|canvas_div: Element| {
82-
let canvas: HtmlCanvasElement = window.canvas();
83-
canvas.set_class_name("game");
84-
canvas.set_id("rrr");
85-
canvas.set_width(width);
86-
canvas.set_height(height);
87-
let res = canvas_div
88-
.append_child(&web_sys::Element::from(window.canvas()))
89-
.ok();
90-
canvas.set_onblur(Some(onblur.as_ref().unchecked_ref()));
91-
canvas.set_tab_index(1);
92-
canvas.focus().ok();
93-
res
94-
});
95+
if let Some(canvas) = canvas {
96+
canvas.set_onblur(Some(onblur.as_ref().unchecked_ref()));
97+
canvas.set_tab_index(1);
98+
canvas.focus().ok();
99+
}
95100

96101
onblur.forget();
97102
}

‎web/index.html

+2-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
</div>
4343

4444
<div id="canvas">
45+
<canvas class="game" id="rrr" width="512" height="720"></canvas>
4546
<div class="progress">
4647
<div class="progress-inner">
4748
<p id="progress">Press Space to Start!</p>
@@ -90,7 +91,7 @@
9091
console.log("Post Edit Engine: ", engine.toJSON().toJSON());
9192

9293
// Launch the game
93-
play(512, 720);
94+
play(document.getElementById("rrr"), 512, 720);
9495
}
9596

9697
run();

0 commit comments

Comments
 (0)