Fabric.js | Image angle Property

In this article, we are going to see how to set the angle of a canvas image using FabricJS. The canvas means the image is movable and can be stretched according to requirement. Further, the image can be customized when it comes to angle, stroke width, padding, etc.

To make this possible we are going to use a JavaScript library called FabricJS. After importing the library, we will create a canvas block in the body tag which will contain our image. Further, we will create an img element which contains the image to be added inside the canvas and set the style attribute to display: none; because we don’t want the image to be visible outside the canvas. After this, we will initialize instances of Canvas and Image provided by FabricJS and render the Image on the Canvas and set the angle of the canvas image using angle property as given in the example below.

Syntax:

fabric.Image({
    image,
    angle: number
}); 

Parameters: This function accepts two parameters as mentioned above and described below:

  • image: It specifies the image object.
  • angle: It specifies the angle of the image in degrees.

Example: This example uses FabricJS to set the angle of the canvas image.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title
        Fabric.js | Image angle Property
    </title>
  
    <!-- FabricJS CDN -->
    <script src=
    </script>
</head>
  
<body>
    <div style="text-align: center;width: 600px;">
        <h1 style="color: green;">
            GeeksforGeeks
        </h1>
        <b>
            Fabric.js | Image angle Property
        </b>
    </div>
  
    <div style="text-align: center;">
        <canvas id="canvas" width="600" height="400" 
                style="border:1px solid #000000;">
        </canvas>
    </div>
  
    <!-- Add the image to be used in the canvas and hide it here 
         because only need it inside the canvas -->
        id="my-image" alt="">
          
    <script>
        // Initiate a Canvas instance
        var canvas = new fabric.Canvas("canvas");
  
        // Get the image element
        var image = document.getElementById('my-image');
  
        // Initiate a Fabric instance
        var fabricImage = new fabric.Image(image, {
            angle: 30
        });
  
        // Add the image to canvas
        canvas.add(fabricImage);
    </script>
</body>
  
</html>                   

chevron_right


Output:

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.