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 subscript with Text using FabricJS?
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: Default Subscript Size
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 \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 Size
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 \ntext 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>
Key Points
The
setSubscript()method defines which characters in the text should be displayed as subscriptThe
subscriptproperty object controls the appearance withsizeandbaselineparametersDefault subscript size is 0.6 (60% of normal font size)
Smaller size values create more pronounced subscript effects
Conclusion
FabricJS provides flexible subscript functionality through the setSubscript() method and subscript property. You can easily customize both the positioning and size of subscript text to match your design requirements.
