Short answer: you're probably not targeting the canvas element properly with your CSS selector.
Long 'answer': I think there's a couple of things to note:
- Changing the
height
or width
attributes of the canvas
tag will complete clear it (i.e. remove anything drawn to it, resetting it to a blank slate). (Although apparently, this doesn't happen in some browsers.)
- For
<canvas>
elements, the height
or width
attributes have a unique relationship with the CSS properties of height
and width
, which I describe here: https://stackoverflow.com/a/19079320/1937302
- Changes to the
height
or width
attributes will not be 'transitioned'/animated
- Changes to the CSS properities of
height
or width
attributes will be 'transitioned'/animated
I've demoed some of this here:
http://jsfiddle.net/BYossarian/WWtGZ/3/
HTML:
<canvas id="canvas1" height="50" width="100"></canvas>
CSS:
canvas {
border: 1px solid black;
transition: all 1s ease;
height: 50px;
}
JS:
var canvas = document.getElementById('canvas1'),
ctx = canvas.getContext('2d');
ctx.fillStyle = "red";
ctx.fillRect(10, 10, 30, 30);
// changing width attribute clears canvas element (in most broswers)
// changing width attribute isn't animated by CSS transition
setTimeout(function () {
canvas.width = "200";
}, 2000);
// changing CSS rule for width is animated, although if no CSS rule for width already exists, then a width of 0px is taken as the starting point
setTimeout(function () {
ctx.fillStyle = "red";
ctx.fillRect(10, 10, 30, 30);
canvas.style.width = "100px";
}, 4000);