From 60a49084af20ad8cb1cae765c8b33007e12ef54a Mon Sep 17 00:00:00 2001
From: Benign X <1341398182@qq.com>
Date: Tue, 12 Nov 2024 16:29:57 +0800
Subject: [PATCH] =?UTF-8?q?=F0=9F=98=81(wasm):=20update=20wasm=20target?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.github/workflows/rust.yml | 2 +-
Cargo.toml | 1 +
index.html | 72 +++++++++++++++++++++-----------------
rust-toolchain.toml | 3 ++
src/interfaces.rs | 1 +
src/main.rs | 42 ++++++++++++++++++----
6 files changed, 82 insertions(+), 39 deletions(-)
create mode 100644 rust-toolchain.toml
diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml
index 64d6e88..96f265d 100644
--- a/.github/workflows/rust.yml
+++ b/.github/workflows/rust.yml
@@ -96,7 +96,7 @@ jobs:
- uses: actions-rs/toolchain@v1
with:
profile: minimal
- toolchain: 1.72.0
+ toolchain: stable
target: wasm32-unknown-unknown
override: true
- name: Download and install Trunk binary
diff --git a/Cargo.toml b/Cargo.toml
index 93c901c..41f79bb 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -20,6 +20,7 @@ log = "0.4.19"
levenshtein = "*"
dyn-clone = "1.0.16"
+web-sys = "0.3.72"
# native:
[target.'cfg(all(not(target_arch = "wasm32"), platform = "macos"))'.dependencies]
diff --git a/index.html b/index.html
index b6b20a6..b45ec42 100644
--- a/index.html
+++ b/index.html
@@ -1,32 +1,32 @@
-
+
- Vegravis
+ eframe template
-
+
-
+
-
-
-
-
-
-
+
+
+
+
+
+
-
-
+
+
@@ -60,15 +60,16 @@
width: 100%;
}
- /* Position canvas in center-top: */
+ /* Make canvas fill entire document: */
canvas {
margin-right: auto;
margin-left: auto;
display: block;
position: absolute;
- top: 0%;
- left: 50%;
- transform: translate(-50%, 0%);
+ top: 0;
+ left: 0;
+ width: 100%;
+ height: 100%;
}
.centered {
@@ -114,27 +115,34 @@
transform: rotate(360deg);
}
}
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
-
+
\ No newline at end of file
diff --git a/rust-toolchain.toml b/rust-toolchain.toml
new file mode 100644
index 0000000..6e10895
--- /dev/null
+++ b/rust-toolchain.toml
@@ -0,0 +1,3 @@
+[toolchain]
+channel = "1.82"
+components = ["rustc", "cargo", "rust-std", "clippy", "rustfmt"]
diff --git a/src/interfaces.rs b/src/interfaces.rs
index 3ef4e91..9912d5c 100644
--- a/src/interfaces.rs
+++ b/src/interfaces.rs
@@ -159,6 +159,7 @@ pub trait IVisData: DynClone {
dyn_clone::clone_trait_object!(IVisData);
+#[allow(dead_code)]
pub trait IVisualizer {
fn new(transform: [[f64; 3]; 3]) -> Self;
fn plot(
diff --git a/src/main.rs b/src/main.rs
index 742529c..35934a2 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -7,7 +7,10 @@ use vegravis::MainApp;
fn main() -> Result<(), eframe::Error> {
env_logger::init(); // Log to stderr (if you run with `RUST_LOG=debug`).
let options = eframe::NativeOptions {
- viewport: eframe::egui::ViewportBuilder::default().with_inner_size([1440.0, 960.0]),
+ viewport: eframe::egui::ViewportBuilder::default().with_icon(
+ eframe::icon_data::from_png_bytes(&include_bytes!("../assets/icon-256.png")[..])
+ .expect("Failed to load icon"),
+ ),
..Default::default()
};
eframe::run_native(
@@ -20,19 +23,46 @@ fn main() -> Result<(), eframe::Error> {
// When compiling to web using trunk:
#[cfg(target_arch = "wasm32")]
fn main() {
+ use eframe::wasm_bindgen::JsCast as _;
+
// Redirect `log` message to `console.log` and friends:
eframe::WebLogger::init(log::LevelFilter::Debug).ok();
let web_options = eframe::WebOptions::default();
wasm_bindgen_futures::spawn_local(async {
- eframe::WebRunner::new()
+ let document = web_sys::window()
+ .expect("No window")
+ .document()
+ .expect("No document");
+
+ let canvas = document
+ .get_element_by_id("vegravis_canvas")
+ .expect("Failed to find vegravis_canvas")
+ .dyn_into::()
+ .expect("vegravis_canvas was not a HtmlCanvasElement");
+
+ let start_result = eframe::WebRunner::new()
.start(
- "vegravis_canvas", // hardcode it
+ canvas,
web_options,
- Box::new(|_cc| Box::::default()),
+ Box::new(|_cc| Ok(Box::::default())),
)
- .await
- .expect("failed to start eframe");
+ .await;
+
+ // Remove the loading text and spinner:
+ if let Some(loading_text) = document.get_element_by_id("loading_text") {
+ match start_result {
+ Ok(_) => {
+ loading_text.remove();
+ }
+ Err(e) => {
+ loading_text.set_inner_html(
+ " The app has crashed. See the developer console for details.
",
+ );
+ panic!("Failed to start eframe: {e:?}");
+ }
+ }
+ }
});
}