A Comprehensive Guide for Customizing Rotate Icons in Fabric.js

Dinesh Rawat
3 min readJun 5, 2023

Fabric.js is a powerful tool for creating interactive and visually engaging graphical applications that utilize HTML5 canvas.

Customizing Rotate Icons in Fabric.js

One of the highly customizable features of Fabric.js is the ability to modify the default rotate icon. In this comprehensive guide, we will walk you through the process of customizing the rotate icon in Fabric.js, allowing you to add a unique touch to your canvas-based projects.

Step 1: Preparing the Custom Icon

Begin by preparing the custom icon you want to use as the new rotate icon. For instance, let’s assume you have an SVG icon named “custom_rotate_icon.svg.”

To convert this icon file into a Base64 encoded string, you can utilize the FileReader API in JavaScript. Here’s an example code snippet to achieve this:

// Read the SVG icon file
const iconFile = "custom_rotate_icon.svg";

// Convert the file to a Base64 encoded string
const reader = new FileReader();
reader.onload = function (e) {
const base64Icon = btoa(e.target.result);
// Use the base64Icon string as the custom rotate icon
// Proceed to Step 2
};
reader.readAsBinaryString(iconFile);

Step 2: Integrating the Custom Icon

In your Fabric.js code, locate the section where the rotate icon is defined. Usually, there is a constant variable named ROTATE_ICON. Replace the existing Base64 encoded string with the one representing your custom icon. Here’s an example code snippet illustrating this integration:

// Replace the existing rotate icon with the custom icon
const ROTATE_ICON = base64Icon;

Step 3: Rendering the Icon

Next, we need to define a rendering function responsible for drawing the custom rotate icon on the canvas. Within your code, you will find a function named renderIcon(). As a programmer, you can modify this function to suit your specific positioning and styling preferences.

Consider the following example code snippet, where we customize the position and size of the icon:

function renderIcon(ctx, left, top, styleOverride, fabricObject) {
const size = 16;
ctx.save();
ctx.translate(left, top);
ctx.rotate(fabric.util.degreesToRadians(fabricObject.angle));
ctx.drawImage(imgIcon, -size / 2, -size / 2, size, size);
ctx.restore();
}

Step 4: Configuring the Control

Now, let’s create a new control object representing the rotation control and incorporating our custom icon. Within your code, you’ll notice the creation of a control object named mtr using new fabric.Control(). This object allows us to define various properties such as position offsets, action handlers, cursor style handlers, connection options, action names, and the render function responsible for rendering the control icon. Here's an example code snippet demonstrating the configuration of the control:

// Create the custom control object
const mtr = new fabric.Control({
x: -0.6,
y: 0.6,
actionHandler: controlsUtils.rotationWithSnapping,
cursorStyleHandler: controlsUtils.rotationStyleHandler,
withConnection: true,
actionName: 'rotate',
render: renderIcon,
});

Step 5: Assigning the Custom Control

The final step is to assign the newly created control object (mtr) to the mtr property of both fabric.Object.prototype.controls and fabric.Textbox.prototype.controls. This ensures that the custom control is available for all fabric objects and textboxes created within your application. Consider the following code snippet illustrating this assignment:

// Assign the custom control to fabric.Object.prototype.controls
fabric.Object.prototype.controls.mtr = mtr;

// Assign the custom control to fabric.Textbox.prototype.controls
fabric.Textbox.prototype.controls.mtr = mtr;

Conclusion:

By meticulously following these comprehensive steps and incorporating your own custom icon, you can easily customize the rotate icon in Fabric.js to seamlessly blend with your design requirements.

Whether you aim to add a personal touch or align the icon with your project’s branding, the ability to customize the rotate icon provides unparalleled flexibility and enhances the visual appeal of your canvas-based applications. Don’t hesitate to explore other customization options offered by Fabric.js to further tailor the library to your specific needs.

Unleash your creativity and embark on a journey of creating remarkable canvas-based applications with Fabric.js!

--

--

Dinesh Rawat

Seasoned software engineer, Content creator, Helping teams achieve their goals. https://www.linkedin.com/in/dinesh-rawat/