Create Professional-Looking Textboxes in Fabric.js with Strokes and Padding
This blog post covers how to create professional-looking stroked textboxes in Fabric.js, including customization options for padding and rounded corners.
Fabric.js is a powerful JavaScript library that provides a simple way to work with HTML5 canvas elements. It offers a range of features and tools to create interactive and visually appealing web applications. One of its key benefits is the ability to create custom classes, allowing developers to extend its functionality and tailor it to their specific needs.
In this blog, we will explore how to create and use a custom class in Fabric.js to create a stroked textbox with rounded corners and padding. We'll also discuss how to customize this class to fit different use cases. By the end of this blog, you'll have a better understanding of how to create custom classes in Fabric.js and how to leverage them to enhance your canvas applications.
Creating the Stroked Textbox Class
The Stroked Textbox class extends the default Textbox class in Fabric.js and adds several new features, including stroke width, stroke color, padding, and rounded corners. Let's take a look at each line of code in the class:
- type: "textbox": This line specifies the type of object we are creating.
- strokeWidth: 5: This line sets the width of the stroke to 5 pixels.
- strokeColor: "#ffb64f": This line sets the color of the stroke to #ffb64f.
- splitByGrapheme: true: This line specifies that the textbox should be split by graphemes.
- rx: 0: This line sets the value of rx to 0, which determines the degree of rounding on the x-axis.
- ry: 0: This line sets the value of ry to 0, which determines the degree of rounding on the y-axis.
- toObject: This function returns an object that contains all the properties of the Textbox class, as well as the new properties added by the Stroked Textbox class.
- _renderBackground: This function is responsible for rendering the background of the textbox with padding and rounded corners, as well as the stroke.
fabric.TextboxWithPadding = fabric.util.createClass(fabric.Textbox, {
type: "textbox",
strokeWidth: 5, // Define stroke width
strokeColor: "#ffb64f", // Define stroke color
splitByGrapheme: true,
rx: 0, // Define rx value for rounded corners on x-axis
ry: 0, // Define ry value for rounded corners on y-axis
toObject: function () {
return fabric.util.object.extend(this.callSuper("toObject"), {
backgroundColor: this.get("backgroundColor"),
padding: this.get("padding"),
splitByGrapheme: this.get("splitByGrapheme"),
rx: this.get("rx"),
ry: this.get("ry")
});
},
_renderBackground: function (ctx) {
if (!this.backgroundColor) {
return;
}
var dim = this._getNonTransformedDimensions();
ctx.fillStyle = this.backgroundColor;
ctx.fillRect(
-dim.x / 2 - this.padding,
-dim.y / 2 - this.padding,
dim.x + this.padding * 2,
dim.y + this.padding * 2
);
// Add stroke only at the top
ctx.strokeStyle = this.strokeColor;
ctx.lineWidth = this.strokeWidth;
ctx.beginPath();
ctx.moveTo(-dim.x / 2 - this.padding, -dim.y / 2 - this.padding);
ctx.lineTo(-dim.x / 2 - this.padding, dim.y / 2 + this.padding);
ctx.stroke();
ctx.beginPath();
ctx.strokeStyle = this.strokeColor;
ctx.lineWidth = 0.2; // Set line width to 1
ctx.lineTo(dim.x / 2 + this.padding, -dim.y / 2 - this.padding + 1);
ctx.lineTo(dim.x / 2 + this.padding, dim.y / 2 + this.padding - 1);
ctx.strokeStyle = "#9181fc";
ctx.lineWidth = 0.2; // Set line width to 1
ctx.lineTo(dim.x / 2 + this.padding - 1, dim.y / 2 + this.padding);
ctx.lineTo(-dim.x / 2 - this.padding + 1, dim.y / 2 + this.padding);
ctx.strokeStyle = "#9181fc";
ctx.lineWidth = 0.2; // Set line width to 1
ctx.lineTo(-dim.x / 2 - this.padding, dim.y / 2 + this.padding - 1);
ctx.lineTo(-dim.x / 2 - this.padding, -dim.y / 2 - this.padding + 1);
ctx.closePath();
ctx.stroke();
// if there is background color no other shadows
// should be casted
this._removeShadow(ctx);
}
});
Benefits of using the class
By using the Stroked Textbox class, we can create custom textboxes that have unique visual characteristics that are not available in the default Textbox class. This allows us to create more visually appealing and engaging canvas applications that stand out from the crowd.
Using the Stroked Textbox Class
To use the Stroked Textbox class, we simply need to create a new instance of the class and pass in the appropriate parameters. Here is an example of how to use the class to create a textbox with padding, rounded corners, and different stroke colors and widths:
const textbox = new fabric.TextboxWithPadding('Inpur field', {
fontFamily: "Roboto",
// fontSize: 20,
padding: 5,
width: 100,
textAlign: 'left',
//@ts-ignore
// left: rect.left + 20,
//@ts-ignore
// top: rect.top + 10,
selectable: true,
evented: true,
editable: true,
//@ts-ignore
placeholder: "",
splitByGrapheme: true,
// lockScalingX: true,
// lockScalingY: true,
lockMovementX: true,
lockMovementY: true,
fill: '#000000',
fontSize: 10,
// stroke: OBJECT_BORDER_COLOR,
strokeWidth: 1,
backgroundColor: "#d5d1eb",
//@ts-ignore
left: 30,
//@ts-ignore
top: 30,
className: "text_box_with_padding",
includeDefaultValues: true,
intersectsWithFrame: true,
hasControls: false,
hasBorders: true,
});
In this example, we have set the fontFamily, padding, width, textAlign, selectable, evented, editable, splitByGrapheme, fill, fontSize, strokeWidth, backgroundColor, hasControls, and hasBorders properties to customize the appearance and behavior of the textbox.
Customizing the Stroked Textbox Class
To customize the Stroked Textbox class further, we can modify the values of the parameters used in the class, or we can create a new subclass that extends the Stroked Textbox class and adds new functionality. Here are some tips on how to customize the class to fit specific needs:
- Adjust the stroke width: To change the width of the stroke, modify the value of strokeWidth. A higher value will create a thicker stroke, while a lower value will create a thinner stroke. For example, setting strokeWidth to 10 will create a textbox with a thicker stroke than the default value of 5.
- Change the stroke color: To adjust the color of the stroke, modify the value of strokeColor. You can set the value to a hex code or a named color. For example, setting strokeColor to "#ff0000" will create a red stroke.
- Modify the padding: To adjust the amount of padding around the textbox, modify the value of padding. This parameter determines the amount of space between the text and the edge of the textbox. For example, setting padding to 10 will increase the amount of space around the text.
- Adjust the rounding of the corners: To change the degree of rounding on the corners of the textbox, modify the values of rx and ry. These parameters determine the amount of rounding on the x and y axes, respectively. For example, setting rx and ry to 10 will create a textbox with more rounded corners.
Additionally, you can create a new subclass that extends the Stroked Textbox class and adds new functionality. This allows you to customize the class even further and add new features to the textbox. For example, you could create a subclass that adds a drop shadow to the textbox or one that changes the font based on the user's input. The possibilities are endless, and the Stroked Textbox class provides a great foundation to build upon.
Why Static Site Generation Beats SPAs for SEO: Our 90% Performance Boost
Discover how Static Site Generation (SSG) outperforms Single Page Applications (SPAs) for SEO. Learn why we achieved 562 new indexed pages in 10 days and improved Core Web Vitals with SSG over React, Vue, and Angular SPAs.
HOW TO Effortlessly Publish Your JavaScript Project as an NPM Module
Effortlessly Publish Your JavaScript Project as an NPM Module with our step-by-step guide. Discover how to share your work and boost visibility today!
A Comprehensive Guide to Working with Objects and Shapes in Fabric.js
This guide explores the extensive capabilities of Fabric.js in creating and manipulating objects and shapes on the web. Learn how to bring your web graphics to life.
A Comprehensive Step-by-Step Guide: Customizing Rotate Icons in Fabric.js for Programmers
This guide provides a comprehensive overview of how to customize the rotate icon in Fabric.js, enhancing your canvas projects with unique designs.
Achieving Pixel-Perfect Alignment: Exploring Object Snapping with SnappyRect in Fabric.js
This article explores the SnappyRect class in Fabric.js, a custom solution for achieving pixel-perfect object alignment through object snapping.
Angular vs React: Which Framework Wins for Scalable Enterprise Apps?
This blog post compares Angular and React, analyzing their strengths and weaknesses for building scalable enterprise applications, helping you choose the right framework for your project.
Best NodeJS Architecture for Scalable Cloud Apps: Monolith vs Microservices vs Serverless (Shorterloop’s Guide)
Discover Shorterloop's insights on the best NodeJS architectures for building scalable cloud applications. We compare monolithic, microservices, and serverless approaches, providing real examples to help you decide.
Boosting Performance in Fabric.js 5: Essential Optimization Techniques and Tips
Discover key optimization techniques to enhance the performance of Fabric.js. This guide provides practical tips for creating responsive applications.
Create Dynamic Sections with Drag-and-Drop Functionality and Customizable Textboxes
Explore how to use Fabric.js 5 to create dynamic sections with draggable textboxes, enhancing your web application's interactivity.
Create Professional-Looking Textboxes in Fabric.js with Strokes and Padding
This blog post covers how to create professional-looking stroked textboxes in Fabric.js, including customization options for padding and rounded corners.
Creating Interactive Diagrams with Fabric.js: A Guide to Ports and Connector Lines
In this guide, we explore creating interactive diagrams using Fabric.js by adding ports and connector lines, enhancing user interaction and visual clarity.
Express.js vs Hono.js: The Ultimate Framework Showdown for Node.js Developers
This blog post compares Express.js and Hono.js, examining their features, performance, and suitability for modern web development. Make an informed choice for your next project.
Fabric.js 5 Mastery: Effortlessly Deleting Selected Objects Programmatically
This blog post explores how to programmatically delete selected objects in Fabric.js 5, focusing on removing polygons and circles. Discover the remove method and practical implementations.
Finding the Middle Coordinates of the Vertices of a Polygon in Fabric.js
This blog post explains how to find the middle coordinates of a polygon's vertices in Fabric.js, simplifying object manipulation for developers. It offers a step-by-step guide and code examples for better understanding.
Optimizing Angular Performance: Change Detection Strategy OnPush vs Default
This blog post explores Angular's change detection strategies, comparing OnPush and Default to help developers optimize performance effectively.
Sequelize Associations: How a Misplaced belongsTo Broke Our Query (And How to Fix It)
This blog post explores a common mistake in Sequelize associations involving belongsTo and belongsToMany, detailing how this led to unexpected SQL query errors and their solutions.
Simplify Object Manipulation: Grouping and Ungrouping with Fabric.js
Learn how to simplify object manipulation in Fabric.js by grouping and ungrouping objects. This guide provides syntax and examples for effective canvas management.
Simplifying Code Review: Tips for Improving Readability
This article provides effective strategies for simplifying the code review process while improving code readability and quality in software development.
Step-by-Step Guide to Deploy NodeJS app with PM2 on AWS EC2 instance
This comprehensive guide walks you through the process of deploying a Node.js application on an AWS EC2 instance using PM2. Explore the crucial steps and benefits.
Step-by-Step Guide to Getting Started with Fabric.js in Angular 13
This post is a comprehensive guide to getting started with Fabric.js in your Angular 13 project, covering installation, examples, and editing techniques.
Stop Wasting API Calls: How to Cancel Pending Requests Like a Pro
Discover how to manage and cancel pending API requests effectively using AbortController to enhance performance and user experience.
Streamlining Your Node.js Projects: How GitHub Actions Can Revolutionize Your Workflow
This blog post explores the benefits of migrating from AWS Pipeline to GitHub Actions for Node.js projects, highlighting improved workflow management and automation capabilities.
The Power of Feature Flags: A Guide to Unlocking the Potential of Your Software
This guide explores the power of feature flags in software development, highlighting their benefits and practical implementation strategies to enhance user experience.
Unleashing the Power of Fabric.js Canvas Events: Elevating Interactivity and User Experience
This article explores the functionalities of Fabric.js canvas events, showcasing methods to amplify web interactivity and improve user experiences.
What is Test Driven Development (TDD)? Definition, Benefits, Example, and more!
Learn about Test Driven Development (TDD), a pivotal software development method that enhances code quality through the Red/Green/Refactor cycle, alongside its benefits and limitations.