# How to set the size of subscript with Text using FabricJS?

FabricJSJavascriptHTML5 Canvas

#### Complete Python Prime Pack

9 Courses     2 eBooks

#### Artificial Intelligence & Machine Learning Prime Pack

6 Courses     1 eBooks

#### Java Prime Pack

9 Courses     2 eBooks

In this tutorial, we are going to learn how to set the size of subscript with Text using FabricJS. We can display text on canvas by adding an instance of fabric.Text. Not only does it allow us to move, scale and change the dimensions of the text but it also provides additional functionality like text alignment, text decoration, line height which can be obtained by the properties textAlign, underline and lineHeight respectively. We can also use the subscript property where we can specify its size.

## Syntax

new fabric.Text(text: String , { subscript : {“size”: Number, "baseline": Number}: Object }: Object)


## Parameters

• text − This parameter accepts a String which is the text string that we want to display.

• options (optional) − This parameter is an Object which provides additional customizations to our text. Using this parameter colour, cursor, border width and a lot of other properties can be changed related to the object of which subscript is a property.

## Options Keys

• subscript − This property accepts an Object as value. Inside this object we can specify the size and baseline of our subscript text. The size determines how small our subscript will be whereas the baseline value determines how further below our subscript will be placed. The default values for size (fontSize factor) and baseline (downwards baseline shift factor) are 0.6 and 0.11 respectively.

## Example 1

Appearance of the Text object when only setSubscript is used

Let’s see a code example to see how our text object looks when only the setSubscript method is used. In this case, our subscript text will have its default size, which is 0.6.

<!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>Appearance of the Text object when only setSubscript is used</h2>
<p>You can see the default size of subscript text</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 text object
var text = new fabric.Text("Add sample text here.", {
width: 300,
left: 50,
top: 70,
fill: "green",
});

// Using the setSubscript method
text.setSubscript(0,4)

// Add it to the canvas
canvas.add(text);
</script>
</body>
</html>


## Example 2

Using the setSubscript method with subscript property

In this example, we will see how by using the setSubscript method in conjunction with the subscript property we can manipulate the size of our subscript. Here we have specified the size as 0.3 which makes our subscript text appear smaller than usual.

<!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 setSubscript method with subscript property</h2>
<p>You can see that subscript text now appears to be smaller</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 text object
var text = new fabric.Text("Add sample text here.", {
width: 300,
left: 50,
top: 70,
fill: "green",
subscript: {"size": 0.3}
});

// Using the setSubscript method
text.setSubscript(0,4)

// Add it to the canvas
canvas.add(text);
</script>
</body>
</html>

Updated on 14-Sep-2022 11:29:04

Advertisements