Home > Web Front-end > JS Tutorial > body text

How to set the size of a rectangle's control corners using FabricJS?

WBOY
Release: 2023-09-21 11:17:02
forward
1127 people have browsed it

How to set the size of a rectangles control corners using FabricJS?

In this tutorial we will learn how to set the size of the control corners Rectangle using FabricJS. The control angle of the object allows us to scale and stretch or change its location.

We can customize the control corners in many ways, such as adding specific Color it, change its size and more. We can use cornerSize to change the size Attributes.

Syntax

new fabric.Rect({ cornerSize: Number }: Object)
Copy after login

Parameters

  • Options (optional) - This parameter is an object which is our rectangle Provides additional customization. Using this parameter, you can change the color, cursor, stroke width and other properties related to the object with cornerSize as the attribute.

Option Key

  • cornerSize - This property accepts a number which allows us to manipulate Controls the size of the selected object's corners. The default value is 13.

Example 1

Default size of control corners

Let us look at a code example that describes the control corners default size When a rectangular object is actively selected.

<!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>Default size of the controlling corners</h2>
   <p>Select the rectangle to see the default size of the controlling corners</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 rectangle object
      var rect = new fabric.Rect({
         left: 125,
         top: 90,
         width: 170,
         height: 70,
         fill: "#cf1020",
         borderColor: "black",
         borderScaleFactor: 3,
         cornerColor: "#3b7a57",
      });

      // Add it to the canvas
      canvas.add(rect);
   </script>
</body>
</html>
Copy after login

Example 2

Passing cornerSize as key with custom value

In this example we will Pass cornerSize property as key with value 17. us You can see how it changes the size of our control corners when the rectangular object chosen.

<!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 cornerSize as key with a custom value</h2>
   <p>Select the rectangle to see the size of the controlling corners</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 rectangle object
      var rect = new fabric.Rect({
         left: 125,
         top: 90,
         width: 170,
         height: 70,
         fill: "#cf1020",
         borderColor: "black",
         borderScaleFactor: 3,
         cornerColor: "#3b7a57",
         cornerSize: 17,
      });

      // Add it to the canvas
      canvas.add(rect);
   </script>
</body>
</html>
Copy after login

The above is the detailed content of How to set the size of a rectangle's control corners using FabricJS?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!