Home >Web Front-end >JS Tutorial >How to add shadow to textbox using FabricJS?
In this tutorial, we will learn how to add a shadow to a text box using FabricJS. We can customize, stretch or move the text written in the text box. In order to create a textbox, we have to create an instance of the Fabric.Textbox class and add it to the canvas. Our text box object can be customized in many ways, such as changing its dimensions, adding a background color, or even adding a shadow to it. We can add a shadow to the text box using the shadow property.
new fabric.Textbox(text: String, { shadow : fabric.Shadow }: Object)
text - This parameter accepts a string which is the text we are usingString Want to display in our text box.
shadow - This property accepts a fabric.Shadow object that allows us to cast Add shadow to Textbox object. For example, to add a shadow to a Textbox object, we need to create a new Fabric.Shadow instance and then assign that instance to the Shadow property.
Passing a shadow object to the shadow property
Let’s look at a code Example to see how to assign a value to the Shadow property in order to add a Shadow to our TextBox object. First, we need to create a new instance of fabric.Shadow and then assign that instance to our shadow property.
<!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>Passing the shadow object to the shadow property</h2> <p>You can see that a blue shadow has been added to the textbox</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 shadow instance var shadow = new fabric.Shadow({ color: "blue", blur: 20, }); // Initiate a textbox object var textbox = new fabric.Textbox("Try Again. Fail again. Fail better.", { backgroundColor: "#fffff0", width: 400, left: 110, top: 70, fill: "violet", strokeWidth: 2, stroke: "blue", textAlign: "center", shadow: shadow, }); // Add it to the canvas canvas.add(textbox); </script> </body> </html>
Passing RGBA values to the shadow object
We can also adjust the shadow and give it a blurry appearance by specifying a shadow RGBA values, representing red, green, blue and alpha. Alpha determines the opacity of the color.
<!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>Passing an RGBA value to the shadow object</h2> <p>You can see the shadow created using RGBA colour value</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 shadow instance var shadow = new fabric.Shadow({ color: "rgba(0,0,205, 0.7)", blur: 20, }); // Initiate a textbox object var textbox = new fabric.Textbox("Try Again. Fail again. Fail better.", { backgroundColor: "#fffff0", width: 400, left: 110, top: 70, fill: "violet", strokeWidth: 2, stroke: "blue", textAlign: "center", shadow: shadow, }); // Add it to the canvas canvas.add(textbox); </script> </body> </html>
The above is the detailed content of How to add shadow to textbox using FabricJS?. For more information, please follow other related articles on the PHP Chinese website!