How to customize the viewport of the canvas using FabricJS?

In this article, we are going to learn how to customize the viewport of the canvas using FabricJS. The viewport is the visible area of the canvas that users can see. We can customize the viewport using the viewportTransform property, which allows us to control transformations like scaling, translation, and skewing of the entire canvas view.

What is viewportTransform?

The viewportTransform property is a transformation matrix represented as an array of 6 values: [scaleX, skewY, skewX, scaleY, translateX, translateY]. This matrix transforms the entire canvas coordinate system, affecting how all objects are displayed.

Syntax

new fabric.Canvas(element, { 
    viewportTransform: [scaleX, skewY, skewX, scaleY, translateX, translateY] 
})

Parameters

  • element ? The <canvas> element or its ID string where the FabricJS canvas will be initialized.

  • options (optional) ? Configuration object for canvas customization. The viewportTransform property accepts an array of 6 values:

    • [0] - scaleX: Horizontal scaling
    • [1] - skewY: Vertical skewing
    • [2] - skewX: Horizontal skewing
    • [3] - scaleY: Vertical scaling
    • [4] - translateX: Horizontal translation
    • [5] - translateY: Vertical translation

    Default value: [1, 0, 0, 1, 0, 0] (no transformation)

Example 1: Complex Transformation

This example demonstrates a complex transformation with scaling, skewing, and translation applied to the viewport.

<!DOCTYPE html>
<html>
<head>
   <script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/510/fabric.min.js"></script>
</head>
<body>
   <h2>Complex Viewport Transformation</h2>
   <canvas id="canvas"></canvas>
   <script>
      // Create canvas with complex viewport transformation
      var canvas = new fabric.Canvas("canvas", {
         viewportTransform: [0.7, 0.1, 0.5, 0.9, 20, 50]
      });
      
      // Add a circle to visualize the transformation
      var circle = new fabric.Circle({
         left: 215,
         top: 100,
         radius: 50,
         fill: "red",
      });
      
      canvas.add(circle);
      canvas.setWidth(400);
      canvas.setHeight(250);
   </script>
</body>
</html>

Example 2: Simple Scale and Translation

This example shows a simpler transformation that scales the viewport to 80% and translates it 50 pixels to the right and down.

<!DOCTYPE html>
<html>
<head>
   <script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/510/fabric.min.js"></script>
</head>
<body>
   <h2>Scaled and Translated Viewport</h2>
   <canvas id="canvas"></canvas>
   <script>
      // Create canvas with scale and translation only
      var canvas = new fabric.Canvas("canvas", {
         viewportTransform: [0.8, 0, 0, 0.8, 50, 50]
      });
      
      // Add a circle to see the effect
      var circle = new fabric.Circle({
         left: 215,
         top: 100,
         radius: 50,
         fill: "red"
      });
      
      canvas.add(circle);
      canvas.setWidth(400);
      canvas.setHeight(250);
   </script>
</body>
</html>

Understanding the Matrix Values

Index Property Effect Default
0 scaleX Horizontal scaling (1 = normal, 0.5 = half size) 1
1 skewY Vertical skewing 0
2 skewX Horizontal skewing 0
3 scaleY Vertical scaling (1 = normal, 2 = double size) 1
4 translateX Horizontal offset in pixels 0
5 translateY Vertical offset in pixels 0

Common Use Cases

  • Zoom functionality: Use scale values less than 1 to zoom out, greater than 1 to zoom in
  • Pan/scroll: Use translateX and translateY to move the viewport
  • Perspective effects: Combine skewing with scaling for visual effects

Conclusion

The viewportTransform property in FabricJS provides powerful control over how the entire canvas is displayed. By understanding the 6-value transformation matrix, you can implement features like zoom, pan, and visual effects to enhance user interaction with your canvas applications.

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

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements