Skip to content

Commit db99c1e

Browse files
committed
remove websys dependency
1 parent 8e243ff commit db99c1e

6 files changed

Lines changed: 36 additions & 66 deletions

File tree

Cargo.lock

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

Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ softbuffer-wayland=["softbuffer/wayland"]
5050
rug=["kalc-lib/rug","kalc-lib"]
5151
fastnum=["kalc-lib/fastnum","kalc-lib"]
5252
kalc-lib=["dep:kalc-lib"]
53-
wasm=["dep:wasm-bindgen", "dep:web-sys"]
53+
wasm=["dep:wasm-bindgen"]
5454
wasm-draw=["rupl/wasm", "wasm", "rupl/winit", "dep:winit"]
5555
wee=["dep:wee_alloc"]
5656

@@ -67,5 +67,4 @@ rupl={version = "0.1.2",path="../rupl",default-features = false}
6767
kalc-lib={version="1.5.1",default-features=false,features = ["fastrand"],path="../kalc-lib",optional = true}
6868
wasm-bindgen = {version="0.2.100",optional = true}
6969
#console_error_panic_hook = { version = "0.1.7", optional = true }
70-
web-sys = { version = "0.3.77", features = ["CanvasRenderingContext2d", "HtmlCanvasElement"], optional = true }
7170
wee_alloc = {version = "0.4.5", optional = true}

src/app.rs

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -295,11 +295,7 @@ impl App {
295295
self.plot.update(width, height, &mut v);
296296
}
297297
let canvas = self.plot.canvas.as_ref().unwrap();
298-
draw_buffer_web(
299-
self.window.as_ref().unwrap(),
300-
canvas.width(),
301-
wasm_bindgen::Clamped(canvas.data()),
302-
);
298+
crate::draw(canvas.data(), canvas.width());
303299
if b {
304300
let name = self.name.clone();
305301
if let Some(w) = self.window() {
@@ -326,21 +322,7 @@ impl App {
326322
b = true;
327323
self.name = n;
328324
};
329-
use wasm_bindgen::prelude::*;
330-
use winit::platform::web::WindowExtWebSys;
331-
let canvas: web_sys::HtmlCanvasElement = self
332-
.window
333-
.as_ref()
334-
.unwrap()
335-
.canvas()
336-
.expect("Failed to get canvas");
337-
let ctx: web_sys::CanvasRenderingContext2d = canvas
338-
.get_context("2d")
339-
.expect("Failed to get 2d context")
340-
.expect("Failed to get 2d context")
341-
.dyn_into()
342-
.expect("Failed to convert to CanvasRenderingContext2d");
343-
self.plot.update(width, height, ctx);
325+
self.plot.update(width, height);
344326
if b {
345327
let name = self.name.clone();
346328
if let Some(w) = self.window() {
@@ -353,23 +335,3 @@ impl App {
353335
}
354336
}
355337
}
356-
#[cfg(all(feature = "wasm", feature = "tiny-skia"))]
357-
fn draw_buffer_web(win: &winit::window::Window, width: u32, clamped: wasm_bindgen::Clamped<&[u8]>) {
358-
use wasm_bindgen::prelude::*;
359-
let canvas = get_a_canvas(win);
360-
let ctx: web_sys::CanvasRenderingContext2d = canvas
361-
.get_context("2d")
362-
.expect("Failed to get 2d context")
363-
.expect("Failed to get 2d context")
364-
.dyn_into()
365-
.expect("Failed to convert to CanvasRenderingContext2d");
366-
let image = web_sys::ImageData::new_with_u8_clamped_array(clamped, width)
367-
.expect("Failed to create image data");
368-
ctx.put_image_data(&image, 0.0, 0.0)
369-
.expect("Failed to put image data");
370-
371-
fn get_a_canvas(win: &winit::window::Window) -> web_sys::HtmlCanvasElement {
372-
use winit::platform::web::WindowExtWebSys;
373-
win.canvas().expect("Failed to get canvas")
374-
}
375-
}

src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ extern crate wee_alloc;
2525
#[cfg(feature = "wee")]
2626
#[global_allocator]
2727
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
28+
#[cfg(feature = "wasm")]
29+
#[wasm_bindgen::prelude::wasm_bindgen(module = "/window.js")]
30+
extern "C" {
31+
#[cfg(feature = "tiny-skia")]
32+
pub fn draw(slice: &[u8], width: u32);
33+
pub fn get_canvas() -> wasm_bindgen::JsValue;
34+
pub fn resize(x: u32, y: u32);
35+
pub fn dpr() -> f64;
36+
}
2837
#[cfg_attr(target_arch = "wasm32", wasm_bindgen(start))]
2938
pub fn main() {
3039
//#[cfg(feature = "wasm")]

src/window.rs

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -56,34 +56,20 @@ impl App {
5656
}
5757
}
5858
#[cfg(feature = "wasm")]
59-
use wasm_bindgen::JsCast;
60-
#[cfg(feature = "wasm")]
61-
use web_sys::HtmlCanvasElement;
62-
#[cfg(feature = "wasm")]
6359
use winit::platform::web::WindowAttributesExtWebSys;
6460
#[cfg(any(feature = "skia", feature = "tiny-skia", feature = "wasm-draw"))]
6561
impl winit::application::ApplicationHandler for App {
6662
fn resumed(&mut self, event_loop: &winit::event_loop::ActiveEventLoop) {
6763
let window = {
68-
#[cfg(feature = "wasm")]
69-
let window = web_sys::window().expect("no global `window` exists");
70-
#[cfg(feature = "wasm")]
71-
let document = window.document().expect("should have a document");
72-
#[cfg(feature = "wasm")]
73-
let canvas = document
74-
.get_element_by_id("canvas")
75-
.expect("document should have canvas")
76-
.dyn_into::<HtmlCanvasElement>()
77-
.expect("element should be a canvas");
7864
let window = winit::window::Window::default_attributes();
7965
#[cfg(feature = "wasm")]
80-
let window = window.with_canvas(Some(canvas));
66+
let canvas = crate::get_canvas();
67+
#[cfg(feature = "wasm")]
68+
let window = window.with_canvas(Some(canvas.into()));
8169
let window = event_loop.create_window(window);
8270
#[cfg(feature = "wasm")]
8371
{
84-
let window = web_sys::window().unwrap();
85-
let d = window.device_pixel_ratio();
86-
self.dpr = d;
72+
self.dpr = crate::dpr();
8773
}
8874
window.unwrap()
8975
};
@@ -122,10 +108,7 @@ impl winit::application::ApplicationHandler for App {
122108
};
123109
#[cfg(feature = "wasm")]
124110
{
125-
use winit::platform::web::WindowExtWebSys;
126-
let canvas: HtmlCanvasElement = state.canvas().unwrap();
127-
canvas.set_width(_d.width);
128-
canvas.set_height(_d.height);
111+
crate::resize(_d.width, _d.height);
129112
}
130113
state.request_redraw();
131114
#[cfg(feature = "skia-vulkan")]

window.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const canvas = document.getElementById('canvas');
2+
const ctx = canvas.getContext("2d");
3+
export function draw(slice, width) {
4+
const clamped = new Uint8ClampedArray(slice);
5+
const height = clamped.length / (width * 4);
6+
const image = new ImageData(clamped, width, height);
7+
ctx.putImageData(image, 0, 0);
8+
}
9+
export function get_canvas() {
10+
return canvas;
11+
}
12+
export function resize(x, y) {
13+
canvas.width = x;
14+
canvas.height = y;
15+
}
16+
export function dpr() {
17+
return window.devicePixelRatio
18+
}

0 commit comments

Comments
 (0)