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 baseline of superscript with Text using FabricJS?
In this tutorial, we are going to learn how to set the baseline 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 baseline.
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 and baseline is 0.6 and 0 respectively.
Example 1: Default Baseline Value
Appearance of the Text object when default value of baseline is used
Let's see a code example to see how our text object looks when we pass the default value of baseline. In this case the default value of our baseline is 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>Appearance of the Text object when default value of baseline is used</h2>
<p>You can see the default baseline 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: 60,
top: 70,
fill: "green",
superscript: {"size": 1, "baseline": ""}
});
// Using the setSuperscript method
text.setSuperscript(0,4)
// Add it to the canvas
canvas.add(text);
</script>
</body>
</html>
Example 2: Custom Baseline 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 baseline of our superscript. Here we have specified the baseline as -0.5 due to which the shift occurs in the upward direction.
<!DOCTYPE html>
<html>
<head>
<!-- Adding the Fabric JS Library-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/510/fabric.min.js"></script>
</head>
<body>
<h2> Using the setSuperscript method with superscript property</h2>
<p>You can see that the baseline of superscript text has changed</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: 60,
top: 70,
fill: "green",
superscript: {"size": 0.3, "baseline": -0.5}
});
// Using the setSuperscript method
text.setSuperscript(0,4)
// Add it to the canvas
canvas.add(text);
</script>
</body>
</html>
How Baseline Values Work
The baseline property controls the vertical positioning of superscript text:
- Positive values move the superscript downward
- Negative values move the superscript upward
- Default value (0) keeps the superscript at normal position
Conclusion
The baseline property in FabricJS superscript allows precise control over vertical positioning of superscript text. Use negative values to move superscript higher and positive values to move it lower than the default position.
