set width and height of canvas in fabric.js

These work as well!

canvas.setWidth(500);
canvas.setHeight(400);

I think the above answer does not use fabric.js, it uses normal canvas.

When you use fabric.js and its Fabric.Canvas class, the code should be:

image.onLoad = function() {
  var fabric_canvas = new fabric.Canvas('canvas1');
  fabric_canvas.setDimensions({width:image.width, height:image.height});
}; 

In my case,

var canvas = new fabric.Canvas("test_fabric");
canvas.setDimensions({width:800, height:200});

The result is:

<canvas id="test_fabric" width="800" height="200" class="lower-canvas" style="position: absolute; width: 800px; height: 200px; left: 0px; top: 0px; -webkit-user-select: none;"></canvas>

Both canvas.width and canvas.style.width are set.