How to set the position of Textbox from top using FabricJS?

In this tutorial, we are going to set the position of Textbox from top using FabricJS. The top property allows us to manipulate the position of the object. We can customize, stretch or move around the text written in a textbox. In order to create a textbox, we will have to create an instance of fabric.Textbox class and add it to the canvas. By default, the top position is relative to the canvas's top edge.

Syntax

new fabric.Textbox(text: String, { top: Number }: Object)

Parameters

  • text ? This parameter accepts a String which is the text string that we want to display inside our textbox.

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

Options Keys

  • top: This property accepts a Number which allows us to set the distance from top of the canvas for textbox.

Example 1

Default appearance of the Textbox object

Let's see a code example to understand how our textbox object would appear when the top property is not used.

<!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 the Textbox object</h2>
    <p>You can see the default appearance of Textbox in this example</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 textbox object
        var textbox = new fabric.Textbox("Tomorrow is often the busiest day of the week.", {
            backgroundColor: "#e3dac9",
            width: 400,
            left: 70,
            fill: "green",
            stroke: "black",
        });
        
        // Add it to the canvas
        canvas.add(textbox);
    </script>
</body>
</html>

Example 2

Passing the top property as key with a custom value

In this example, we are passing the top property as key, with a value of 70. This means that our textbox object will be placed at a distance of 70px from the top.

<!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>Passing the top property as key with a custom value</h2>
    <p>You can see that now the textbox is placed at a distance of 70px from the top</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 textbox object
        var textbox = new fabric.Textbox("Tomorrow is often the busiest day of the week.", {
            backgroundColor: "#e3dac9",
            width: 400,
            left: 70,
            top: 70,
            fill: "green",
            stroke: "black",
        });
        
        // Add it to the canvas
        canvas.add(textbox);
    </script>
</body>
</html>

Key Points

  • The top property positions the textbox vertically from the top edge of the canvas

  • Values are in pixels and can be positive numbers

  • When top is not specified, the textbox appears at the default position (near the top of the canvas)

  • Combine top with left to position the textbox precisely on the canvas

Conclusion

The top property in FabricJS provides precise control over textbox vertical positioning. Use it to place your textboxes exactly where you need them on the canvas for better layout control.

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

648 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements