Implement HTMLCanvasElement.transferControlToOffscreen (#34959)

This follows the spec by introducing a new "Placeholder" canvas context
mode. The underlying offscreen canvas is kept accessible from the DOM
element to allow for the drawImage() implementation to work with canvases
that have transfered their control.

Signed-off-by: webbeef <me@webbeef.org>
This commit is contained in:
webbeef 2025-01-12 20:09:02 -08:00 committed by GitHub
parent 90c5685d61
commit c936dd6c4e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 139 additions and 42 deletions

View file

@ -57,7 +57,7 @@ impl OffscreenCanvas {
}
}
fn new(
pub(crate) fn new(
global: &GlobalScope,
proto: Option<HandleObject>,
width: u64,
@ -115,8 +115,9 @@ impl OffscreenCanvas {
Some((data, size.to_u32()))
}
#[allow(unsafe_code)]
fn get_or_init_2d_context(&self) -> Option<DomRoot<OffscreenCanvasRenderingContext2D>> {
pub(crate) fn get_or_init_2d_context(
&self,
) -> Option<DomRoot<OffscreenCanvasRenderingContext2D>> {
if let Some(ctx) = self.context() {
return match *ctx {
OffscreenCanvasContext::OffscreenContext2d(ref ctx) => Some(DomRoot::from_ref(ctx)),
@ -190,6 +191,10 @@ impl OffscreenCanvasMethods<crate::DomTypeHolder> for OffscreenCanvas {
},
}
}
if let Some(canvas) = &self.placeholder {
canvas.set_natural_width(value as _);
}
}
// https://html.spec.whatwg.org/multipage/#dom-offscreencanvas-height
@ -208,5 +213,9 @@ impl OffscreenCanvasMethods<crate::DomTypeHolder> for OffscreenCanvas {
},
}
}
if let Some(canvas) = &self.placeholder {
canvas.set_natural_height(value as _);
}
}
}