Add from_canvas() to Surface for Wasm (#76)
Co-authored-by: Toniman575 <toniman575@gmail.com>
This commit is contained in:
parent
ecfae194ce
commit
fcda747ddf
4 changed files with 48 additions and 0 deletions
22
src/web.rs
22
src/web.rs
|
|
@ -11,6 +11,7 @@ use web_sys::ImageData;
|
|||
use crate::error::SwResultExt;
|
||||
use crate::SoftBufferError;
|
||||
use std::convert::TryInto;
|
||||
use std::marker::PhantomData;
|
||||
use std::num::NonZeroU32;
|
||||
|
||||
/// Display implementation for the web platform.
|
||||
|
|
@ -56,6 +57,10 @@ impl WebImpl {
|
|||
// We already made sure this was a canvas in `querySelector`.
|
||||
.unchecked_into();
|
||||
|
||||
Self::from_canvas(canvas)
|
||||
}
|
||||
|
||||
fn from_canvas(canvas: HtmlCanvasElement) -> Result<Self, SoftBufferError> {
|
||||
let ctx = canvas
|
||||
.get_context("2d")
|
||||
.ok()
|
||||
|
|
@ -96,6 +101,23 @@ impl WebImpl {
|
|||
}
|
||||
}
|
||||
|
||||
/// Extension methods for the Wasm target on [`Surface`](crate::Surface).
|
||||
pub trait SurfaceExtWeb: Sized {
|
||||
/// Creates a new instance of this struct, using the provided [`HtmlCanvasElement`].
|
||||
fn from_canvas(canvas: HtmlCanvasElement) -> Result<Self, SoftBufferError>;
|
||||
}
|
||||
|
||||
impl SurfaceExtWeb for crate::Surface {
|
||||
fn from_canvas(canvas: HtmlCanvasElement) -> Result<Self, SoftBufferError> {
|
||||
let imple = crate::SurfaceDispatch::Web(WebImpl::from_canvas(canvas)?);
|
||||
|
||||
Ok(Self {
|
||||
surface_impl: Box::new(imple),
|
||||
_marker: PhantomData,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub struct BufferImpl<'a> {
|
||||
imp: &'a mut WebImpl,
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue