Home > Article > Web Front-end > How to get the currently selected style in text using FabricJS?
In this tutorial, we will learn how to get the style of the currently selected text using FabricJS. We can display text on the canvas by adding an instance of Fabric.Text. Not only does it allow us to move, scale and change the dimensions of text, but it also provides additional features such as text alignment, text decoration, line height, which are available through the properties textAlign, underline and lineHeight respectively. We can also find the styles of the current selection using the getSelectionStyles method.
getSelectionStyles(startIndexopt, endIndexopt, completeopt)
startIndexopt - This parameter accepts a number, indicating the starting index to obtain the style.
endIndexopt - This parameter accepts a Number, indicating the end index of the style to be obtained.
completeopt - This parameter accepts a Boolean value that determines whether to obtain the complete style.
Use getSelectionStyles method
Let's look at a code example to see the output logged when using the getSelectionStyles method. In this case, styles from the 0th index up to the 4th index will be displayed.
<!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 getSelectionStyles method</h2> <p>You can open console from dev tools and see that the value is being displayed in the console</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 sampletext here", { width: 300, fill: "green", fontWeight: "bold", }); // Add it to the canvas canvas.add(text); // Using getSelectionStyles method console.log("The style is", text.getSelectionStyles(0, 5, true)); </script> </body> </html>
Use getSelectionStyles method and pass different values
Let's look at a code example to see the output logged when the getSelectionStyles method is passed different values. In this case, the recorded output will contain the character styles at the 4th and 5th index.
<!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 getSelectionStyles method and passing different values</h2> <p>You can open console from dev tools and see that the value is being displayed in the console</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 sampletext here", { width: 300, fill: "green", fontWeight: "bold", styles: { 0: { 5: { fontSize: 55, fill: "blue", fontStyle: "oblique", }, 4: { fontSize: 45, fill: "pink", fontWeight: "bold", }, }, }, }); // Add it to the canvas canvas.add(text); // Using getSelectionStyles method console.log("The style is", text.getSelectionStyles(4, 6, true)); </script> </body> </html>
The above is the detailed content of How to get the currently selected style in text using FabricJS?. For more information, please follow other related articles on the PHP Chinese website!