FabricJS – Capturing the Stream of a Polygon Converted to a HTMLCanvasElement?

We can create a Polygon object by creating an instance of fabric.Polygon. A polygon object can be characterized by any closed shape consisting of a set of connected straight line segments. 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 convert a polygon object into HTMLCanvasElement we use the toCanvasElement method. It returns the DOM element of type HTMLCanvasElement, an interface which inherits its properties and methods from the HTMLElement interface. We use the captureStream method to capture stream of Polygon converted to HTMLCanvasElement. It returns CanvasCaptureMediaStreamTrack which is a real-time stream capture of the surface of the canvas.

Syntax

HTMLCanvasElement.captureStream(frameRate)

Parameters

frameRate (optional): A number specifying the frame rate for the captured stream. If omitted, a new frame is captured each time the canvas changes.

Return Value

Returns a MediaStream object containing a CanvasCaptureMediaStreamTrack that captures the canvas content in real-time.

Example 1: Using the toCanvasElement Method

Let's see a code example to see the logged output when the toCanvasElement method is used. On using the toCanvasElement method, the DOM element of type HTMLCanvasElement is returned. The HTMLCanvasElement interface provides various methods and properties for changing the presentation of the canvas. You can open the console from dev tools to see that the DOM element of type HTMLCanvasElement is being returned.

<!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 toCanvasElement method</h2>
   <p>You can open console from dev tools to see the logged output</p>
   <canvas id="canvas"></canvas>
   <script>
      // Initiate a canvas instance
      var canvas = new fabric.Canvas("canvas");
      canvas.setWidth(document.body.scrollWidth);
      canvas.setHeight(250);
      
      // Initiate a polygon object
      var polygon = new fabric.Polygon(
         [
            { x: 600, y: 310 },
            { x: 650, y: 450 },
            { x: 600, y: 480 },
            { x: 550, y: 480 },
            { x: 450, y: 460 },
            { x: 300, y: 210 },
         ],
         {
            fill: "#778899",
            stroke: "blue",
            strokeWidth: 5,
            top: 50,
            left: 100,
            scaleX: 0.5,
            scaleY: 0.5,
         }
      );
      
      // Adding it to the canvas
      canvas.add(polygon);
      
      // Using toCanvasElement method
      console.log(
         "The output on using toCanvasElement method is:",
         polygon.toCanvasElement()
      );
   </script>
</body>
</html>
The output on using toCanvasElement method is: [HTMLCanvasElement]

Example 2: Using captureStream Method

Let's see a code example to see the logged output when the captureStream method is used along with toCanvasElement method to find the CanvasCaptureMediaStreamTrack. We can open the console from dev tools to see the real time stream capture object of the Polygon object.

<!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 captureStream method</h2>
   <p>You can open console from dev tools to see that the CanvasCaptureMediaStreamTrack is being returned</p>
   <canvas id="canvas"></canvas>
   <script>
      // Initiate a canvas instance
      var canvas = new fabric.Canvas("canvas");
      canvas.setWidth(document.body.scrollWidth);
      canvas.setHeight(250);
      
      // Initiate a polygon object
      var polygon = new fabric.Polygon(
         [
            { x: 600, y: 310 },
            { x: 650, y: 450 },
            { x: 600, y: 480 },
            { x: 550, y: 480 },
            { x: 450, y: 460 },
            { x: 300, y: 210 },
         ],
         {
            fill: "#778899",
            stroke: "blue",
            strokeWidth: 5,
            top: 50,
            left: 100,
            scaleX: 0.5,
            scaleY: 0.5,
         }
      );
      
      // Adding it to the canvas
      canvas.add(polygon);
      
      // Using toCanvasElement method
      var polygonCanvas = polygon.toCanvasElement({
         width: 200,
      });
      
      // Using captureStream method
      var stream = polygonCanvas.captureStream();
      console.log(
         "The real time stream capture of the canvas is as follows:",
         stream
      );
      console.log("Stream tracks:", stream.getTracks());
   </script>
</body>
</html>
The real time stream capture of the canvas is as follows: [MediaStream]
Stream tracks: [CanvasCaptureMediaStreamTrack]

Common Use Cases

  • Recording Canvas Content: Capture real-time drawing or animation for video recording
  • Live Streaming: Stream canvas content to other users in real-time
  • Video Conferencing: Share canvas content as a video source
  • Screen Sharing: Broadcast canvas-based applications

Browser Compatibility

The captureStream() method is supported in modern browsers including Chrome, Firefox, and Safari. Always check for browser support before implementing in production applications.

Conclusion

The combination of FabricJS's toCanvasElement() and HTML5's captureStream() provides a powerful way to capture real-time streams of polygon objects. This enables advanced features like recording, live streaming, and sharing canvas content in web applications.

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

401 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements