Skip to content

Commit d735510

Browse files
committed
Add web support
A limitation of this implementation is that it can't coexist with anything which creates a canvas context other than `CanvasRenderingContext2D`, since only one context can exist per canvas.
1 parent bc900e6 commit d735510

File tree

7 files changed

+155
-0
lines changed

7 files changed

+155
-0
lines changed

Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ winapi = "0.3.9"
2929
core-graphics = "0.22.3"
3030
objc = "0.2.7"
3131

32+
[target.'cfg(target_arch = "wasm32")'.dependencies]
33+
wasm-bindgen = "0.2.78"
34+
35+
[target.'cfg(target_arch = "wasm32")'.dependencies.web-sys]
36+
version = "0.3.55"
37+
features = ["CanvasRenderingContext2d", "Document", "Element", "HtmlCanvasElement", "ImageData", "Window"]
38+
3239
[dev-dependencies]
3340
winit = "0.26.1"
3441
image = "0.23.14"

examples/animation.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,21 @@ use winit::window::WindowBuilder;
99
fn main() {
1010
let event_loop = EventLoop::new();
1111
let window = WindowBuilder::new().build(&event_loop).unwrap();
12+
13+
#[cfg(target_arch = "wasm32")]
14+
{
15+
use winit::platform::web::WindowExtWebSys;
16+
17+
web_sys::window()
18+
.unwrap()
19+
.document()
20+
.unwrap()
21+
.body()
22+
.unwrap()
23+
.append_child(&window.canvas())
24+
.unwrap();
25+
}
26+
1227
let mut graphics_context = unsafe { GraphicsContext::new(window) }.unwrap();
1328

1429
let mut old_size = (0, 0);

examples/fruit.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,21 @@ fn main() {
1717

1818
let event_loop = EventLoop::new();
1919
let window = WindowBuilder::new().build(&event_loop).unwrap();
20+
21+
#[cfg(target_arch = "wasm32")]
22+
{
23+
use winit::platform::web::WindowExtWebSys;
24+
25+
web_sys::window()
26+
.unwrap()
27+
.document()
28+
.unwrap()
29+
.body()
30+
.unwrap()
31+
.append_child(&window.canvas())
32+
.unwrap();
33+
}
34+
2035
let mut graphics_context = unsafe { GraphicsContext::new(window) }.unwrap();
2136

2237
event_loop.run(move |event, _, control_flow| {

examples/winit.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,21 @@ use winit::window::WindowBuilder;
66
fn main() {
77
let event_loop = EventLoop::new();
88
let window = WindowBuilder::new().build(&event_loop).unwrap();
9+
10+
#[cfg(target_arch = "wasm32")]
11+
{
12+
use winit::platform::web::WindowExtWebSys;
13+
14+
web_sys::window()
15+
.unwrap()
16+
.document()
17+
.unwrap()
18+
.body()
19+
.unwrap()
20+
.append_child(&window.canvas())
21+
.unwrap();
22+
}
23+
924
let mut graphics_context = unsafe { GraphicsContext::new(window) }.unwrap();
1025

1126
event_loop.run(move |event, _, control_flow| {

examples/winit_wrong_sized_buffer.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,21 @@ const BUFFER_HEIGHT: usize = 128;
99
fn main() {
1010
let event_loop = EventLoop::new();
1111
let window = WindowBuilder::new().build(&event_loop).unwrap();
12+
13+
#[cfg(target_arch = "wasm32")]
14+
{
15+
use winit::platform::web::WindowExtWebSys;
16+
17+
web_sys::window()
18+
.unwrap()
19+
.document()
20+
.unwrap()
21+
.body()
22+
.unwrap()
23+
.append_child(&window.canvas())
24+
.unwrap();
25+
}
26+
1227
let mut graphics_context = unsafe { GraphicsContext::new(window) }.unwrap();
1328

1429
event_loop.run(move |event, _, control_flow| {

src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ mod cg;
1212
mod x11;
1313
#[cfg(target_os = "linux")]
1414
mod wayland;
15+
#[cfg(target_arch = "wasm32")]
16+
mod web;
1517

1618
mod error;
1719

@@ -45,6 +47,8 @@ impl<W: HasRawWindowHandle> GraphicsContext<W> {
4547
RawWindowHandle::Win32(win32_handle) => Box::new(win32::Win32Impl::new(&win32_handle)?),
4648
#[cfg(target_os = "macos")]
4749
RawWindowHandle::AppKit(appkit_handle) => Box::new(cg::CGImpl::new(appkit_handle)?),
50+
#[cfg(target_arch = "wasm32")]
51+
RawWindowHandle::Web(web_handle) => Box::new(web::WebImpl::new(web_handle)?),
4852
unimplemented_handle_type => return Err(SoftBufferError::UnsupportedPlatform {
4953
window,
5054
human_readable_platform_name: window_handle_type_name(&unimplemented_handle_type),

src/web.rs

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
use raw_window_handle::HasRawWindowHandle;
2+
use raw_window_handle::WebHandle;
3+
use wasm_bindgen::Clamped;
4+
use wasm_bindgen::JsCast;
5+
use web_sys::CanvasRenderingContext2d;
6+
use web_sys::HtmlCanvasElement;
7+
use web_sys::ImageData;
8+
9+
use crate::GraphicsContextImpl;
10+
use crate::SoftBufferError;
11+
12+
pub struct WebImpl {
13+
canvas: HtmlCanvasElement,
14+
ctx: CanvasRenderingContext2d,
15+
}
16+
17+
impl WebImpl {
18+
pub fn new<W: HasRawWindowHandle>(handle: WebHandle) -> Result<Self, SoftBufferError<W>> {
19+
let canvas: HtmlCanvasElement = web_sys::window()
20+
.ok_or_else(|| {
21+
SoftBufferError::PlatformError(
22+
Some("`window` is not present in this runtime".into()),
23+
None,
24+
)
25+
})?
26+
.document()
27+
.ok_or_else(|| {
28+
SoftBufferError::PlatformError(
29+
Some("`document` is not present in this runtime".into()),
30+
None,
31+
)
32+
})?
33+
.query_selector(&format!("canvas[data-raw-handle=\"{}\"]", handle.id))
34+
// `querySelector` only throws an error if the selector is invalid.
35+
.unwrap()
36+
.ok_or_else(|| {
37+
SoftBufferError::PlatformError(
38+
Some("No canvas found with the given id".into()),
39+
None,
40+
)
41+
})?
42+
// We already made sure this was a canvas in `querySelector`.
43+
.unchecked_into();
44+
45+
let ctx = canvas
46+
.get_context("2d")
47+
.map_err(|_| {
48+
SoftBufferError::PlatformError(
49+
Some("Canvas already controlled using `OffscreenCanvas`".into()),
50+
None,
51+
)
52+
})?
53+
.ok_or_else(|| {
54+
SoftBufferError::PlatformError(
55+
Some("A canvas context other than `CanvasRenderingContext2d` was already created".into()),
56+
None,
57+
)
58+
})?
59+
.dyn_into()
60+
.expect("`getContext(\"2d\") didn't return a `CanvasRenderingContext2d`");
61+
62+
Ok(Self { canvas, ctx })
63+
}
64+
}
65+
66+
impl GraphicsContextImpl for WebImpl {
67+
unsafe fn set_buffer(&mut self, buffer: &[u32], width: u16, height: u16) {
68+
self.canvas.set_width(width.into());
69+
self.canvas.set_height(height.into());
70+
71+
let bitmap: Vec<_> = buffer
72+
.iter()
73+
.copied()
74+
.flat_map(|pixel| [(pixel >> 16) as u8, (pixel >> 8) as u8, pixel as u8, 255])
75+
.collect();
76+
77+
// This should only throw an error if the buffer we pass's size is incorrect, which is checked in the outer `set_buffer` call.
78+
let image_data =
79+
ImageData::new_with_u8_clamped_array(Clamped(&bitmap), width.into()).unwrap();
80+
81+
// This can only throw an error if `data` is detached, which is impossible.
82+
self.ctx.put_image_data(&image_data, 0.0, 0.0).unwrap();
83+
}
84+
}

0 commit comments

Comments
 (0)