-
I've been working on an emulator project targeting WASM. Currently, I'm using a simple 2D JavaScript canvas to draw the screen, but I'm running into performance issues. This library looks like it could be a much more performant approach, but I don't really want to use Winit in my project -- I'd rather handle the event loop, key presses, etc. myself. Is there a way to use pixels without using Winit, such as directly attaching the SurfaceTexture to a |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
At present you need something that implements If we decide to support canvases directly, it will be behind a feature flag and will pretty much be a bespoke substitute for rust-windowing/raw-window-handle#102 (which is at an impasse and not expected to see any progress in the near future). |
Beta Was this translation helpful? Give feedback.
-
Thank you for the fast reply! For anyone else stumbling upon this problem, here's a quick and dirty implementor for use pixels::raw_window_handle::{
HasRawDisplayHandle, HasRawWindowHandle, RawDisplayHandle, RawWindowHandle, WebDisplayHandle,
WebWindowHandle,
};
use web_sys::HtmlCanvasElement;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct CanvasWindow {
id: u32,
}
impl CanvasWindow {
pub fn new(canvas: &HtmlCanvasElement) -> Self {
// Get the ID from the canvas's `data-raw-handle` attribute.
let id = canvas
.attributes()
.get_named_item("data-raw-handle")
.expect("Canvas has no `data-raw-handle` attribute")
.value()
.parse::<u32>()
.expect("Canvas `data-raw-handle` attribute is not a number");
Self { id }
}
}
unsafe impl HasRawWindowHandle for CanvasWindow {
fn raw_window_handle(&self) -> RawWindowHandle {
let mut handle = WebWindowHandle::empty();
handle.id = self.id;
RawWindowHandle::Web(handle)
}
}
unsafe impl HasRawDisplayHandle for CanvasWindow {
fn raw_display_handle(&self) -> RawDisplayHandle {
let handle = WebDisplayHandle::empty();
RawDisplayHandle::Web(handle)
}
} |
Beta Was this translation helpful? Give feedback.
At present you need something that implements
HasRawWindowHandle
to gain access to a WebGL (or WebGPU) context. This is by design. See #309 (comment) for rationale. This means you do not needwinit
, but you still need some abstraction layer.If we decide to support canvases directly, it will be behind a feature flag and will pretty much be a bespoke substitute for rust-windowing/raw-window-handle#102 (which is at an impasse and not expected to see any progress in the near future).