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

FabricJSJavascriptHTML5 Canvas

#### Complete Python Prime Pack for 2023

9 Courses     2 eBooks

#### Artificial Intelligence & Machine Learning Prime Pack

6 Courses     1 eBooks

#### Java Prime Pack 2023

8 Courses     2 eBooks

In this tutorial, we are going to learn how to set the size of superscript 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 superscript property where we can specify its size.

## Syntax

new fabric.Text(text: String , { superscript : {“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 superscript is a property.

## Options Keys

• superscript − This property accepts an Object as value. Inside this object we can specify the size and baseline of our superscript text. The size determines how small our superscript will be whereas the baseline value determines how further below our superscript will be placed. The default value for size is 0.6.

## Example 1

Appearance of the Text object when default value of size is used

Let’s see a code example to see how our text object looks when the default value of size is used. In this case, our superscript text will have its default size value.

<!DOCTYPE html>
<html>
<!-- Adding the Fabric JS Library-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/510/fabric.min.js"></script>
<body>
<h2>Appearance of the Text object when default value of size is used</h2>
<p>You can see the default size of superscript 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",
superscript: {"size": 0.6}
});

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

// Add it to the canvas
</script>
</body>
</html>


## Example 2

Using the setSuperscript method with superscript property

In this example, we will see how by using the setSuperscript method in conjunction with the superscript property we can manipulate the size of our superscript. Here we have specified the size as 0.3, which is our fontSize factor, which essentially makes the size of our superscript text as 12px (0.3*40=12).

<!DOCTYPE html>
<html>
<!-- Adding the Fabric JS Library-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/510/fabric.min.js"></script>
<body>
<h2> Using the setSuperscript method with superscript property</h2>
<p>You can see that the supercript text appears 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",
fontSize: 40,
superscript: {"size": 0.3}
});

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

// Add it to the canvas