Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions src/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ pub struct WebDisplayImpl {
impl WebDisplayImpl {
pub(super) fn new() -> Result<Self, SoftBufferError> {
let document = web_sys::window()
.swbuf_err("`window` is not present in this runtime")?
.swbuf_err("`Window` is not present in this runtime")?
.document()
.swbuf_err("`document` is not present in this runtime")?;
.swbuf_err("`Document` is not present in this runtime")?;

Ok(Self { document })
}
Expand All @@ -44,6 +44,9 @@ pub struct WebImpl {

/// The current width of the canvas.
width: u32,

/// The current height of the canvas.
height: u32,
}

impl WebImpl {
Expand Down Expand Up @@ -76,6 +79,7 @@ impl WebImpl {
ctx,
buffer: Vec::new(),
width: 0,
height: 0,
})
}

Expand All @@ -92,6 +96,7 @@ impl WebImpl {
self.canvas.set_width(width);
self.canvas.set_height(height);
self.width = width;
self.height = height;
Ok(())
}

Expand Down Expand Up @@ -171,14 +176,28 @@ impl<'a> BufferImpl<'a> {
let image_data = result.unwrap();

// This can only throw an error if `data` is detached, which is impossible.
self.imp.ctx.put_image_data(&image_data, 0.0, 0.0).unwrap();
self.imp.ctx.put_image_data(&image_data, 0., 0.).unwrap();

Ok(())
}

/// Fetch the buffer from the window.
pub fn fetch(&mut self) -> Result<(), SoftBufferError> {
todo!()
let image_data = self
.imp
.ctx
.get_image_data(0., 0., self.imp.width.into(), self.imp.height.into())
.ok()
// TODO: Can also error if width or height are 0.
.swbuf_err("`Canvas` contains pixels from a different origin")?;

self.imp
.buffer
.iter_mut()
.zip(image_data.data().0.chunks_exact(4))
.for_each(|(old, new)| *old = u32::from_be_bytes([0, new[0], new[1], new[2]]));

Ok(())
}
}

Expand Down