Skip to content
Permalink
Browse files

Reset canvas state when changing bitmap dimensions

  • Loading branch information
dzbarsky committed Dec 1, 2015
1 parent b4906aa commit 302f41d34bcb8d7eecc58770087f1727a99e19d7
@@ -143,12 +143,20 @@ impl CanvasRenderingContext2D {
CanvasRenderingContext2DBinding::Wrap)
}

pub fn recreate(&self, size: Size2D<i32>) {
// https://html.spec.whatwg.org/multipage/#concept-canvas-set-bitmap-dimensions
pub fn set_bitmap_dimensions(&self, size: Size2D<i32>) {
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<CanvasMsg> {
self.ipc_renderer.clone()
}
@@ -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),
}
}
@@ -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",
@@ -0,0 +1,87 @@
<!DOCTYPE html>
<!-- THIS IS COPIED FROM
tests/wpt/web-platform-tests/html/semantics/embedded-content/the-canvas-element/initial.reset.2dstate.html - delete when that test passes -->
<title>Canvas test: initial.reset.2dstate</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/common/canvas-tests.js"></script>
<link rel="stylesheet" href="/common/canvas-tests.css">
<body class="show_output">

<h1>initial.reset.2dstate</h1>
<p class="desc">Resetting the canvas state resets 2D state variables</p>


<p class="output">Actual output:</p>
<canvas id="c" class="output" width="100" height="50"><p class="fallback">FAIL (fallback content)</p></canvas>

<ul id="d"></ul>
<script>
var t = async_test("Resetting the canvas state resets 2D state variables");
_addTest(function(canvas, ctx) {

canvas.width = 100;
var default_val;

default_val = ctx.strokeStyle;
ctx.strokeStyle = "#ff0000";
canvas.width = 100;
_assertSame(ctx.strokeStyle, default_val, "ctx.strokeStyle", "default_val");

default_val = ctx.fillStyle;
ctx.fillStyle = "#ff0000";
canvas.width = 100;
_assertSame(ctx.fillStyle, default_val, "ctx.fillStyle", "default_val");

default_val = ctx.globalAlpha;
ctx.globalAlpha = 0.5;
canvas.width = 100;
_assertSame(ctx.globalAlpha, default_val, "ctx.globalAlpha", "default_val");

default_val = ctx.lineWidth;
ctx.lineWidth = 0.5;
canvas.width = 100;
_assertSame(ctx.lineWidth, default_val, "ctx.lineWidth", "default_val");

default_val = ctx.lineCap;
ctx.lineCap = "round";
canvas.width = 100;
_assertSame(ctx.lineCap, default_val, "ctx.lineCap", "default_val");

default_val = ctx.lineJoin;
ctx.lineJoin = "round";
canvas.width = 100;
_assertSame(ctx.lineJoin, default_val, "ctx.lineJoin", "default_val");

default_val = ctx.miterLimit;
ctx.miterLimit = 0.5;
canvas.width = 100;
_assertSame(ctx.miterLimit, default_val, "ctx.miterLimit", "default_val");

default_val = ctx.shadowOffsetX;
ctx.shadowOffsetX = 5;
canvas.width = 100;
_assertSame(ctx.shadowOffsetX, default_val, "ctx.shadowOffsetX", "default_val");

default_val = ctx.shadowOffsetY;
ctx.shadowOffsetY = 5;
canvas.width = 100;
_assertSame(ctx.shadowOffsetY, default_val, "ctx.shadowOffsetY", "default_val");

default_val = ctx.shadowBlur;
ctx.shadowBlur = 5;
canvas.width = 100;
_assertSame(ctx.shadowBlur, default_val, "ctx.shadowBlur", "default_val");

default_val = ctx.shadowColor;
ctx.shadowColor = "#ff0000";
canvas.width = 100;
_assertSame(ctx.shadowColor, default_val, "ctx.shadowColor", "default_val");

default_val = ctx.globalCompositeOperation;
ctx.globalCompositeOperation = "copy";
canvas.width = 100;
_assertSame(ctx.globalCompositeOperation, default_val, "ctx.globalCompositeOperation", "default_val");
});
</script>

0 comments on commit 302f41d

Please sign in to comment.
You can’t perform that action at this time.