- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
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
text 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
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/fabric.js/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
text 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>
- Related Articles
- How to set the baseline of subscript with Text using FabricJS?
- How to set the size of superscript with Text using FabricJS?
- How to add superscript with Text using FabricJS?
- How to shift the baseline of individual characters in Text using FabricJS?
- How to set the background colour of text lines with Text using FabricJS?
- How to set the size of subscript with Text using FabricJS?
- How to set the text alignment of Text using FabricJS?
- How to set the text overline of Text using FabricJS?
- How to set the background colour of text lines with IText using FabricJS?
- How to set the text overline of IText using FabricJS?
- How to set the duration of animation in Text using FabricJS?
- How to shift the baseline of individual characters in IText using FabricJS?
- How to set the angle of rotation of a Text using FabricJS?
- How to set the horizontal origin of transformation of Text using FabricJS?
- How to set the style of controlling corners of Text using FabricJS?
