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 subscript with Text using FabricJS?
In this tutorial, we are going to learn how to set the baseline 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 baseline value.
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: Default Subscript Baseline
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 baseline 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 only setSubscript is used</h2>
<p>You can see the default baseline value 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 \ntext 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: Custom Subscript Baseline
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 baseline value of our subscript. Here we have specified the baseline as 0.55 which makes our subscript text have a lower baseline.
<!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 has a lower baseline</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",
subscript: {"size": 0.4,"baseline": 0.55}
});
// Using the setSubscript method
text.setSubscript(0,4)
// Add it to the canvas
canvas.add(text);
</script>
</body>
</html>
Key Points
- The
subscriptproperty accepts an object withsizeandbaselineproperties - Default baseline value is 0.11, which controls how far below the text the subscript appears
- Higher baseline values (like 0.55) move the subscript further down
- The
setSubscript()method defines which characters become subscript based on start and end positions
Conclusion
FabricJS allows precise control over subscript positioning through the baseline property. By adjusting the baseline value, you can customize how far below the main text your subscript appears, providing flexibility for various text formatting needs.
