Fabric.js changes my canvas size to 300x150 after initialization


  <div class="canvas-wrapper">
    <canvas id="myCanvas"></canvas>


.canvas-wrapper {
    width: 900px;
    min-height: 600px;

     border:1px solid red;
     position: absolute;
     height: 100%;
     width: 99%;


var myCanvas = new fabric.Canvas('myCanvas');

My canvas get resized to 300x150 after it's been initialized, why?


in the latest version, you will have to do something like:

var canvas = new fabric.Canvas('myCanvas');

.... Your code ....


Works fine for me..

When initializing canvas, Fabric reads width/height attributes on canvas element or takes width/height passed in options.

var myCanvas = new fabric.Canvas('myCanvas', { width: 900, height: 600 });


<canvas width="900" height="600"></canvas>
var myCanvas = new fabric.Canvas('myCanvas');

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:

  1. In CSS, you can override individual properties and following them with !important; the downside to this is that any further definitions must also use !important or they will be ignored:
[width], [height], [style]
  width:      100vw!important;
  min-height: 100vh!important;
  1. Using JQuery you can set the inline property values to an empty string. You can presumably achieve this without JQuery, but I will leave that as an exercise to you.

Javascript (JQuery):

$('[style]').attr(  'style',  function(index, style){  return ''; });
$('[height]').attr( 'height', function(index, height){ return ''; });
$('[width]').attr(  'width',  function(index, height){ return ''; });


*, *:before, *:after
  box-sizing: border-box;

  width:      100vw; 
  min-height: 100vh;
  padding:    0;
  margin:     0;

  width:      inherit; 
  min-height: inherit;

  position: absolute;
  top:      0;

After that, CSS will be applied as expected.

It is apparently also necessary to tell Fabric about the change:

function sizeCanvas()
  var width  = Math.max(document.documentElement.clientWidth,  window.innerWidth  || 0);
  var height = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);
  canvas.setHeight( height )
  canvas.setWidth( width )

window.addEventListener('resize', draw, false);

function initDraw()
  // setup

function draw()


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:

  1. 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.

  2. Fabric changes the DOM arrangement and nests the original canvas in a new div with a second nested canvas, which may result in surprises.

Need Your Help

Sql query to select from 1 hour ago?

sql mysql

I have this query but I want to change the date to delete everything that is from more than 1 hour ago based on the server time (or if not possible by server time by post date). How do I do that?

Performance issue in update query

sql postgresql sqlperformance

I have one small doubt in query performance. Basically, I have a table with more than 1C records. sl_id is the primary key in that table. Currently, I am updating the table column status to true (d...