How to add image smoothing for an Image using FabricJS?

In this tutorial, we are going to show how you can add image smoothing for an Image using FabricJS. Smoothing gives a smooth effect to the image. We can create an Image object by creating an instance of fabric.Image. Since it is one of the basic elements of FabricJS, we can also easily customize it by applying properties like angle, opacity etc. In order to add image smoothing, we use the imageSmoothing property.

Syntax

new fabric.Image(element: HTMLImageElement | HTMLCanvasElement | HTMLVideoElement | String, 
                {imageSmoothing: Boolean}: Object, 
                callback: function)

Parameters

  • element ? This parameter accepts HTMLImageElement, HTMLCanvasElement, HTMLVideoElement or String which denotes the image element. The String should be a URL and would be loaded as an image.

  • options (optional) ? This parameter is an Object which provides additional customizations to our object. Using this parameter origin, stroke width and a lot of other properties can be changed related to the image object of which imageSmoothing is a property.

  • callback (optional) ? This parameter is a function which is to be called after eventual filters are applied.

Options Keys

  • imageSmoothing ? This property accepts a Boolean value which indicates whether the canvas will use image smoothing while painting the image or not. Its default value is true.

Default Appearance of Image object

Example

Let's see a code example of how the Image object appears when the imageSmoothing property is not used. In this case, the default value will be used which is true and hence the canvas will use image smoothing.

<!DOCTYPE html>
<html>
<head>
   <!-- Adding the Fabric JS Library-->
   <script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/510/fabric.min.js"></script>
</head>
<body>
   <h2>Default appearance of Image object</h2>
   <p>You can see that image smoothing has been applied by default</p>
   <canvas id="canvas"></canvas>
   <img src="https://www.tutorialspoint.com/images/logo.png" id="img1" style="display: none" />
   <script>
      // Initiate a canvas instance
      var canvas = new fabric.Canvas("canvas");
      canvas.setWidth(document.body.scrollWidth);
      canvas.setHeight(250);
      
      // Initiating the image element
      var imageElement = document.getElementById("img1");
      
      // Initiate an Image object
      var image = new fabric.Image(imageElement, {
         top: 50,
         left: 110,
      });
      
      // Add it to the canvas
      canvas.add(image);
   </script>
</body>
</html>

Using the imageSmoothing property and passing it a false value

Example

In this example, we have used the imageSmoothing property and assigned it a false value. Therefore, the canvas will no longer use image smoothing for painting the image.

<!DOCTYPE html>
<html>
<head>
   <!-- Adding the Fabric JS Library-->
   <script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/510/fabric.min.js"></script>
</head>
<body>
   <h2>Using the imageSmoothing property and passing it a false value</h2>
   <p>You can see that image smoothing is no longer functioning</p>
   <canvas id="canvas"></canvas>
   <img src="https://www.tutorialspoint.com/images/logo.png" id="img1" style="display: none" />
   <script>
      // Initiate a canvas instance
      var canvas = new fabric.Canvas("canvas");
      canvas.setWidth(document.body.scrollWidth);
      canvas.setHeight(250);
      
      // Initiating the image element
      var imageElement = document.getElementById("img1");
      
      // Initiate an Image object
      var image = new fabric.Image(imageElement, {
         top: 50,
         left: 110,
         imageSmoothing: false,
      });
      
      // Add it to the canvas
      canvas.add(image);
   </script>
</body>
</html>

Key Points

  • Image smoothing is enabled by default in FabricJS (imageSmoothing: true)
  • Setting imageSmoothing: false creates a more pixelated, sharp appearance
  • Image smoothing affects how the canvas renders scaled or transformed images
  • This property is particularly useful when working with pixel art or when you want crisp edges

Conclusion

The imageSmoothing property in FabricJS controls whether images are rendered with smooth interpolation or sharp pixels. Use false for crisp pixel art and true for smooth scaling effects.

Updated on: 2026-03-15T23:19:00+05:30

672 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements