HTML:
<div class="canvas-wrapper">
<canvas id="myCanvas"></canvas>
</div>
CSS
.canvas-wrapper {
width: 900px;
min-height: 600px;
}
#myCanvas{
border:1px solid red;
position: absolute;
top:22px;
left:0px;
height: 100%;
width: 99%;
}
JS
var myCanvas = new fabric.Canvas('myCanvas');
My canvas get resized to 300x150 after it's been initialized, why?
A real answer to this question—not involving a new static size but actually permitting CSS to be effective—is one of the following options:
Javascript (JQuery):
CSS:
After that, CSS will be applied as expected.
It is apparently also necessary to tell Fabric about the change:
This will configure Fabric to the viewport size and keep it sized accordingly if it resizes.
It's worth noting that this problem arises for two reasons:
Someone thought it was a good idea to use inline styles and should be severely castigated, as there is no situation where this is ever an appropriate practice.
Fabric changes the DOM arrangement and nests the original canvas in a new div with a second nested canvas, which may result in surprises.
in the latest version, you will have to do something like:
.... Your code ....
Works fine for me..
When initializing canvas, Fabric reads width/height attributes on canvas element or takes width/height passed in options.
or: