Skip to content

Commit 19ad67f

Browse files
committed
start wasm draw bit
1 parent f895168 commit 19ad67f

5 files changed

Lines changed: 65 additions & 19 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ rug=["kalc-lib/rug","kalc-lib"]
5151
fastnum=["kalc-lib/fastnum","kalc-lib"]
5252
kalc-lib=["dep:kalc-lib"]
5353
wasm=["dep:wasm-bindgen", "dep:web-sys"]
54+
wasm-draw=["rupl/wasm", "wasm", "rupl/winit", "dep:winit"]
5455

5556
[dependencies]
5657
egui={version="0.31.1",default-features = false,optional=true}
@@ -65,4 +66,4 @@ rupl={version = "0.1.2",path="../rupl",default-features = false}
6566
kalc-lib={version="1.5.1",default-features=false,features = ["fastrand"],path="../kalc-lib",optional = true}
6667
wasm-bindgen = {version="0.2.100",optional = true}
6768
#console_error_panic_hook = { version = "0.1.7", optional = true }
68-
web-sys = { version = "0.3.77", features = ["CanvasRenderingContext2d"], optional = true }
69+
web-sys = { version = "0.3.77", features = ["CanvasRenderingContext2d", "HtmlCanvasElement"], optional = true }

src/app.rs

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,13 @@ impl App {
123123
#[cfg(any(feature = "skia", feature = "tiny-skia"))]
124124
#[cfg(not(feature = "skia-vulkan"))]
125125
surface_state: None,
126-
#[cfg(any(feature = "skia", feature = "tiny-skia"))]
126+
#[cfg(any(feature = "skia", feature = "tiny-skia", feature = "wasm-draw"))]
127127
input_state: rupl::types::InputState::default(),
128-
#[cfg(any(feature = "skia", feature = "tiny-skia"))]
128+
#[cfg(any(feature = "skia", feature = "tiny-skia", feature = "wasm-draw"))]
129129
name: function,
130-
#[cfg(any(feature = "skia", feature = "tiny-skia"))]
130+
#[cfg(any(feature = "skia", feature = "tiny-skia", feature = "wasm-draw"))]
131131
touch_positions: Default::default(),
132-
#[cfg(any(feature = "skia", feature = "tiny-skia"))]
132+
#[cfg(any(feature = "skia", feature = "tiny-skia", feature = "wasm-draw"))]
133133
last_touch_positions: Default::default(),
134134
}
135135
}
@@ -184,13 +184,13 @@ impl App {
184184
#[cfg(any(feature = "skia", feature = "tiny-skia"))]
185185
#[cfg(not(feature = "wasm"))]
186186
surface_state: None,
187-
#[cfg(any(feature = "skia", feature = "tiny-skia"))]
187+
#[cfg(any(feature = "skia", feature = "tiny-skia", feature = "wasm-draw"))]
188188
input_state: rupl::types::InputState::default(),
189-
#[cfg(any(feature = "skia", feature = "tiny-skia"))]
189+
#[cfg(any(feature = "skia", feature = "tiny-skia", feature = "wasm-draw"))]
190190
name: _function,
191-
#[cfg(any(feature = "skia", feature = "tiny-skia"))]
191+
#[cfg(any(feature = "skia", feature = "tiny-skia", feature = "wasm-draw"))]
192192
touch_positions: Default::default(),
193-
#[cfg(any(feature = "skia", feature = "tiny-skia"))]
193+
#[cfg(any(feature = "skia", feature = "tiny-skia", feature = "wasm-draw"))]
194194
last_touch_positions: Default::default(),
195195
}
196196
}
@@ -307,8 +307,49 @@ impl App {
307307
}
308308
}
309309
}
310+
#[cfg(feature = "wasm")]
311+
#[cfg(feature = "wasm-draw")]
312+
pub(crate) fn main(&mut self, width: u32, height: u32) {
313+
let mut b = false;
314+
self.plot.keybinds(&self.input_state);
315+
self.plot
316+
.set_screen(width as f64, height as f64, true, true);
317+
#[cfg(feature = "bincode")]
318+
if let Some(tiny) = std::mem::take(&mut self.tiny) {
319+
self.plot.apply_tiny(tiny);
320+
}
321+
if let Some(n) = self.data.update(&mut self.plot) {
322+
b = true;
323+
self.name = n;
324+
};
325+
use wasm_bindgen::prelude::*;
326+
use winit::platform::web::WindowExtWebSys;
327+
let canvas: web_sys::HtmlCanvasElement = self
328+
.window
329+
.as_ref()
330+
.unwrap()
331+
.canvas()
332+
.expect("Failed to get canvas");
333+
let ctx: web_sys::CanvasRenderingContext2d = canvas
334+
.get_context("2d")
335+
.expect("Failed to get 2d context")
336+
.expect("Failed to get 2d context")
337+
.dyn_into()
338+
.expect("Failed to convert to CanvasRenderingContext2d");
339+
self.plot.update(width, height, ctx);
340+
if b {
341+
let name = self.name.clone();
342+
if let Some(w) = self.window() {
343+
if name.is_empty() {
344+
w.set_title("kalc-plot");
345+
} else {
346+
w.set_title(&name);
347+
}
348+
}
349+
}
350+
}
310351
}
311-
#[cfg(feature = "wasm")]
352+
#[cfg(all(feature = "wasm", feature = "tiny-skia"))]
312353
fn draw_buffer_web(win: &winit::window::Window, width: u32, clamped: wasm_bindgen::Clamped<&[u8]>) {
313354
use wasm_bindgen::prelude::*;
314355
let canvas = get_a_canvas(win);

src/lib.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![windows_subsystem = "windows"]
22
mod app;
33
mod data;
4-
#[cfg(any(feature = "skia", feature = "tiny-skia"))]
4+
#[cfg(any(feature = "skia", feature = "tiny-skia", feature = "wasm-draw"))]
55
mod window;
66
use crate::data::Data;
77
#[cfg(feature = "kalc-lib")]
@@ -97,7 +97,7 @@ pub fn main() {
9797
)
9898
.unwrap();
9999
}
100-
#[cfg(any(feature = "skia", feature = "tiny-skia"))]
100+
#[cfg(any(feature = "skia", feature = "tiny-skia", feature = "wasm-draw"))]
101101
{
102102
#[cfg(feature = "kalc-lib")]
103103
let f = data.colors.graphtofile.clone();
@@ -169,14 +169,14 @@ struct App {
169169
#[cfg_attr(feature = "bincode", serde(skip))]
170170
#[cfg(feature = "wasm")]
171171
window: Option<winit::window::Window>,
172-
#[cfg(any(feature = "skia", feature = "tiny-skia"))]
172+
#[cfg(any(feature = "skia", feature = "tiny-skia", feature = "wasm-draw"))]
173173
#[cfg_attr(feature = "bincode", serde(skip))]
174174
input_state: rupl::types::InputState,
175-
#[cfg(any(feature = "skia", feature = "tiny-skia"))]
175+
#[cfg(any(feature = "skia", feature = "tiny-skia", feature = "wasm-draw"))]
176176
name: String,
177-
#[cfg(any(feature = "skia", feature = "tiny-skia"))]
177+
#[cfg(any(feature = "skia", feature = "tiny-skia", feature = "wasm-draw"))]
178178
touch_positions: std::collections::HashMap<u64, rupl::types::Vec2>,
179-
#[cfg(any(feature = "skia", feature = "tiny-skia"))]
179+
#[cfg(any(feature = "skia", feature = "tiny-skia", feature = "wasm-draw"))]
180180
last_touch_positions: std::collections::HashMap<u64, rupl::types::Vec2>,
181181
}
182182

src/window.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#[cfg(any(feature = "skia", feature = "tiny-skia"))]
1+
#[cfg(any(feature = "skia", feature = "tiny-skia", feature = "wasm-draw"))]
22
use crate::App;
3-
#[cfg(any(feature = "skia", feature = "tiny-skia"))]
3+
#[cfg(any(feature = "skia", feature = "tiny-skia", feature = "wasm-draw"))]
44
impl App {
55
#[cfg(not(target_arch = "wasm32"))]
66
pub(crate) fn set_title(&self, window: &std::sync::Arc<winit::window::Window>) {
@@ -19,6 +19,7 @@ impl App {
1919
}
2020
}
2121
#[cfg(not(feature = "skia-vulkan"))]
22+
#[cfg(not(feature = "wasm-draw"))]
2223
#[cfg(not(target_arch = "wasm32"))]
2324
pub(crate) fn surface_state(
2425
&mut self,
@@ -51,7 +52,7 @@ use wasm_bindgen::JsCast;
5152
use web_sys::HtmlCanvasElement;
5253
#[cfg(feature = "wasm")]
5354
use winit::platform::web::WindowAttributesExtWebSys;
54-
#[cfg(any(feature = "skia", feature = "tiny-skia"))]
55+
#[cfg(any(feature = "skia", feature = "tiny-skia", feature = "wasm-draw"))]
5556
impl winit::application::ApplicationHandler for App {
5657
fn resumed(&mut self, event_loop: &winit::event_loop::ActiveEventLoop) {
5758
let window = {
@@ -76,6 +77,7 @@ impl winit::application::ApplicationHandler for App {
7677
let window = std::sync::Arc::new(window);
7778
self.set_title(&window);
7879
#[cfg(not(feature = "skia-vulkan"))]
80+
#[cfg(not(feature = "wasm-draw"))]
7981
{
8082
let context = softbuffer::Context::new(window.clone()).unwrap();
8183
self.surface_state =
@@ -320,6 +322,7 @@ impl winit::application::ApplicationHandler for App {
320322
}
321323
fn suspended(&mut self, _: &winit::event_loop::ActiveEventLoop) {
322324
#[cfg(not(feature = "skia-vulkan"))]
325+
#[cfg(not(feature = "wasm-draw"))]
323326
#[cfg(not(target_arch = "wasm32"))]
324327
{
325328
self.surface_state = None

0 commit comments

Comments
 (0)