Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setup the Fabric.js?

I am very new to fabric.js. I have downloaded the fabric.js, but I don't know how to start it. For example:

<html>
<head>
    <script src="fabric.min.js"></script>
</head>

<body>
    <canvas id="canvas" width="800" height="450" style="border:1px solid #000000"></canvas>
    <script>
        var canvas = new fabric.Canvas('c1');
        canvas.add(new fabric.Circle({ radius: 30, fill: '#f55', top: 100, left: 100 }));

        canvas.selectionColor = 'rgba(0,255,0,0.3)';
        canvas.selectionBorderColor = 'red';
        canvas.selectionLineWidth = 5;
    </script>
</body>
</html>

In this script I supposed to see a circle. I followed this example: http://fabricjs.com/customization/

But why I couldn't see anything; what I am missing?

like image 464
Gobi Avatar asked May 22 '12 03:05

Gobi


People also ask

How does fabric JS work?

Fabric. js is based on HTML5 canvas, so just put a canvas object into an HTML file, with an id, and that's it. In the controller's AfterContentInit lifecycle hook (so the canvas was rendered inside the nice wrapper): this.

Why do we use fabric JS?

With the help of Fabric. js, every shape drawn on the canvas is a JavaScript object with properties and methods. Using these properties and methods we can manipulate the shape on the canvas without having to redraw the objects on the canvas manually.

Is fabric JS open source?

js is a Javascript HTML5 canvas library. It is a fully open-source project with many contributions over the years.


1 Answers

The parameter of fabric.Canvas should match the canvas id in HTML.

Change the line

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

to

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

This should work.

like image 183
santhgates Avatar answered Oct 20 '22 12:10

santhgates