如何使用 FabricJS 垂直翻转圆形?


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

语法

new fabric.Circle({ flipY: Boolean }: Object)

参数

  • options(可选) - 此参数是一个 对象,它为我们的圆形提供额外的自定义。使用此参数,可以更改与对象的 flipY 属性相关的颜色、光标、笔划宽度和许多其他属性。

选项键

  • flipY - 此属性接受布尔值。它允许我们垂直翻转对象。

示例 1

flipY 作为键并传递 'false' 值

让我们看一个示例,它向我们展示了 FabricJS 中圆形对象的默认方向。由于我们将 flipY 属性传递了一个 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 a Circle vertically using FabricJS?</h2>
      <p>This is the default orientation of the object. We have set <b>flipY</b> as False, but even if we don't use it, <b>flipY</b> will be by default set to False. </p>
      <canvas id="canvas"></canvas>

      <script>
         // Initiate a canvas instance
         var canvas = new fabric.Canvas("canvas");
         var circle = new fabric.Circle({
            left: 215,
            top: 50,
            fill: "green",
            radius: 80,
            stroke: "#228b22",
            strokeWidth: 2,
            flipY: false
         });

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

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

示例 2

将 flipY 属性作为键并传递 'true' 值

在此示例中,我们有一个半径为 80px 且具有垂直线性渐变填充的圆形对象。当我们将 flipY 属性应用于圆形对象时,它会垂直翻转,因此我们看到渐变也翻转了。

<!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 a Circle vertically using FabricJS?</h2>
      <p>Here observe that the circle has flipped vertically (see the gradient), as we have set <b>flipY</b> to True.</p>
      <canvas id="canvas"></canvas>

      <script>
         // Initiate a canvas instance
         var canvas = new fabric.Canvas("canvas");
         var circle = new fabric.Circle({
            left: 215,
            top: 50,
            fill: "green",
            radius: 80,
            stroke: "#228b22",
            strokeWidth: 2,
            flipY: true
         });

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

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

更新于: 2022年5月25日

174 次查看

开启你的 职业生涯

通过完成课程获得认证

立即开始
广告

© . All rights reserved.