Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 827
user469652 Avatar asked May 14 '12 10:05

user469652


People also ask

How do I change the width and height of a dynamic canvas?

To do it, simply set the width and height properties of the Canvas object, and then redraw the canvas contents: Canvas . width = 600 ; Canvas . height = 500 ; drawScreen ();

Which function will get the width of the canvas?

You can get the width and height of a canvas element simply by accessing those properties of the element. For example: var canvas = document. getElementById('mycanvas'); var width = canvas.


1 Answers

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

var canvas = new fabric.Canvas('myCanvas'); canvas.setHeight(500); canvas.setWidth(800); 

.... Your code ....

canvas.renderAll(); 

Works fine for me..

For dynamically changing size, this works too

like image 88
Smit Avatar answered Oct 15 '22 14:10

Smit