How to get the current colour of the character at cursor in IText using FabricJS?


In this tutorial, we are going to learn about how to get the current colour of the character at cursor in IText using FabricJS. The IText class was introduced in FabricJS version 1.4, extends fabric.Text and is used to create IText instances. An IText instance gives us the freedom to select, cut, paste or add new text without additional configurations. There are also various supported key combinations and mouse/touch combinations which make text interactive which are not provided in Text.

Textbox, however, which is based on IText allows us to resize the text rectangle and wraps lines automatically. This is not true for IText as height is not adjusted based on the wrapping of lines. We can manipulate our IText object by using various properties. Likewise, we can get the cursor character colour by using the getCurrentCharColor method. However, if the itext object has a pattern, gradient or fill then the colour of the character at the current cursor will be returned. The default fill colour for an itext object is rgb(0,0,0) which is the colour “black”. Thus, if nothing has been specified rgb(0,0,0) will be returned as the logged output.

Syntax

getCurrentCharColor(): String | fabric.Gradient | fabric.Pattern

Example 1

Using the getCurrentCharColor method to get default value

Let’s see a code example to see how the getCurrentCharColor method behaves when a fill colour is not provided. The logged output will return us the default character colour.

<!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 getCurrentCharColor method to get default value</h2> <p>You can open console from dev tools and see the logged output</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 an itext object var itext = new fabric.IText( "Add sample text here.
Lorem ipsum dolor sit amet"
,{ width: 300, left: 60, top: 70, } ); // Add it to the canvas canvas.add(itext); // Using getCurrentCharColor method console.log("The current character colour is: ", itext.getCurrentCharColor()); </script> </body> </html>

Example 2

Using the getCurrentCharColor method for an itext object with fill

Let’s see a code example to see the logged output when a fill is used. As we can see, color of the fill will be returned.

<!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 getCurrentCharColor method for an itext object with fill</h2> <p>You can open console from dev tools and see the logged output for fill</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 an itext object var itext = new fabric.IText( "Add sample text here.
Lorem ipsum dolor sit amet"
,{ width: 300, left: 60, top: 70, fill: "red", } ); // Add it to the canvas canvas.add(itext); // Using getCurrentCharColor method console.log( "The current character colour is: ", itext.getCurrentCharColor() ); </script> </body> </html>

Updated on: 13-Sep-2022

108 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements