How to set the color of controlling corners of Ellipse using FabricJS?


In this tutorial, we are going to set the color of controlling corners of Ellipse using FabricJS. The cornerColor property allows us to manipulate the color of the controlling corners when the object is active.

Syntax

new fabric.Ellipse({ cornerColor: String }: Object)

Parameters

  • options (optional) − This parameter is an Object which provides additional customizations to our ellipse. Using this parameter color, cursor, stroke width and a lot of other properties can be changed related to the object of which the cornerColor is a property.

Options Keys

  • cornerColor − This property accepts a String which allows us to assign a color to the controlling corners when the object is actively selected.

Example 1

Passing cornerColor as key with a color name as value

The following example demonstrates how to change the color by using the cornerColor property. In this case, we have assigned the value "black" to the key which thus makes the controlling corners appear black.

<!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>How to set the color of controlling corners of Ellipse using FabricJS?</h2>
      <p>Select the object and observe the color of the controlling corners. Here we have used the <b>cornerColor</b> property to set the controlling corners black. </p>
      <canvas id="canvas"></canvas>

      <script>
         // Initiate a canvas instance
         var canvas = new fabric.Canvas("canvas");

         // Initiate an ellipse instance
         var ellipse = new fabric.Ellipse({
            left: 215,
            top: 100,
            fill: "white",
            rx: 90,
            ry: 50,
            stroke: "#c154c1",
            strokeWidth: 5,
            borderColor: "#daa520",
            cornerColor: "black",
         });

         // Adding it to the canvas
         canvas.add(ellipse);
         canvas.setWidth(document.body.scrollWidth);
         canvas.setHeight(250);
      </script>
   </body>
</html>

Example 2

Assigning an RGBA value to the cornerColor property

Instead of passing a simple color name as the String value to the key, we can also assign an RGBA value. RGBA stands for red, green, blue and alpha where alpha is the opacity. Let us see a code example of how we can do that −

<!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>How to set the color of controlling corners of Ellipse using FabricJS?</h2>
      <p>Select the object and observe the color of the controlling corners. Here we have used the <b>cornerColor</b> property and passed an RGB value. </p>
      <canvas id="canvas"></canvas>

      <script>
         // Initiate a canvas instance
         var canvas = new fabric.Canvas("canvas");

         // Initiate an ellipse instance
         var ellipse = new fabric.Ellipse({
            left: 215,
            top: 100,
            fill: "white",
            rx: 90,
            ry: 50,
            stroke: "#c154c1",
            strokeWidth: 5,
            borderColor: "#daa520",
            cornerColor: "rgb(255,20,147)",
         });

         // Adding it to the canvas
         canvas.add(ellipse);
         canvas.setWidth(document.body.scrollWidth);
         canvas.setHeight(250);
      </script>
   </body>
</html>

Updated on: 25-May-2022

228 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements