visit
This JavaScript library simplifies working with HTML5 canvas in web applications with Fabric.js. It serves as an interactive platform for effortlessly creating and manipulating objects and shapes on the canvas.
We've got everything you need to integrate Fabric.js
into your project! We'll walk you through getting started with Fabric.js
in Angular 13
in this blog post.
To begin using Fabric.js
in your Angular 13
project, you have two options for installation: manual installation or using the npm module.
Start by downloading the Fabric.js
library from the official website
Create a new folder in your Angular project's directory, such as lib
, and place fabric.js
inside it.
Open your Angular project's angular.json
file and locate the scripts
array under the architect > build > options
section. You can add the path to the "fabric.js" file within the "scripts" array by following this example:
"scripts": [
"src/lib/fabric.js"
]
Save the angular.json
file.
Run the following command to install Fabric.js
npm install fabric
Import fabric.js
in the component typescript file:
import { fabric } from 'fabric';
To incorporate Fabric.js into your component's HTML file, simply include a canvas element with an assigned id. Customize the dimensions of the canvas by utilizing the width
and height
attributes.
For example:
<canvas id="myCanvas" width="400" height="400"></canvas>
import { fabric } from 'fabric';
In your component's class, you can create a new instance of the Fabric.js canvas by referencing the canvas element using its unique id
attribute.
For example:
const canvas = new fabric.Canvas('myCanvas');
Let's now incorporate a basic "Hello World" text box onto our canvas. We can achieve this by creating a new instance of fabric.Textbox
and defining the desired text content
, dimensions
, cursor
color
, and position
.
const text = new fabric.Textbox('Hello World', {
width: 200,
height: 100,
fontSize: 24,
cursorColor: 'blue',
left: 50,
top: 50
});
canvas.add(text);
To add shapes to your canvas, you can use the provided shape classes from the fabric
module. Here's an example of adding a circle to the canvas:
const circle = new fabric.Circle({
radius: 50,
fill: 'red',
left: 100,
top: 100
});
canvas.add(circle);
You can customize the properties of the shape, according to your requirements.
const rectangle = new fabric.Rect({
width: 100,
height: 50,
fill: 'blue',
left: 200,
top: 200
});
rectangle.set({
fill: 'green',
width: 150,
height: 75
});
canvas.add(rectangle);
Fabric.js provides event-handling capabilities, allowing you to respond to user interactions on the canvas. You can listen for events such as object selection, mouse clicks, or dragging. Here's an example of handling the object:selected
event:
canvas.on('object:selected', (event) => {
const selectedObject = event.target;
console.log('Selected object:', selectedObject);
});
In this example, we listen for the object:selected
event and log the selected object to the console.
With its interactive platform, you can effortlessly create and manipulate objects and shapes on the canvas. Throughout this blog post, we have presented a comprehensive guide that will assist you in initiating your journey with Fabric.js in an Angular 13 project.