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

How to flip a triangle horizontally using FabricJS?

WBOY
Release: 2023-09-14 18:09:06
forward
1437 people have browsed it

如何使用 FabricJS 水平翻转三角形?

In this tutorial, we will learn how to flip a Triangle object horizontally using FabricJS. Triangle is one of the various shapes provided by FabricJS. In order to create a triangle, we must create an instance of the Fabric.Triangle class and add it to the canvas. We can flip the triangle object horizontally using the flipX property.

Syntax

new fabric.Triangle({ flipX: Boolean }: Object)
Copy after login

Parameters

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

  • < /ul>

    Option Key

    • flipX − This property accepts Boolean values Allows us to flip the object's value horizontally.

    Example 1

    Pass flipX as a key with a value of "false"

    Let Let's look at a code example that shows us the default orientation of triangle objects in FabricJS. Since we passed a False value to the flipX property, the triangle object does not flip horizontally.

    <!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 flipX as key with a &#39;False&#39; value</h2>
       <p>You can see that the triangle object has not been flipped horizontally</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 triangle object
          var triangle = new fabric.Triangle({
             left: 75,
             top: 45,
             width: 180,
             height: 109,
             stroke: "#e3f988",
             strokeWidth: 5,
             flipX: false,
          });
    
          // Create gradient fill
          triangle.set(
             "fill",
             new fabric.Gradient({
                type: "linear",
                coords: { x1: 0, y1: 0, x2: 100, y2: 0 },
                colorStops: [
                   { offset: 0, color: "#545a2c" },
                   { offset: 1, color: "#6495ed" },
                ],
             })
          );
          // Add it to the canvas
          canvas.add(triangle);
       </script>
    </body>
    </html>
    Copy after login

    Example 2

    Passing the flipX attribute as a key with a value of "True"

    In this example, We have a triangle object with a width of 180px and a height of 109px, with a horizontal linear gradient fill. When we apply the flipX property to the triangle object, it flips horizontally, so we see the gradient flipped as well.

    <!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 the flipX property as key with a &#39;True&#39; value</h2>
       <p>You can see that the triangle has been flipped horizontally</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 triangle object
          var triangle = new fabric.Triangle({
             left: 75,
             top: 45,
             width: 180,
             height: 109,
             stroke: "#e3f988",
             strokeWidth: 5,
             flipX: true,
          });
          // Create gradient fill
          triangle.set( "fill", new fabric.Gradient({
             type: "linear", coords: { x1: 0, y1: 0, x2: 100, y2: 0 },
             colorStops: [{ offset: 0, color: "#545a2c" },{ offset: 1, color: "#6495ed" },],})
          );
    
          // Add it to the canvas
          canvas.add(triangle);
       </script>
    </body>
    </html>
    Copy after login

    The above is the detailed content of How to flip a triangle horizontally 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!