Skip to content

Commit 7521606

Browse files
authored
Merge pull request #2 from Liamolucko/web
Add web support
2 parents bc900e6 + 62529c4 commit 7521606

File tree

9 files changed

+177
-4
lines changed

9 files changed

+177
-4
lines changed

Cargo.toml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,24 @@ 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]
40+
instant = "0.1.12"
3341
winit = "0.26.1"
42+
43+
[dev-dependencies.image]
44+
version = "0.23.14"
45+
# Disable rayon on web
46+
default-features = false
47+
features = ["jpeg"]
48+
49+
[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
50+
# Turn rayon back on everywhere else; creating the separate entry resets the features to default.
3451
image = "0.23.14"
3552
rayon = "1.5.1"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ For now, the priority for new platforms is:
4444
- Orbital ❌
4545
- UiKit ❌
4646
- Wayland ✅ (Wayland support in winit is immature at the moment, so it might be wise to force X11 if you're using winit)
47-
- Web
47+
- Web
4848
- Win32 ✅
4949
- WinRt ❌
5050
- Xcb ❌

examples/animation.rs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::f64::consts::PI;
2-
use std::time::Instant;
2+
use instant::Instant;
3+
#[cfg(not(target_arch = "wasm32"))]
34
use rayon::prelude::*;
45
use softbuffer::GraphicsContext;
56
use winit::event::{Event, WindowEvent};
@@ -9,6 +10,21 @@ use winit::window::WindowBuilder;
910
fn main() {
1011
let event_loop = EventLoop::new();
1112
let window = WindowBuilder::new().build(&event_loop).unwrap();
13+
14+
#[cfg(target_arch = "wasm32")]
15+
{
16+
use winit::platform::web::WindowExtWebSys;
17+
18+
web_sys::window()
19+
.unwrap()
20+
.document()
21+
.unwrap()
22+
.body()
23+
.unwrap()
24+
.append_child(&window.canvas())
25+
.unwrap();
26+
}
27+
1228
let mut graphics_context = unsafe { GraphicsContext::new(window) }.unwrap();
1329

1430
let mut old_size = (0, 0);
@@ -49,7 +65,7 @@ fn main() {
4965
}
5066

5167
fn pre_render_frames(width: usize, height: usize) -> Vec<Vec<u32>>{
52-
(0..60).into_par_iter().map(|frame_id|{
68+
let render = |frame_id|{
5369
let elapsed = ((frame_id as f64)/(60.0))*2.0*PI;
5470
let buffer = (0..((width * height) as usize))
5571
.map(|index| {
@@ -66,5 +82,11 @@ fn pre_render_frames(width: usize, height: usize) -> Vec<Vec<u32>>{
6682
.collect::<Vec<_>>();
6783

6884
buffer
69-
}).collect()
85+
};
86+
87+
#[cfg(target_arch = "wasm32")]
88+
return (0..60).map(render).collect();
89+
90+
#[cfg(not(target_arch = "wasm32"))]
91+
(0..60).into_par_iter().map(render).collect()
7092
}

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/error.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ pub enum SoftBufferError<W: HasRawWindowHandle> {
1616
PlatformError(Option<String>, Option<Box<dyn Error>>)
1717
}
1818

19+
#[allow(unused)] // This isn't used on all platforms
1920
pub(crate) fn unwrap<T, E: std::error::Error + 'static, W: HasRawWindowHandle>(res: Result<T, E>, str: &str) -> Result<T, SoftBufferError<W>>{
2021
match res{
2122
Ok(t) => Ok(t),

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)