如何使用 FabricJS 垂直翻转圆?

WBOY
WBOY 转载
2023-08-24 12:29:02 692浏览

如何使用 FabricJS 垂直翻转圆?

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

语法

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

参数

  • 选项(可选) - 此参数是一个对象 为我们的圈子提供额外的定制。使用此参数,可以更改与 flipY 为属性的对象相关的颜色、光标、描边宽度和许多其他属性等属性。

  • ul>

    选项键

    • 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>

以上就是如何使用 FabricJS 垂直翻转圆?的详细内容,更多请关注php中文网其它相关文章!

声明:本文转载于:tutorialspoint,如有侵犯,请联系admin@php.cn删除