FabricJS made canvas drawing easy as pie

A developer who writes somethings useful.
Search for a command to run...

A developer who writes somethings useful.
No comments yet. Be the first to comment.
Welcome to the Crawl Budget Circus! 🎪 Step right up, ladies and gentlemen! Today, we’re diving into the thrilling world of crawl budgets—the magical limit that determines how much time Googlebot spends crawling your website. Think of it as a circus ...

A simple math trick discovered by Indian mathematician D. R. Kaprekar

Efficiency at Your Fingertips in Visual Studio Code

Tracing the Roots of the World Wide Web and HTML Evolution

Unlocking Unpopular Functions in JavaScript for Better Code

If you ever thought to take a hand at Canvas drawing and hesitate to it because geometry is not your subject, you should first start with FabricJS.
Canvas native API could be confusing if you just have started on it.
Let's take a simple example to draw a rectangle, rotated at 45 degrees.
By using native canvas API.
var canvasEl = document.getElementById('c');
var context = canvasEl.getContext('2d');
context.fillStyle = 'red';
context.translate(100, 100);
context.rotate(Math.PI / 180 * 45); //Converts value in radian
context.fillRect(-10, -10, 20, 20);
Same thing but by using Febirc.
var canvas = new fabric.Canvas('c');
// create a rectangle with angle=45
var rect = new fabric.Rect({
left: 100,
top: 100,
fill: 'red',
width: 20,
height: 20,
angle: 45
});
canvas.add(rect);

Drawing with Fabric is self-explanatory, isn't it! Whereas in canvas example, someone probably wonder what is getContext for, what is translate doing? why extra overhead to convert a value from degree to radian?
Fabric abstracts much more complex canvas operations and in addition, it exposes rich observing events and object events which makes integration and customization extremely smooth.
I have made one sample using path:created observing event and freeDrawingBrush API. This event triggers each time when path drawing ends and provides full detail of the drawn path. Using that data, the same path is automatically drawn right beside the canvas on another canvas(Mirror Canvas).
The full sample was originally posted at http://fabricjs.com/freedrawing. For the sake of simplicity, I have removed different types of drawing modes and added mirror drawing canvas.
Credits: