Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to set the size of superscript with Text using FabricJS?
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: Default Size Value
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>
<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 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 \ntext 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
canvas.add(text);
</script>
</body>
</html>
Example 2: Custom Size Value
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>
<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 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 \ntext 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
canvas.add(text);
</script>
</body>
</html>
How It Works
The superscript size property works as a multiplication factor with the base font size. For example, if your base fontSize is 40px and you set superscript size to 0.3, the superscript text will be rendered at 12px (40 × 0.3). The setSuperscript() method takes two parameters: the start index and end index of the text portion to be converted to superscript.
Conclusion
The superscript property in FabricJS allows you to control the size of superscript text using a size factor. By adjusting this value, you can make superscript text larger or smaller relative to the base font size.
