How to set the position of a Triangle from left using FabricJS?


<p>In this tutorial, we are going to learn how to set the position of a Triangle from the left using FabricJS. Triangle is one of the various shapes provided by FabricJS. In order to create a triangle, we will have to create an instance of <em>fabric.Triangle</em> class and add it to the canvas.</p><p>We can manipulate a triangle object by changing its position, opacity, stroke and also its dimension. The position from left can be changed by using the <em>left</em> property.</p><h2>Syntax</h2><pre class="just-code notranslate language-javascript" data-lang="javascript">new fabric.Triangle( { left: Number }: Object)</pre><h2>Parameters</h2><ul class="list"><li><p><strong>Options</strong><strong> (optional)</strong> − This parameter is an <em>Object</em> which provides additional customizations to our triangle. Using this parameter, properties such as colour, cursor, stroke width, and a lot of other properties can be changed related to the object of which <em>left</em> is a property.</p></li></ul><h2>Options Keys</h2><ul class="list"><li><p><strong>left</strong> − This property accepts a <strong>Number</strong> value which sets the left position of an object. The value determines how far from left edge of canvas, the object will be placed.</p></li></ul><h2>Example 1</h2><p><strong>Default placement of the triangle object</strong></p><p>Let's see a code example to understand the default placement of a triangle object in the canvas when its position has not been changed.</p><pre class="demo-code notranslate language-javascript" data-lang="javascript"><!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>Default placement of the triangle object</h2>    <p>You can see the default placement of the triangle</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({          top: 50,          fill: "#b0e0e6",          height: 90,          width: 100,       });       // Add it to the canvas       canvas.add(triangle);    </script> </body> </html></pre><h2>Example 2</h2><p><strong>Passing left property as key</strong></p><p>In this example, we are assigning the <em>left</em> property with a custom value. Since it accepts a <strong>Number</strong>, you must assign it a numerical value which will represent its position from the left.</p><pre class="demo-code notranslate language-javascript" data-lang="javascript"><!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 left property as key</h2>    <p>You can see that the triangle is placed at a distance of 80px from the left.</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: 80,          top: 50,          fill: "#b0e0e6",          height: 90,          width: 100,       });       // Add it to the canvas       canvas.add(triangle);    </script> </body> </html></pre>

Updated on: 27-Jun-2022

104 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements