How to add shadow to a canvas circle using Fabric.js?


We can add a shadow to a canvas circle using Fabric.js by creating a new circle object, then setting the "shadow" property of the object to an object containing the desired shadow properties such as color and offset. 

Fabric.js is an open-source JavaScript library that allows developers to create and manipulate HTML5 canvas elements with ease. It simplifies the process of creating, editing and animating canvas elements by providing a set of powerful and flexible APIs.

With Fabric.js, developers can create complex canvas graphics, animations, and interactive elements without having to deal with the low-level canvas API. It also provides an object-oriented structure that makes it easy to work with canvas elements as objects and perform common operations like scaling, rotating, and moving them. It is widely used in web development, game development, and other interactive applications that require canvas capabilities.

Fabric.js is a library for working with object-oriented canvas graphics. It provides an easy-to-use API for creating and manipulating canvas elements, such as shapes, text, and images. With Fabric.js, you can create complex canvas graphics and animations using JavaScript, without the need for low-level canvas API calls. It also provides a set of built-in objects, such as rectangles, circles, and text, that can be easily manipulated using the library's API.

Additionally, fabric.js provides many useful features such as events handling, undo/redo, object serialization, and more. It also has a wide variety of built-in filters and image-manipulation functions.

Fabric.js is widely used in web development, particularly in creating interactive graphics and charts, and creating user interface elements such as image editors and custom form inputs.

Approach

  • Create a canvas object using Fabric.js −

var canvas = new fabric.Canvas('canvas');
  • Create a circle object −

var circle = new fabric.Circle({
   radius: 50,
   fill: 'red',
   left: 100,
   top: 100
});
  • Set the shadow properties for the circle object −

circle.setShadow({
   color: 'black',
   offsetX: 5,
   offsetY: 5,
   blur: 10
});
  • Add the circle object to the canvas −

canvas.add(circle);
  • Render the canvas −

canvas.renderAll();

Note − You can adjust the offsetX, offsetY and blur values to adjust the position and intensity of the shadow. Also, you can use different colors for the shadow.

Example

<!DOCTYPE html>
<html>
<head>
   <script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/3.6.3/fabric.min.js"></script>
</head>
   <body>
      <canvas id="canvas"></canvas> 
      <script>
         var canvas = new fabric.Canvas('canvas');
         var circle = new fabric.Circle({
            radius: 50,
            fill: 'red',
            left: 100,
            top: 100
         });
         circle.setShadow({
            color: 'black',
            offsetX: 5,
            offsetY: 5,
            blur: 10
         });
         canvas.add(circle);
         canvas.renderAll();
      </script>
   </body>
</html>

In this example, we first include the Fabric.js library and then create a canvas element in the HTML body. We then use Fabric.js to create a circle object, set its properties, and add it to the canvas. We also set the shadow properties for the circle object using the "setShadow" method. Finally, we render the canvas to display the circle with the shadow.

Updated on: 13-Feb-2023

196 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements