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


<p>In this tutorial, we are going to set the position of triangle using FabricJS. The <em>top</em> property allows us to manipulate the position of the object. By default, the top position is relative to the object's top.</p><h2>Syntax</h2><pre class="just-code notranslate language-javascript" data-lang="javascript">new fabric.Triangle({ top: 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 the <em>top</em> is a property.</p></li></ul><h2>Options Keys</h2><ul class="list"><li><p><strong>top</strong> − This property accepts a <strong>Number</strong> which allows us to set the distance from top of the canvas for triangle.</p></li></ul><h2>Example 1</h2><p><strong>Default appearance of the Triangle object</strong></p><p>Let's see a code example to understand how our triangle object would appear when the top property is not used.</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 appearance of the Triangle object</h2>    <p>You can see the default appearance of the triangle when the <b>top</b> property is not used.</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: 105,          width: 90,          height: 80,          fill: "#ffc1cc",          stroke: "#fbaed2",          strokeWidth: 5,       });       // Add it to the canvas       canvas.add(triangle);    </script> </body> </html></pre><h2>Example 2</h2><p><strong>Passing the <em>top</em> property as key with a custom value</strong></p><p>In this example, we are passing the <em>top</em> property as key, with a value of 70. This means that our triangle object will be placed at a distance of 70px from the top.</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 the top property as key with a custom value</h2>    <p>You can see the triangle object is placed at a distance of 70 px from the top</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: 105,          top: 70,          width: 90,          height: 80,          fill: "#ffc1cc",          stroke: "#fbaed2",          strokeWidth: 5,       });       // Add it to the canvas       canvas.add(triangle);    </script> </body> </html></pre>

Updated on: 27-Jun-2022

90 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements