Home >Web Front-end >JS Tutorial >How to set the horizontal scale factor of a circle using FabricJS?

How to set the horizontal scale factor of a circle using FabricJS?

王林
王林forward
2023-08-24 09:41:121274browse

如何使用 FabricJS 设置圆的水平比例因子?

In this tutorial, we will learn how to set the horizontal scale factor of a circle using FabricJS. Circles are one of the various shapes provided by FabricJS. In order to create a circle, we must create an instance of the Fabric.Circle class and add it to the canvas. Just as we can specify the position, color, opacity, and size of a circular object within the canvas, we can also set the horizontal scale of the circular object. This can be done using the scaleX property.

Syntax

new fabric.Circle({ scaleX : Number }: Object)

Parameters

  • Options (optional) - This parameter is a Object Provides additional customization for our circles. Using this parameter, you can change properties such as color, cursor, stroke width, and many other properties associated with the object for which scaleX is an attribute.

  • ul>

    Option Key

    • scaleX - This property accepts numbers value. The assigned value determines the horizontal object scale factor. Its default value is 1.

    Example 1

    Default appearance when scaleX is not used

    Let’s take a look at Code that displays the appearance of a circular object when the scaleX property is not used. By default, circular objects have a horizontal scale factor of 1. scaleX determines the transformation that resizes the object along the X-axis.

    <!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>Setting the horizontal scale factor of circle using FabricJS</h2>
          <p>This is the default horizontal scale factor. We have not used the <b>scaleX</b> property, but by default, it is set to 1. </p>
          <canvas id="canvas"></canvas>
    
          <script>
             // Initiate a canvas instance
             var canvas = new fabric.Canvas("canvas");
             var circle = new fabric.Circle({
                left: 115,
                top: 50,
                padding: 7,
                radius: 50,
                fill: "#85bb65"
             });
             canvas.add(circle);
             canvas.setWidth(document.body.scrollWidth);
             canvas.setHeight(250);
          </script>
       </body>
    </html>

    Example 2

    Passing the scaleX attribute as key

    In this example, we pass scaleX Attribute as key, value is 2. This means that the scale factor of a circular object is doubled in the horizontal direction.

    <!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>Setting the horizontal scale factor of circle using FabricJS</h2>
          <p>Notice the horizontal scale factor of the object. Here we have set <b>scaleX</b> at 2, so the object appears stretched in the horizontal direction. </p>
          <canvas id="canvas"></canvas>
       
          <script>
             // Initiate a canvas instance
             var canvas = new fabric.Canvas("canvas");
             var circle = new fabric.Circle({
                left: 115,
                top: 50,
                padding: 7,
                radius: 50,
                scaleX: 2,
                fill: "#85bb65"
             });
             canvas.add(circle);
             canvas.setWidth(document.body.scrollWidth);
             canvas.setHeight(250);
          </script>
       </body>
    </html>

The above is the detailed content of How to set the horizontal scale factor of a circle using FabricJS?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete