diff --git a/components/script/dom/canvasrenderingcontext2d.rs b/components/script/dom/canvasrenderingcontext2d.rs index c0ca96beef6..5fed20daae7 100644 --- a/components/script/dom/canvasrenderingcontext2d.rs +++ b/components/script/dom/canvasrenderingcontext2d.rs @@ -143,12 +143,20 @@ impl CanvasRenderingContext2D { CanvasRenderingContext2DBinding::Wrap) } - pub fn recreate(&self, size: Size2D) { + // https://html.spec.whatwg.org/multipage/#concept-canvas-set-bitmap-dimensions + pub fn set_bitmap_dimensions(&self, size: Size2D) { + self.reset_to_initial_state(); self.ipc_renderer .send(CanvasMsg::Common(CanvasCommonMsg::Recreate(size))) .unwrap(); } + // https://html.spec.whatwg.org/multipage/#reset-the-rendering-context-to-its-default-state + fn reset_to_initial_state(&self) { + self.saved_states.borrow_mut().clear(); + *self.state.borrow_mut() = CanvasContextState::new(); + } + pub fn ipc_renderer(&self) -> IpcSender { self.ipc_renderer.clone() } diff --git a/components/script/dom/htmlcanvaselement.rs b/components/script/dom/htmlcanvaselement.rs index 325a324b3c7..5d97d922fcd 100644 --- a/components/script/dom/htmlcanvaselement.rs +++ b/components/script/dom/htmlcanvaselement.rs @@ -81,7 +81,7 @@ impl HTMLCanvasElement { let size = self.get_size(); if let Some(ref context) = *self.context.borrow() { match *context { - CanvasContext::Context2d(ref context) => context.recreate(size), + CanvasContext::Context2d(ref context) => context.set_bitmap_dimensions(size), CanvasContext::WebGL(ref context) => context.recreate(size), } } diff --git a/tests/wpt/mozilla/meta/MANIFEST.json b/tests/wpt/mozilla/meta/MANIFEST.json index e2b7aa33d2d..485040e3fb5 100644 --- a/tests/wpt/mozilla/meta/MANIFEST.json +++ b/tests/wpt/mozilla/meta/MANIFEST.json @@ -5187,6 +5187,12 @@ "url": "/_mozilla/mozilla/calc.html" } ], + "mozilla/canvas.initial.reset.2dstate.html": [ + { + "path": "mozilla/canvas.initial.reset.2dstate.html", + "url": "/_mozilla/mozilla/canvas.initial.reset.2dstate.html" + } + ], "mozilla/caption.html": [ { "path": "mozilla/caption.html", diff --git a/tests/wpt/mozilla/tests/mozilla/canvas.initial.reset.2dstate.html b/tests/wpt/mozilla/tests/mozilla/canvas.initial.reset.2dstate.html new file mode 100644 index 00000000000..e276ed09ffc --- /dev/null +++ b/tests/wpt/mozilla/tests/mozilla/canvas.initial.reset.2dstate.html @@ -0,0 +1,87 @@ + + +Canvas test: initial.reset.2dstate + + + + + + +

initial.reset.2dstate

+

Resetting the canvas state resets 2D state variables

+ + +

Actual output:

+

FAIL (fallback content)

+ +
    + +