如何使用 FabricJS 水平翻转椭圆?


在本教程中,我们将学习如何使用 FabricJS **水平** 翻转椭圆对象。椭圆是 FabricJS 提供的各种形状之一。为了创建椭圆,我们必须创建fabric.Ellipse类的实例并将其添加到画布中。我们可以使用flipX属性水平翻转椭圆对象。

语法

new fabric.Ellipse({ flipX: Boolean }: Object)

参数

  • options (可选) − 此参数是一个对象,它为我们的椭圆提供额外的自定义。使用此参数,可以更改与对象相关的许多属性,其中flipX是一个属性,例如颜色、光标、描边宽度等。

选项键

  • flipX − 此属性接受布尔值。它允许我们水平翻转对象。

示例 1

flipX作为键传递,值为“false”

以下示例向我们展示了 FabricJS 中椭圆对象的默认方向。由于我们将flipX属性的值设置为“false”,因此椭圆对象不会水平翻转。

<!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 flip an Ellipse horizontally using FabricJS?</h2>
      <p>Select the object and flip it horizontally.</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: "green",
            rx: 100,
            ry: 60,
            stroke: "#228b22",
            strokeWidth: 2,
            flipX: false,
         });

         // Create gradient fill
         ellipse.set("fill", new fabric.Gradient({
            type: "linear",
            coords: {
               x1: 0,
               y1: 0,
               x2: 50,
               y2: 0
            },
            colorStops: [{
               offset: 0,
               color: "red"
            }, {
                  offset: 1,
                  color: "green"
            }, ],
         }));

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

示例 2

flipX属性作为键传递,值为“true”

在这个例子中,我们有一个水平半径为 100,垂直半径为 60,填充为水平线性渐变的椭圆对象。当我们将flipX属性应用于椭圆对象时,它会水平翻转,因此我们看到渐变也翻转了。

<!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 flip an Ellipse horizontally using FabricJS?</h2>
      <p>Select the object and try to flip it horizontally. Here we have set the <b>flipX</b> property as True. </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: "green",
            rx: 100,
            ry: 60,
            stroke: "#228b22",
            strokeWidth: 2,
            flipX: true,
         });

         // Create gradient fill
         ellipse.set("fill", new fabric.Gradient({
            type: "linear",
            coords: {
               x1: 0,
               y1: 0,
               x2: 50,
               y2: 0
            },
            colorStops: [{
               offset: 0,
               color: "red"
            }, {
                  offset: 1,
                  color: "green"
            }, ],
         }));

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

更新于:2022年5月24日

浏览量:125

开启你的职业生涯

完成课程获得认证

开始学习
广告
© . All rights reserved.