<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Mandev on Medium]]></title>
        <description><![CDATA[Stories by Mandev on Medium]]></description>
        <link>https://medium.com/@aditi.6ti?source=rss-9188150d345a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*APxXXFl6jgTs-_Tgldrdnw.jpeg</url>
            <title>Stories by Mandev on Medium</title>
            <link>https://medium.com/@aditi.6ti?source=rss-9188150d345a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 00:33:17 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@aditi.6ti/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Everything you Need in order to Master FabricJS]]></title>
            <link>https://medium.com/@aditi.6ti/everything-you-need-to-master-fabricjs-68fd25f937ba?source=rss-9188150d345a------2</link>
            <guid isPermaLink="false">https://medium.com/p/68fd25f937ba</guid>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[fabricjs]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Mandev]]></dc:creator>
            <pubDate>Fri, 09 Jun 2023 20:10:21 GMT</pubDate>
            <atom:updated>2023-06-09T20:10:48.625Z</atom:updated>
            <content:encoded><![CDATA[<p>Hello! I’m sure you didn’t just <em>stumble</em> upon this article… your curiosity brought you here, right?</p><p>I have a <strong>soft spot</strong> for witty and curious people in my heart. However, curiosity can also lead to a lot of anxiety <em>especially</em> when you find information that is scattered and not well put together.</p><p>This is why, I decided to bunch up all of my FabricJS articles and provide the link to them, here, in this article.</p><p>In this way, you can find everything you need to know about FabricJS all in one place without the worries or anxiety.</p><p>Hehe… I hope my humble effort of simplifying things helps you all! 😉</p><p>So, here’s the list of the articles I wrote on FabricJS:</p><blockquote>1. <a href="https://medium.com/@aditi.6ti/creating-canvas-using-fabricjs-tutorial-68bf16c40e8e">Creating canvas using FabricJS</a></blockquote><blockquote>2. <a href="https://medium.com/@aditi.6ti/exploring-the-canvas-class-options-of-fabricjs-566c9d8e9e03">Canvas class options in FabricJS</a></blockquote><blockquote>3. <a href="https://medium.com/@aditi.6ti/lower-and-upper-canvas-in-fabricjs-8a62821bfc5c">Lower and Upper Canvas of FabricJS</a></blockquote><blockquote>4. <a href="https://medium.com/@aditi.6ti/drawing-with-fabricjs-part-1-20c73e37d7f2">Basic Shapes provided in FabricJS</a></blockquote><blockquote>5. <a href="https://medium.com/@aditi.6ti/drawing-with-fabricjs-part-2-82cbce125447">Creating a Rectangle Object using FabricJS</a></blockquote><blockquote>6. <a href="https://medium.com/@aditi.6ti/drawing-with-fabricjs-part-3-6d8dec6cff2c">Drawing circle and ellipse using FabricJS</a></blockquote><blockquote>7. <a href="https://medium.com/@aditi.6ti/drawing-with-fabricjs-part-4-889ff2d6c392">Drawing a Line and Polyline using FabricJS</a></blockquote><blockquote>8. <a href="https://medium.com/@aditi.6ti/drawing-with-fabricjs-part-5-85386029a70f">How to add multiple objects onto the Canvas using FabricJS</a></blockquote><blockquote>9. <a href="https://medium.com/@aditi.6ti/how-to-create-gradients-using-fabric-js-f9605789f5df">Drawing Linear Gradients using FabricJS</a></blockquote><blockquote>10. <a href="https://medium.com/@aditi.6ti/how-to-create-radial-gradient-using-fabricjs-3715d57483e5">Drawing Radial Gradients using FabricJS</a></blockquote><blockquote>11. <a href="https://medium.com/@aditi.6ti/add-text-to-the-canvas-using-fabricjs-part-1-2b55dd758c40">How to add text using fabric.Text in FabricJS</a></blockquote><blockquote>12. <a href="https://medium.com/@aditi.6ti/add-text-to-the-canvas-using-fabricjs-part-2-e206644e4339">How to add text using fabric.IText in FabricJS</a></blockquote><blockquote>13. <a href="https://medium.com/@aditi.6ti/add-text-to-the-canvas-using-fabricjs-part-3-c84e70917833">IText class properties of FabricJS</a></blockquote><blockquote>14. <a href="https://medium.com/@aditi.6ti/text-wrapping-in-canvas-using-fabricjs-aac03ed77821">Textbox class of FabricJS</a></blockquote><blockquote>15. <a href="https://medium.com/@aditi.6ti/how-to-add-shadow-to-objects-in-canvas-using-fabricjs-7a7a005f7d0c">Adding a shadow to objects in FabricJS</a></blockquote><blockquote>16. <a href="https://medium.com/@aditi.6ti/how-to-animate-objects-using-fabricjs-7f2491ed0e48">Animating objects in FabricJS</a></blockquote><blockquote>17. <a href="https://medium.com/@aditi.6ti/create-simple-animations-using-fabricjs-b731b9a10fd7">Animating Text in FabricJS</a></blockquote><blockquote>18. <a href="https://medium.com/@aditi.6ti/create-a-grow-shrink-animation-using-fabricjs-d02b2038d8ee">Creating grow-shrink animation using FabricJS</a></blockquote><blockquote>19. <a href="https://medium.com/@aditi.6ti/how-to-create-a-group-of-objects-using-fabricjs-17db8a9d54f0">Grouping Objects using FabricJS</a></blockquote><blockquote>20. <a href="https://medium.com/@aditi.6ti/add-filters-to-images-using-fabricjs-part-1-140f5cb0bed1">Adding Images to the canvas using FabricJS</a></blockquote><blockquote>21. <a href="https://medium.com/@aditi.6ti/add-filters-to-images-using-fabricjs-part-2-7a508fc92808">Adding noise or brightness to Images in FabricJS</a></blockquote><blockquote>22. <a href="https://medium.com/@aditi.6ti/add-filters-to-images-using-fabricjs-part-3-b3ad8337ce31">Adding the hue rotation effect using FabricJS</a></blockquote><blockquote>23. <a href="https://medium.com/@aditi.6ti/add-filters-to-images-using-fabricjs-part-4-db00a1528a13">Edge Detection using FabricJS</a></blockquote><blockquote>24. <a href="https://medium.com/@aditi.6ti/draw-a-b%C3%A9zier-curve-using-fabricjs-8cdc06d0f63a">Drawing a Bezier curve using FabricJS</a></blockquote><blockquote>25. <a href="https://medium.com/@aditi.6ti/canvas-fabricjs-and-its-interactivity-5e5fb9addda1">The selectable property of FabricJS</a></blockquote><blockquote>26. <a href="https://medium.com/@aditi.6ti/cloning-objects-in-the-canvas-using-fabricjs-697f7c2dfe32">Cloning objects in FabricJS</a></blockquote><blockquote>27. <a href="https://medium.com/@aditi.6ti/the-event-system-of-fabricjs-fadbb7c7014b">The Event System of FabricJS</a></blockquote><blockquote>28. <a href="https://medium.com/@aditi.6ti/how-to-attach-event-listeners-directly-to-canvas-objects-10ed29fbc7f8">Adding event listeners in FabricJS</a></blockquote><blockquote>29. <a href="https://medium.com/@aditi.6ti/free-drawing-on-the-canvas-using-fabricjs-b373cf090863">Free Drawing on the canvas using FabricJS</a></blockquote><blockquote>30. <a href="https://medium.com/@aditi.6ti/de-serialize-a-string-representing-svg-document-to-canvas-using-fabricjs-9ccac869bab6">De-serialize SVG to Canvas using FabricJS</a></blockquote><p>Thank You for stopping by. Keep learning! 💕</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=68fd25f937ba" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[De-serialize SVG to canvas using FabricJS]]></title>
            <link>https://medium.com/@aditi.6ti/de-serialize-a-string-representing-svg-document-to-canvas-using-fabricjs-9ccac869bab6?source=rss-9188150d345a------2</link>
            <guid isPermaLink="false">https://medium.com/p/9ccac869bab6</guid>
            <category><![CDATA[html5]]></category>
            <category><![CDATA[fabricjs]]></category>
            <category><![CDATA[javascript-development]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[html5-canvas]]></category>
            <dc:creator><![CDATA[Mandev]]></dc:creator>
            <pubDate>Thu, 08 Dec 2022 19:42:52 GMT</pubDate>
            <atom:updated>2022-12-09T08:34:10.660Z</atom:updated>
            <content:encoded><![CDATA[<p>There are <em>three </em>important words in the above<strong> title</strong>, let’s break them down one by one:</p><ol><li><strong>Deserialize</strong> — When we extract a data structure from a series of bytes, the process is called deserialization. In other words, a <strong>lower level format </strong>is transformed into a <strong>readable object</strong>.</li><li><strong>SVG</strong> — It is a <em>language</em> based on <strong>XML</strong> that describes <strong>2D vectors</strong> on the web. It stands for <strong>Scalable Vector Graphics </strong>and is used to create interactive documents, dynamic changes and some really cool animations as well.</li><li><strong>Canvas</strong> — The HTML5 canvas allows us to draw, add, animate, scale or manipulate objects.</li></ol><blockquote>Read: <a href="https://en.wikipedia.org/wiki/Serialization">Serialization</a></blockquote><blockquote>Suggestion: <a href="https://medium.com/@aditi.6ti/creating-canvas-using-fabricjs-tutorial-68bf16c40e8e">How to create a Canvas using FabricJS</a></blockquote><h4><strong>Target: </strong>Loading an SVG file into the Canvas using FabricJS</h4><p>We can load an SVG file into the canvas using <em>two</em> methods. They are:-</p><ol><li><strong>fabric.loadSVGFromURL </strong>— We need to pass the <strong>URL of the SVG file</strong> or design that we want to use as the first argument.</li><li><strong>fabric.loadSVGFromString</strong> — Loads the svg file from the string representation.</li></ol><p>Both of these are <em>Static Methods </em>that are called on the <strong>fabric object</strong> rather than on a canvas instance.</p><blockquote>I used<a href="https://lovesvg.com/"> this website </a>to download free svg files</blockquote><h3>How to load SVG from a String</h3><p><strong>Step 1:</strong> Download the SVG cut file and unzip it.</p><p><strong>Step 2:</strong> Copy the<strong> .svg</strong> file into the folder where you have your index.html file.</p><p><strong>Step 3:</strong> As soon as you open the .svg file in VS Code, you will see SVG contents starting with &lt;svg&gt; element container.</p><p><strong>Step 4:</strong> Copy all of that and paste it in your index.html file like so:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*39nD-QvswZ745dS9aAZeZw.png" /><figcaption>Paste the SVG file right below the HTML &lt;canvas&gt; element</figcaption></figure><p><strong>Step 5:</strong> In the <strong>&lt;style&gt; </strong>tag of the svg file, set display: none.</p><p><strong>Step 6:</strong> Fetch the<strong> svg element </strong>using the <a href="https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementsByTagName">getElementByTagName</a> and serialize it into a string by using the<strong> serializeToString(</strong>) method.</p><ul><li>We are basically converting the<strong> svg HTML element</strong> into String format since <strong>fabric.loadSVGFromString</strong> accepts only the String representation.</li></ul><p>Step 7: Create a canvas instance and use the <strong>loadSVGFromString() </strong>method to display the svg file on the canvas. This method accepts a function in the second parameter. Therefore we can change the properties of the svg like scaling, rotating, changing its top or left values etc.</p><ul><li>Here’s the JS file for your better understanding:</li></ul><pre>      var svgElement = document.body.getElementsByTagName(&quot;svg&quot;)[0];<br><br>      // The XMLSerializer interface provides serializeToString() method<br>      var serializer = new XMLSerializer();<br><br>      // Serializing the svg element and return that as a String<br>      var svgString = serializer.serializeToString(svgElement);<br><br>      // Creating the Canvas instance<br>      var canvas = new fabric.Canvas(&quot;canvas&quot;, {<br>        backgroundColor: &quot;#fe5a1d&quot;,<br>        isDrawingMode: false,<br>        freeDrawingCursor: &quot;default&quot;,<br>      });<br><br>      // Loading the SVG using loadSVGFromString() method<br>      var path = fabric.loadSVGFromString(<br>        svgString,<br>        function (objects, options) {<br>          var obj = fabric.util.groupSVGElements(objects, options);<br>          obj.set({ left: 20, top: 20, scaleX: 0.5, scaleY: 0.5 });<br>          obj.setCoords();<br><br>          // Adding it to the canvas<br>          canvas.add(obj);<br>          canvas.renderAll();<br>        }<br>      );</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/626/1*WkTzdi6USh9IRC1b_5jzQA.png" /><figcaption>Loading an SVG file from its String representation</figcaption></figure><h3>How to Load SVG from URL</h3><p><strong>Step 1:</strong> Create a Folder where you want to store your FabricJS files. I named mine as Fabric-Demo.</p><p><strong>Step 2:</strong> Open the terminal using (Ctrl+~) and enter the following command:</p><pre>npm create vite@latest</pre><p>We are setting up our local server by using<strong> </strong><a href="https://vitejs.dev/guide/"><strong>Vite </strong></a>so that we don’t run into CORS issues while working with svg URL.</p><p><strong>Step 3:</strong> Add<a href="https://www.npmjs.com/package/fabric"> FabricJS </a>by using this command in your terminal:</p><pre>npm i fabric</pre><p><strong>Step 4:</strong> Remove all the boilerplate code from the <strong><em>main.js, index.html </em></strong>and <strong><em>style.css</em></strong> files.</p><p><strong>Step 5:</strong> Import the FabricJS library in your <strong><em>main.js</em></strong> file like so:</p><pre>import { fabric } from &quot;fabric&quot;;</pre><p><strong>Step 6: </strong>Create a <strong>script</strong> tag in your index.html file and set its type as “module”. Also, use the<em> src </em>attribute to set the location of main.js.</p><p><strong>Step 7:</strong> Use loadSVGFromURL and pass the URL as the first argument. In the second argument, use a function to set the properties.</p><p>Our main.js file should look like this:</p><pre>import { fabric } from &quot;fabric&quot;;<br><br>// Creating the Canvas instance<br>var canvas = new fabric.Canvas(&quot;canvas&quot;, {<br>  backgroundColor: &quot;#f0fff0&quot;,<br>  isDrawingMode: false,<br>  freeDrawingCursor: &quot;crosshair&quot;,<br>});<br><br>fabric.loadSVGFromURL(<br>  &quot;assets/Christmas-Time .svg&quot;,<br>  function (objects, options) {<br>    var christmasSVG = fabric.util.groupSVGElements(objects, options);<br>    christmasSVG.set({ left: 40, top: 5, scaleX: 0.7, scaleY: 0.7 });<br>    canvas.add(christmasSVG);<br>    canvas.calcOffset();<br>    canvas.renderAll();<br>  }<br>);</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*66mYB9vt63fBtqdtMNm06g.png" /><figcaption>This is how index.html should appear</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/667/1*Mmu1chpFBlnIXQg2vFYo1g.png" /><figcaption>Loading an SVG file into the file using a URL</figcaption></figure><h3>Useless Outro</h3><p>This is the final article of my FabricJS series. I feel so happy for some reason as if I have finally added something onto this world.</p><p>I know these articles aren’t perfect and even if you don’t like it, I am grateful that you gave it a chance and read it.</p><p>2022 will be over in 22 days huh… didn’t see that coming. I feel like the only thing I achieved this year was not giving up on doing laundry.</p><p>But I hope yours was much better than that （*＾-＾*）So, wrap yourself in a cozy blanket, keep your coffee on the sidetable — not near the keyboard pleaseeee ╯°□°）╯︵ ┻━┻ and keep coding!</p><p>See ya until next time!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9ccac869bab6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Free Drawing on the Canvas using FabricJS]]></title>
            <link>https://medium.com/@aditi.6ti/free-drawing-on-the-canvas-using-fabricjs-b373cf090863?source=rss-9188150d345a------2</link>
            <guid isPermaLink="false">https://medium.com/p/b373cf090863</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[frontend]]></category>
            <category><![CDATA[fabricjs]]></category>
            <category><![CDATA[html5-canvas]]></category>
            <dc:creator><![CDATA[Mandev]]></dc:creator>
            <pubDate>Tue, 06 Dec 2022 20:20:18 GMT</pubDate>
            <atom:updated>2022-12-07T15:31:26.108Z</atom:updated>
            <content:encoded><![CDATA[<p>In this article we will learn how we can draw on the canvas and take a look at the different brushes that are provided in FabricJS.</p><p>The canvas is a 2D <a href="https://en.wikipedia.org/wiki/Bitmap">bitmap</a>. This means performing free drawing on the canvas is not a big deal at all. Let’s take a look at the canvas properties that come in handy while drawing:</p><ol><li><strong>isDrawingMode</strong> — This property accepts a <strong>Boolean</strong> value which when true, results in free drawing. If it is set to false, we wouldn’t be able to draw on the canvas.</li><li><strong>freeDrawingCursor </strong>— This property accepts a <strong>String</strong> value which denotes the cursor value while free drawing. The accepted cursor values are default, crosshair, pointer, help, all-scroll, not-allowed, col-resize, row-resize etc.</li></ol><h3>How to customize free drawing on the Canvas</h3><p>FabricJS provides us <strong>freeDrawingBrush</strong> for this reason(which is nowhere to be found in the documentation as of 2022 and that is hella weird 🤷‍♀️). We use the dot notation to access the properties of the freeDrawingBrush instance. These properties are<em> color </em>and<em> width</em>. The <em>color </em>property allows us to set the color of the brush while the <em>width</em> property represents the thickness of the brush in pixels.</p><pre>  var canvas = new fabric.Canvas(&quot;canvas&quot;, {<br>        backgroundColor: &quot;black&quot;,<br>        isDrawingMode: true,<br>        freeDrawingCursor: &quot;default&quot;,<br>      });<br><br>      canvas.freeDrawingBrush.color = &quot;#a6e7ff&quot;;<br>      canvas.freeDrawingBrush.width = &quot;5&quot;;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/638/1*_dIRfP1dpgDXkOzlTe5rqQ.png" /><figcaption>Free Drawing Output</figcaption></figure><h3>How to Erase Objects in FabricJS</h3><p>Now that we have learnt how to draw objects on the canvas, let’s see how we can erase them! Drawing and erasing comes hand in hand. So, FabricJS provides us the erasable property which can be set to true or false. It is an instance of <strong>fabric.Eraser</strong> and allows us to erase things on the canvas.</p><pre>   var canvas = new fabric.Canvas(&quot;canvas&quot;, {<br>        backgroundColor: &quot;black&quot;,<br>        isDrawingMode: true,<br>        freeDrawingCursor: &quot;default&quot;,<br>      });<br><br>      var circle = new fabric.Circle({<br>        fill: &quot;red&quot;,<br>        radius: 80,<br>        top: 55,<br>        left: 100,<br>      });<br>      canvas.freeDrawingBrush.width = 20;<br>      canvas.add(circle);<br><br>      circle.set(&quot;erasable&quot;, true);<br>      circle.dirty = true;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/430/1*up8olmPjxJ_y5GyPtBelmw.gif" /><figcaption>Erasing a circle object from the Canvas</figcaption></figure><blockquote>Read:<a href="https://medium.com/@aditi.6ti/drawing-with-fabricjs-part-3-6d8dec6cff2c"> How to create a circle on the Canvas using FabricJS</a></blockquote><h3>How to use the canvas SprayBrush</h3><p>All the brushes provided in FabricJS <em>inherit</em> their properties from the <a href="http://fabricjs.com/docs/fabric.BaseBrush.html">BaseBrush</a> class. The <strong>SprayBrush</strong> class is one of them.</p><p>In order to use a spray brush, we must create an instance of fabric.SprayBrush and pass it the canvas instance. In the code below, I have done just that. We need to create the button first like so:</p><pre>    &lt;button id=&quot;select&quot; style=&quot; padding: 5px; border: 2px solid white; background-color: black;color: whitesmoke;&quot;&gt;Select Drawing&lt;/button&gt;</pre><p>By using the <a href="https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementById">getElementById </a>method, we select the id called “select”. Further the<a href="https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener"> addEventListener</a> method<em> calls</em> a <strong>function</strong> whenever we click on the button. This function sets the drawing mode of our canvas to false.</p><p>Whenever the <strong>drawing mode</strong> of the canvas is set to <strong><em>false</em></strong>, we will be able to play around with the free drawing instances as any other canvas objects. This is because,<em> </em>when<em> </em>we<em> draw </em>on the canvas, we are actually creating <a href="http://fabricjs.com/docs/fabric.Path.html"><strong>fabric.Path</strong></a><strong> </strong>instances. Thus we will be able to<strong><em> modify, move, scale, skew or rotate</em></strong> these objects when<strong> isDrawingMode</strong> is turned off!</p><pre>   var canvas = new fabric.Canvas(&quot;canvas&quot;, {<br>        backgroundColor: &quot;black&quot;,<br>        isDrawingMode: true,<br>        freeDrawingCursor: &quot;default&quot;,<br>      });<br><br>      canvas.freeDrawingBrush = new fabric.SprayBrush(canvas);<br>      canvas.freeDrawingBrush.color = &quot;yellow&quot;;<br>      canvas.freeDrawingBrush.width = 35;<br>      canvas.renderAll();<br><br>      document.getElementById(&quot;select&quot;).addEventListener(&quot;click&quot;, function () {<br>        canvas.isDrawingMode = false;<br>      });</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/444/1*p76ZNwMbfgybS2XW6bTSVg.gif" /><figcaption>Using the <strong>SprayBrush</strong> class</figcaption></figure><h3>How to use the CircleBrush Class</h3><p>Just as its name suggests, the<strong> </strong><a href="http://fabricjs.com/docs/fabric.CircleBrush.html"><strong>CircleBrush</strong></a> class draws circles (DUH!). <em>fabric.CircleBrush </em>returns an instance of <strong>CircleBrush</strong> whose <strong>width</strong> can be controlled by using the <em>width</em> property (the default value is 10).</p><pre>      canvas.freeDrawingBrush = new fabric.CircleBrush(canvas);<br>      canvas.freeDrawingBrush.color = &quot;rgba(255,20,147,0.4)&quot;;<br>      canvas.freeDrawingBrush.width = &quot;2&quot;;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/430/1*SBaYb0ioEUJwXK_lJ5VYdg.gif" /><figcaption>Using the <strong>CircleBrush</strong> class</figcaption></figure><h3>Useless Outro</h3><p>The other brushes can be used exactly how we used the <strong>SprayBrush</strong> and <strong>CircleBrush</strong> classes! I hope this was helpful. See ya~</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b373cf090863" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to attach event listeners directly to canvas objects?]]></title>
            <link>https://medium.com/@aditi.6ti/how-to-attach-event-listeners-directly-to-canvas-objects-10ed29fbc7f8?source=rss-9188150d345a------2</link>
            <guid isPermaLink="false">https://medium.com/p/10ed29fbc7f8</guid>
            <category><![CDATA[html5]]></category>
            <category><![CDATA[html5-canvas]]></category>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[fabricjs]]></category>
            <dc:creator><![CDATA[Mandev]]></dc:creator>
            <pubDate>Sun, 04 Dec 2022 16:26:45 GMT</pubDate>
            <atom:updated>2022-12-04T16:27:53.095Z</atom:updated>
            <content:encoded><![CDATA[<p>In the<a href="https://medium.com/@aditi.6ti/the-event-system-of-fabricjs-fadbb7c7014b"> previous article</a>, we added<strong> mouse events</strong> onto the canvas whereby we learnt how we can tackle <em>events </em>that occur due to <strong>user interaction</strong> with the mouse.</p><p>When we write,<em> canvas.on() </em>and attach the <a href="https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Events">events</a> to the canvas, it means that the particular event is scoped to <strong><em>that</em></strong> canvas instance.</p><p>So, if we had two canvases whose instances were <em>say</em>, <strong>canvas1</strong> and <strong>canvas2</strong>, we could attach different event listeners to them without stumbling upon an error. This is because the<strong><em> instances</em></strong> <strong>respect </strong>only the<em> events </em>that are <strong>assigned to them</strong>.</p><pre>      var canvas1 = new fabric.Canvas(&quot;canvas1&quot;, {<br>        backgroundColor: &quot;black&quot;,<br>        isDrawingMode: false,<br>      });<br><br>      var canvas2 = new fabric.Canvas(&quot;canvas2&quot;, {<br>        backgroundColor: &quot;red&quot;,<br>        isDrawingMode: false,<br>      });<br><br>      canvas1.on(&quot;mouse:down&quot;, () =&gt; {<br>        console.log(&quot;This is canvas1&quot;);<br>      });<br><br>      canvas2.on(&quot;mouse:down&quot;, () =&gt; {<br>        console.log(&quot;This is canvas2&quot;);<br>      });</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*F5AG5FvPqGqxauOH80ZsTg.gif" /><figcaption><strong>canvas1 </strong>has bg color “black” while <strong>canvas2</strong> has bg color “red”.</figcaption></figure><h3>Attach event listeners to objects</h3><h4>The “skewing” event Listener</h4><p>Just how we can attach event listeners to canvas instances by using the on() method, we can do the same to other canvas objects. So, that means that the below code is completely valid:</p><pre><br>      var skewFunction = function () {<br>        canvas.renderAll();<br>        tri.animate(&quot;strokeWidth&quot;, &quot;+=8&quot;, {<br>          onChange: canvas.renderAll.bind(canvas),<br>          easing: fabric.util.ease.easeOutExpo,<br>        });<br>      };<br><br>      tri.on(&quot;skewing&quot;, skewFunction);</pre><p>In the code above, tri is a <a href="http://fabricjs.com/docs/fabric.Triangle.html">triangle instance </a>to which I have added the <strong>“skewing”</strong> event listener. Also, I have created an event handler called the <em>skewFunction</em> which will be called every time we skew the triangle.</p><blockquote>Note: You can skew an object on the canvas by <strong>pressing shift</strong> and dragging along the controls</blockquote><p>The <strong><em>skewFunction</em></strong> inturn, is a function which<a href="https://medium.com/@aditi.6ti/how-to-animate-objects-using-fabricjs-7f2491ed0e48"> <strong>animates</strong></a> the triangle. When we say <em>tri.animate</em> and pass it the arguments<strong> “strokeWidth”</strong> and <strong>“+=8”</strong>, it means that its stroke width will be <em>incremented</em> by a value of 8 every time it is skewed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/404/1*ZmAuxiEg9z13GW9ZLN4Dcw.gif" /><figcaption>Every time we <strong>skew</strong> tri, it’s <strong>strokeWidth</strong> value gets incremented by 8.</figcaption></figure><p>Here’s the complete code:</p><pre>    var canvas = new fabric.Canvas(&quot;canvas&quot;, {<br>        backgroundColor: &quot;black&quot;,<br>        isDrawingMode: false,<br>      });<br><br>      // Initiating a triangle instance<br>      var tri = new fabric.Triangle({<br>        top: 20,<br>        left: 90,<br>        width: 80,<br>        height: 200,<br>        fill: &quot;blue&quot;,<br>        stroke: &quot;white&quot;,<br>        strokeWidth: 1,<br>      });<br><br>      // Adding the tri instance to the canvas<br>      canvas.add(tri);<br><br>      // This is the event handler<br>      var skewFunction = function () {<br>        canvas.renderAll();<br>        tri.animate(&quot;strokeWidth&quot;, &quot;+=8&quot;, {<br>          onChange: canvas.renderAll.bind(canvas),<br>          easing: fabric.util.ease.easeOutExpo,<br>        });<br>      };<br><br>      // when skewing event occurs, call skewFunction<br>      tri.on(&quot;skewing&quot;, skewFunction);<br></pre><h4>Increasing the Radius of a circle while Moving it</h4><p>Instead of creating a handler, we can use the good old<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions"> arrow function</a> like so:</p><pre><br>      // Initiating a circle instance<br>      var circle = new fabric.Circle({<br>        top: 20,<br>        left: 90,<br>        radius: 6,<br>        fill: &quot;#e6e6fa&quot;,<br>        stroke: &quot;#9966cc&quot;,<br>        strokeWidth: 16,<br>      });<br><br>      // Adding the circle instance to the canvas<br>      canvas.add(circle);<br><br>      // Using an arrow function in the second argument<br>      circle.on(&quot;moving&quot;, () =&gt; {<br>        canvas.renderAll();<br>        circle.animate(&quot;radius&quot;, &quot;+=7&quot;, {<br>          onChange: canvas.renderAll.bind(canvas),<br>          easing: fabric.util.ease.easeOutSine,<br>        });<br>      });</pre><p>This is <em>similar</em> to the previous example except, the <strong>radius</strong><em> gets</em> <strong>incremented by 7</strong> whenever <strong><em>we move the circle </em></strong>on the canvas. Since, originally, the radius was 6, on moving it once, its radius will<em> change</em> to 13 and it will <strong>only</strong> be<strong> incrementing on being moved</strong>!</p><p>I hope you get the idea. Other<strong> event listeners</strong> can also be attached to object instances like this. So, those would be, <strong><em>selected, deselected, moving, skewing, rotating, modified</em></strong> etc.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/396/1*sdY97SLj-0XGff1SeM44sA.gif" /><figcaption>You can see the radius increases only when we are moving the circle on the canvas!</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=10ed29fbc7f8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The Event System of FabricJS]]></title>
            <link>https://medium.com/@aditi.6ti/the-event-system-of-fabricjs-fadbb7c7014b?source=rss-9188150d345a------2</link>
            <guid isPermaLink="false">https://medium.com/p/fadbb7c7014b</guid>
            <category><![CDATA[html5-canvas]]></category>
            <category><![CDATA[fabricjs]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[html5]]></category>
            <category><![CDATA[front-end-development]]></category>
            <dc:creator><![CDATA[Mandev]]></dc:creator>
            <pubDate>Sat, 03 Dec 2022 17:02:00 GMT</pubDate>
            <atom:updated>2022-12-03T17:18:08.513Z</atom:updated>
            <content:encoded><![CDATA[<p>FabricJS provides an extensive list of events. Starting from its low-level mouse events, it’s got us fully covered.</p><p>Starting with this article, I will explain how we can work with events using FabricJS. It’s going to be in parts so hang tight and let’s begin!</p><h3>What is an Event in Canvas?</h3><p>An <em>event</em> is an <strong>occurrence</strong> that tells us that something has changed in our system. It depends on us how we react to it. Events can happen anywhere. Similar to how events occur in webpages, <em>Canvas</em> events are no different.</p><p>So, when we <strong>click </strong>on the canvas, it is an <em>event</em>.</p><p>When we <strong>rotate something</strong> on the canvas, it is also <em>an event</em>.</p><p>When we <strong>drag something<em> </em></strong><em>into</em> the canvas, oh yeah you got that, it is also an <strong>E-V-E-N-T</strong>.</p><p>It’s not that complicated, right? ~(￣▽￣)~*</p><h3>Using the on() method</h3><p>Since the <strong>Event API </strong>is similar to <em>jQuery, Underscore.js</em> and other <em>javascript libraries</em>, you might be able to find some similarities.</p><p>However, if you are completely unaware of jQuery or Underscore.js or if you’ve forgotten them, it’s okay we can still make it work!</p><p>The <em>on()</em> method simply attaches the <strong>event </strong>to<strong> </strong>any <strong>selected element</strong>. In this case,<strong> instances</strong>.</p><h4>Adding mouse events onto the canvas</h4><p>As an example, I have used the<strong> mouse:up</strong> and <strong>mouse:down</strong> events and attached them to the <strong>canvas instance</strong> by using the <em>on()</em> method.</p><p>The mouse:up event is fired whenever the mouse button is released. On the contrary, the mouse:down event occurs whenever the mouse button is pressed.</p><p>Here, I have created an instance of fabric.Canvas and by using the <a href="http://fabricjs.com/docs/fabric.Canvas.html#backgroundColor"><strong>backgroundColor property</strong></a> , I have set the background color as black. However, I want its <strong>background color</strong> to change to <strong>white</strong> when a<em> mouse up</em> <em>event occurs</em> and,<strong> red </strong>when a<em> mouse down </em>event occurs. In order to handle those events, I have used the<strong> on()</strong> method to initialize<strong> event listener</strong>. Further the handler, changes the background color and logs an output for us in the console.</p><pre>     var canvas = new fabric.Canvas(&quot;canvas&quot;, {<br>        backgroundColor: &quot;black&quot;,<br>        isDrawingMode: false,<br>      });<br><br>      canvas.on(&quot;mouse:up&quot;, () =&gt; {<br>        console.log(&quot;mouse up event&quot;);<br>        canvas.setBackgroundColor(&quot;white&quot;, canvas.renderAll.bind(canvas));<br>      });<br><br>      canvas.on(&quot;mouse:down&quot;, () =&gt; {<br>        console.log(&quot;mouse down event&quot;);<br>        canvas.setBackgroundColor(&quot;red&quot;, canvas.renderAll.bind(canvas));<br>      });</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/560/1*OwzWo2dk34nO7WpQk_FxKg.png" /><figcaption>Logged output in the console</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/510/1*bK3ryG9nRCTvy9P1jFzVhg.gif" /><figcaption><strong>mouse:down</strong>: set background color to red<strong>; mouse:up</strong>: set background color to white</figcaption></figure><h4>Canvas events that react to User selection</h4><p>Whenever a user interacts with objects in the canvas, user selection events can be activated. The following are the events that are fired by the canvas on user selection:</p><ol><li><strong>selection:created </strong>— When the user advances from <em>no selection</em> to <em>selecting</em> something,<strong> this event</strong> is fired.</li><li><strong>selection:updated </strong>— Multiselection, switching from single to multiselection or <em>moving </em>the<strong> selection</strong> from<strong> object A to B</strong> fires this event.</li><li><strong>before:selection:cleared </strong>—This event is fired <em>just</em> before the selection:cleared event.</li><li><strong>selection:cleared </strong>— This event is fired when a user <em>deselects an object</em> or <em>destroys</em> a multiple selection.</li></ol><p>Here’s a code example to demonstrate that:</p><pre>     var canvas = new fabric.Canvas(&quot;canvas&quot;, {<br>        backgroundColor: &quot;black&quot;,<br>        isDrawingMode: false,<br>      });<br><br>      var rect = new fabric.Rect({<br>        width: 60,<br>        height: 80,<br>        top: 30,<br>        left: 120,<br>        fill: &quot;white&quot;,<br>        stroke: &quot;#ffdead&quot;,<br>      });<br><br>      var square = new fabric.Rect({<br>        width: 90,<br>        height: 90,<br>        top: 80,<br>        angle: 14,<br>        left: 300,<br>        fill: &quot;#43b3ae&quot;,<br>        stroke: &quot;white&quot;,<br>      });<br><br>      canvas.add(rect, square);<br><br>      canvas.on(&quot;selection:created&quot;, (obj) =&gt; {<br>        var selectedObj = obj.target;<br>        console.log(&quot;An object has been selected&quot;);<br>      });<br><br>      canvas.on(&quot;selection:updated&quot;, (obj) =&gt; {<br>        console.log(&quot;You have selected the other object&quot;);<br>      });</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fadbb7c7014b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Cloning objects in the Canvas using FabricJS]]></title>
            <link>https://medium.com/@aditi.6ti/cloning-objects-in-the-canvas-using-fabricjs-697f7c2dfe32?source=rss-9188150d345a------2</link>
            <guid isPermaLink="false">https://medium.com/p/697f7c2dfe32</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[html5]]></category>
            <category><![CDATA[html5-canvas]]></category>
            <category><![CDATA[fabricjs]]></category>
            <dc:creator><![CDATA[Mandev]]></dc:creator>
            <pubDate>Fri, 02 Dec 2022 18:25:37 GMT</pubDate>
            <atom:updated>2022-12-02T18:33:31.613Z</atom:updated>
            <content:encoded><![CDATA[<p>When we say that something has been cloned, it means that it has been duplicated or copied. The<em> </em><a href="http://fabricjs.com/docs/fabric.Ellipse.html#clone"><em>clone()</em></a><em> </em>method allows us to clone any instance. It’s first argument is a callback function while the second argument is the <em>optional</em> properties <strong>Array</strong>.</p><pre> instanceThatWeWantToClone.clone( callback: function, propertiesToInclude: Array)</pre><h3>Using the clone() method</h3><p>In my <a href="https://medium.com/@aditi.6ti/canvas-fabricjs-and-its-interactivity-5e5fb9addda1">previous article</a>, I used the below image as the thumbnail. I used the Text object in order to add the emoji (😩) but I only created<em> one </em><strong>Text</strong> object! <strong>I know</strong> what you’re thinking and Yup, you got that right. I cloned the Text object in order to save myself some time and code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/626/1*3HVmU_iFgNnox4qQtEeuTw.png" /><figcaption><strong>(_</strong><a href="https://medium.com/@aditi.6ti/canvas-fabricjs-and-its-interactivity-5e5fb9addda1"><strong>Link to my previous Article Ahem</strong></a><strong>_)</strong></figcaption></figure><p>But it’s not <em>just</em> about saving time and code. Cloning objects can <em>really</em> come in handy whenever we want to <strong>add </strong>a <em>copy-paste</em> feature or <strong>duplicate </strong>objects so that they can be customized in different ways!</p><p>Let us see how we can clone an object in the canvas by using FabricJS.</p><ol><li>The<em> first step</em> is to create your favorite object. My favorite object doesn’t exist so I’m simply<strong> </strong><a href="https://medium.com/@aditi.6ti/drawing-with-fabricjs-part-3-6d8dec6cff2c"><strong>creating a circle</strong></a><strong> </strong>and a<a href="https://medium.com/@aditi.6ti/add-text-to-the-canvas-using-fabricjs-part-2-e206644e4339"><strong> Text object</strong></a><strong> </strong>cause <em>why not</em>?</li><li>We are using the<em> clone()</em> method to clone the <strong>circle</strong> and <strong>text</strong> <em>instances</em></li><li>You can see that inside the clone() method, lies an <strong>arrow function</strong> which takes<strong> one parameter</strong> called <em>obj</em>.</li></ol><pre>     var canvas = new fabric.Canvas(&quot;canvas&quot;, {<br>        backgroundColor: &quot;black&quot;,<br>        isDrawingMode: false,<br>      });<br><br>      // Circle object<br>      var circle = new fabric.Circle({<br>        radius: 50,<br>        fill: &quot;#00bfff&quot;,<br>        top: 60,<br>        left: 70,<br>      });<br><br>      // cloning the circle object<br>      circle.clone((obj) =&gt; {<br>        obj.set(&quot;top&quot;, 55);<br>        obj.set(&quot;left&quot;, 95);<br>        obj.set(&quot;fill&quot;, &quot;#ff69b4&quot;);<br>        canvas.add(obj);<br>      });<br><br>      // Text object<br>      var tt = new fabric.Text(&quot;🎅&quot;, {<br>        fontSize: 83,<br>        left: 80,<br>        top: 180,<br>      });<br><br>      // cloning the text object<br>      tt.clone((obj) =&gt; {<br>        obj.set(&quot;scaleX&quot;, 0.5);<br>        obj.set(&quot;scaleY&quot;, 0.5);<br>        obj.set(&quot;left&quot;, 185);<br>        canvas.add(obj);<br>      });<br><br>      // Adding them to the canvas<br>      canvas.add(circle, tt);</pre><p>You can simply paste the above code inside the <strong>&lt;script&gt; tag</strong> if you don’t want to go through the trouble of typing this. The output should look like this:</p><blockquote>Read: <a href="https://medium.com/@aditi.6ti/creating-canvas-using-fabricjs-tutorial-68bf16c40e8e">Creating canvas using FabricJS</a></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/597/1*KiQ4eP4x6UDQe4v3bc_jfw.png" /><figcaption>Cloned objects</figcaption></figure><h3>Implementing the copy-paste feature using FabricJS</h3><p>The code is pretty self explanatory and I have added comments explaining each line. But remember, to create two buttons and pass the <strong><em>cpFunc()</em></strong> and <strong><em>pasteFunc()</em></strong> functions to the <strong>onclick event attribute</strong>. Also, if you are wondering, I used <a href="https://getcssscan.com/css-buttons-examples">this website</a> for<strong> styling</strong> the buttons and it got the job done <em>really really</em> fast.</p><h4>Methods used in the example below:</h4><ul><li><strong>getActiveObject()</strong> — returns the current active object</li><li><a href="http://fabricjs.com/docs/fabric.Canvas.html#discardActiveObject"><strong>discardActiveObject()</strong></a><strong> </strong>— discards the current active object</li><li><strong>set()</strong> — setter used for setting the properties</li><li><a href="http://fabricjs.com/docs/fabric.Canvas.html#forEachObject"><strong>forEachObject()</strong></a> — Executes a function for each and every object in that group</li><li><strong>setCoords()</strong> — Sets the corner controls based on the current position coordinates, angle, width, height, left and top.</li></ul><pre>     var canvas = new fabric.Canvas(&quot;canvas&quot;, {<br>        backgroundColor: &quot;black&quot;,<br>        isDrawingMode: false,<br>      });<br><br>      // Initiating Copy function<br>      function cpFunc() {<br>        canvas.getActiveObject().clone(function (Obj) {<br>          copyToClipboard = Obj;<br>        });<br>      }<br><br>      // Initiating Paste function<br>      function pasteFunc() {<br>        copyToClipboard.clone(function (clonedObj) {<br>          canvas.discardActiveObject();<br><br>          // Setting additional properties for the cloned object<br>          clonedObj.set({<br>            left: clonedObj.left + 10,<br>            top: clonedObj.top + 10,<br>            evented: true,<br>          });<br><br>          // If the object is actively selected only then, add it to the canvas<br>          if (clonedObj.type === &quot;activeSelection&quot;) {<br>            clonedObj.canvas = canvas;<br>            clonedObj.forEachObject(function (obj) {<br>              canvas.add(obj);<br>            });<br><br>            // Setting the corner controls of the cloned object<br>            clonedObj.setCoords();<br>          } else {<br>            canvas.add(clonedObj);<br>          }<br><br>          copyToClipboard.top += 10;<br>          copyToClipboard.left += 10;<br><br>          // The cloned object will be the new active object<br>          canvas.setActiveObject(clonedObj);<br><br>          // Appends a renderAll request<br>          canvas.requestRenderAll();<br>        });<br>      }<br><br>      // Circle object<br>      var circle = new fabric.Circle({<br>        radius: 50,<br>        fill: &quot;#ed2939&quot;,<br>        top: 60,<br>        left: 70,<br>        stroke: &quot;white&quot;,<br>        strokeWidth: 3,<br>      });<br><br>      // Text object<br>      var tt = new fabric.Text(&quot;🎅&quot;, {<br>        fontSize: 83,<br>        left: 80,<br>        top: 180,<br>      });<br><br>      // Adding them to the canvas<br>      canvas.add(circle, tt);</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/635/1*JmWwxJXfpumoW0YyZDw2Xg.png" /><figcaption>Implementing Copy-paste using FabricJS</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=697f7c2dfe32" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Canvas, FabricJS and its Interactivity]]></title>
            <link>https://medium.com/@aditi.6ti/canvas-fabricjs-and-its-interactivity-5e5fb9addda1?source=rss-9188150d345a------2</link>
            <guid isPermaLink="false">https://medium.com/p/5e5fb9addda1</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[fabricjs]]></category>
            <category><![CDATA[html5]]></category>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[html5-canvas]]></category>
            <dc:creator><![CDATA[Mandev]]></dc:creator>
            <pubDate>Thu, 01 Dec 2022 20:09:27 GMT</pubDate>
            <atom:updated>2022-12-01T20:28:04.504Z</atom:updated>
            <content:encoded><![CDATA[<p>Yesterday’s article was a bit on the intellectual side, I believe. So I want to keep things really basic for this article. Well nah, I’m just late today and I need to publish one right now 😶 Enough of me making excuses 😑 Let’s get right into it!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/626/1*3HVmU_iFgNnox4qQtEeuTw.png" /></figure><h3>The Manager- fabric.Canvas</h3><p>We know that when we create an instance of fabric.Canvas, it serves as a wrapper around the<strong> HTML &lt;canvas&gt; </strong>element.</p><p>This, gives it the <em>authority </em>to manage and manipulate all of the other objects that are there in that canvas. It acts as the<strong> Manager </strong>or a <em>supervisor</em> of sorts without which we wouldn’t have any control over the individual objects!</p><p>So, we can <em>add, remove or reference</em> objects to the instance of fabric.Canvas.</p><p>However, (<em> and</em> this point is often overlooked! )it also serves as a configuration host.</p><p>When we say that something needs to be configured, it means that it is to be <em>shaped </em>or arranged in such a way that it meets our demands. So, if we want our canvas to<strong> look or behave</strong> in a certain way, we can rely on <a href="https://medium.com/@aditi.6ti/exploring-the-canvas-class-options-of-fabricjs-566c9d8e9e03">fabric.Canvas</a> for that.</p><p>So we can programmatically set <strong>width, zoom or height</strong> of the canvas. We can also, set the background color or image or set an overlay color like I have done below. <strong>All of <em>these</em></strong> properties can be modified <em>at will</em> whether it is at the<em> time of creation </em>of <strong>fabric.Canvas</strong> or <em>after</em>.</p><pre> // Instance of fabric.Rect<br> var rect = new fabric.Rect({<br>        width: 90,<br>        height: 80,<br>        top: 70,<br>        left: 100,<br>        fill: &quot;orange&quot;,<br>      });<br><br>       // Adding the rectangle object to the canvas<br>      canvas.add(rect);<br><br>      // Specifying the overlay color that we want for the canvas<br>      canvas.setOverlayColor(<br>        &quot;rgba(249,66,158, 0.6)&quot;,<br>        canvas.renderAll.bind(canvas)<br>      );</pre><h3>What makes FabricJS special #lessHyperactivityMoreInteractivity</h3><p>When we talked about creating Groups using the FabricJS library, we saw how a collection of objects could be clustered together as if they were one entity.</p><p>On a user level, objects on the canvas can be <strong>manipulated </strong>via touch or mouse movements. We can drag, rotate, skew, scale or select objects. Yes- we can also group them together simply by mouse activity!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/402/1*jK2CWS2BbIDXPzf5EfO3Ow.gif" /><figcaption>Grouping objects by mouse activity</figcaption></figure><p>This is possible because of the top layer of interactivity that is a built-in feature of FabricJS. If we want to turn off the selectivity of objects, we can simply use the <em>selectable</em> property. Whereas if we want to turn off group selection for objects in canvas then we can use the <a href="http://fabricjs.com/docs/fabric.Canvas.html#selection">selection</a> property of Canvas class.</p><p>But, if you want to go the extra mile and get rid of every last bit of selectivity in the canvas then feel free to use <strong>fabric.StaticCanvas</strong> instead.</p><p>The<strong> </strong><a href="http://fabricjs.com/docs/fabric.StaticCanvas.html"><strong>StaticCanvas</strong></a> class is like the little brother of fabric.Canvas. It has the object model of the regular canvas class but only without<strong> event handling</strong> logic. It’s like the <em>lighter</em> <strong>nerdy-school-topper </strong>version of the interactive and fun <strong>fabric.Canvas</strong>. It still gets good grades, still allows us to play around with those objects but the only difference being the interactive layer is gone.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5e5fb9addda1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Draw A Bézier curve using FabricJS]]></title>
            <link>https://medium.com/@aditi.6ti/draw-a-b%C3%A9zier-curve-using-fabricjs-8cdc06d0f63a?source=rss-9188150d345a------2</link>
            <guid isPermaLink="false">https://medium.com/p/8cdc06d0f63a</guid>
            <category><![CDATA[html-canvas]]></category>
            <category><![CDATA[html5]]></category>
            <category><![CDATA[fabricjs]]></category>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Mandev]]></dc:creator>
            <pubDate>Wed, 30 Nov 2022 18:10:22 GMT</pubDate>
            <atom:updated>2022-11-30T18:10:22.886Z</atom:updated>
            <content:encoded><![CDATA[<p>It is time that we move on to drawing more complex shapes with FabricJS. We’ve learnt how we can draw <a href="https://medium.com/@aditi.6ti/drawing-with-fabricjs-part-3-6d8dec6cff2c">circle</a>, ellipse, triangle and many more such <a href="https://medium.com/@aditi.6ti/drawing-with-fabricjs-part-1-20c73e37d7f2">objects</a> by using their <em>respective classes</em> provided by the <strong>FabricJS library</strong>. But when it comes to drawing complex curves, we need to use the <a href="http://fabricjs.com/docs/fabric.Path.html#initialize"><strong>Path</strong> class </a>and pass the coordinates such that they can reflect the shape that we want to draw on the canvas.</p><h4>What are Bézier curves?</h4><p>Patented and popularized by French engineer, <a href="https://en.wikipedia.org/wiki/Pierre_B%C3%A9zier"><strong>Pierre Étienne Bézier</strong></a><strong>, </strong>Bézier curves are widely used in computer graphics to draw smooth curves.</p><p>You might come across vector graphics or animation that uses Bézier curves because they provide computational ease and smooth 🎢 scaling.</p><blockquote>You can click here to read how to <a href="https://javascript.info/bezier-curve">identify Bézier curves</a></blockquote><p>A<strong> Bézier curve</strong> can be defined by its <strong>control points</strong> and starting from 2 it can have several control points. The simplest form of Bézier curve is a straight line.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/370/1*jSCFLgIwzPd-7rvhGUUflQ.png" /><figcaption>(<a href="https://www.gpp7.org.in/wp-content/uploads/sites/22/2020/04/file_5e95c84ed158c.pdf">_Image Source_</a>)</figcaption></figure><p>A Bézier curve having <strong>3 control points</strong>, is called a <strong>quadratic Bézier curve</strong> whereas a Bézier curve having 4 control points is called a cubic Bézier curve. In the image down below, a <strong>cubic Bézier curve </strong>is displayed. Its<strong> control points</strong> are b0, b1, b2 and b3.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/701/1*9e_pl2L3VKFGMSokrBbA2w.png" /><figcaption>(<a href="https://www.gatevidyalay.com/bezier-curve-in-computer-graphics-examples/">_Image Source_</a>)</figcaption></figure><h3>Using Path class to create a Cubic Bézier curve</h3><p>Although it isn’t suggested that we create complex paths using Bézier curves, we mustn’t completely ignore them. So, let’s see how we can create a simple <strong>cubic Bézier curve</strong> using the <strong>Path class </strong>provided in FabricJS.</p><p>This is the Path class constructor:</p><pre>new fabric.Path( path: Array | String, options: Object )</pre><ul><li>You can see in the constructor definition that we can create Path objects by initiating an instance of <strong>fabric.Path</strong>.</li><li>The first argument should be an <strong>Array </strong>or a <strong>String</strong> that specifies the path data. In other words, this is where we need to set the inputs for our curve.</li><li>Lastly, we can make additional changes in property in the optional options <strong>Object </strong>that is the second argument.</li></ul><h4>Creating the cubic Bézier curve</h4><p>In order to create the curve, we need to use the following commands:</p><ul><li>M : Move command; Move to coordinate (x, y)</li><li>C : Cubic bezier command; C (first control point) (second control point) (ending coordinate)</li></ul><pre>C (x1, y1) (x2, y2) (xd, yd)</pre><ul><li>x1, y1 : first control point ie, (150, 50)</li><li>x2 , y2 : second control point ie., (300, 250)</li><li>xd, yd : destination coordinates ie., (350, 100)</li></ul><pre>C 150,50 300,250 350,100</pre><p>Finally, let’s code the entire thing:</p><pre>     // Initiate a path instance<br>      var path = new fabric.Path(&quot;M 50,150 C 150,50 300,250 350,100 &quot;, {<br>        strokeWidth: 6,<br>        stroke: &quot;white&quot;,<br>        left: 10,<br>        top: 100,<br>      });<br><br>       canvas.add(path);</pre><p>On pasting the above piece of code inside the &lt;script&gt; tag, you will get an output like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/616/1*_laywkB3OcK7SlcJkU6kpA.png" /><figcaption>A<strong> Cubic Bézier curve</strong> created using <strong>FabricJS</strong></figcaption></figure><h3>Useless Outro</h3><p>Oh my God guys that was really difficult to explain. Forgive me if this was a silly attempt. While working on this article, I felt like I had dug my own grave lol.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FnjPdRtrrdyoVO%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FnjPdRtrrdyoVO%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2FnjPdRtrrdyoVO%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="289" frameborder="0" scrolling="no"><a href="https://medium.com/media/3e337132a08403bee5e7621d411e5bd3/href">https://medium.com/media/3e337132a08403bee5e7621d411e5bd3/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8cdc06d0f63a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Add Filters to Images using FabricJS- Part 4]]></title>
            <link>https://medium.com/@aditi.6ti/add-filters-to-images-using-fabricjs-part-4-db00a1528a13?source=rss-9188150d345a------2</link>
            <guid isPermaLink="false">https://medium.com/p/db00a1528a13</guid>
            <category><![CDATA[html5]]></category>
            <category><![CDATA[fabricjs]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[front-end-development]]></category>
            <dc:creator><![CDATA[Mandev]]></dc:creator>
            <pubDate>Tue, 29 Nov 2022 10:55:04 GMT</pubDate>
            <atom:updated>2022-11-29T15:50:01.344Z</atom:updated>
            <content:encoded><![CDATA[<p>This is the final part of adding filters to Images using FabricJS. In this article I will illustrate how we can apply the pixelate and convolute effects.</p><h3>How to Convolute Images using FabricJS</h3><p>Convolution filters aren’t commonly heard of. However, you might have come across it in case you worked on <strong>image processing</strong> projects. Many studies on image processing require the edge detection, blurring or sharpening of images.</p><p>This is exactly where <strong>convolution filters</strong> come into play. The term <em>“Convolution”, </em>in <em>mathematics, </em>can express how the shape of one function is modified by another function.</p><p>The <strong>Convolute </strong>class of FabricJS extends <a href="http://fabricjs.com/docs/fabric.Image.filters.BaseFilter.html"><strong>fabric.Image.filters.BaseFilter</strong></a> and it can be called by simply creating an instance of fabric.Image.filters.Convolute.</p><h4><strong>Edge detection using FabricJS</strong></h4><p>Let’s see how we can use the Convolute class in order to detect the edges of an image. Edge detection simply means finding the <strong>boundaries</strong> of an image. Here is the <strong>original image</strong> that we are going to work with today:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/620/1*l0PSDv6oS-e8cFT_EPWrEQ.png" /><figcaption>Adding an <strong>Image</strong> to the <strong>canvas</strong> by using the <strong>fromURL() </strong>method</figcaption></figure><blockquote>It is absolutely okay to not know something. In case, you don’t know how to add images to the canvas, <a href="https://medium.com/@aditi.6ti/add-filters-to-images-using-fabricjs-part-1-140f5cb0bed1">click here</a>!</blockquote><p>Edge detection can be done using filters like <em>Canny, Laplacian, Prewitt, Sobel</em> etc. Here, I have used the<strong> Sobel filter </strong>matrix. The <em>first matrix</em> calculates the <strong>horizontal</strong> gradient of the image while the <em>second </em>matrix calculates the <strong>vertical</strong> gradient. Finally these two are combined in order to compute the edges of the image.</p><pre>         // Applying sobel filter<br>          img.filters.push(<br>            new fabric.Image.filters.Convolute({<br>              matrix: [-1, -2, -1, 0, 0, 0, 1, 2, 1], // horizontal<br>            })<br>          );<br>          img.filters.push(<br>            new fabric.Image.filters.Convolute({<br>              matrix: [-1, 0, 1, -2, 0, 2, -1, 0, 1], // vertical<br>            })<br>          );<br><br>// The applyFilters method applies the filter and re-renders the canvas<br>          img.applyFilters();</pre><p>This is how the complete code is going to look like:</p><pre>&lt;!DOCTYPE html&gt;<br>&lt;html&gt;<br>  &lt;head&gt;<br>    &lt;!-- Adding the FabricJS library --&gt;<br>    &lt;script<br>      src=&quot;https://cdnjs.cloudflare.com/ajax/libs/fabric.js/521/fabric.min.js&quot;<br>      integrity=&quot;sha512-nPzvcIhv7AtvjpNcnbr86eT6zGtiudLiLyVssCWLmvQHgR95VvkLX8mMpqNKWs1TG3Hnf+tvHpnGmpPS3yJIgw==&quot;<br>      crossorigin=&quot;anonymous&quot;<br>      referrerpolicy=&quot;no-referrer&quot;<br>    &gt;&lt;/script&gt;<br>  &lt;/head&gt;<br>  &lt;body&gt;<br>    &lt;canvas<br>      id=&quot;canvas&quot;<br>      style=&quot;border: 2px solid black&quot;<br>      width=&quot;400&quot;<br>      height=&quot;300&quot;<br>    &gt;&lt;/canvas&gt;<br><br>    &lt;script&gt;<br>      var canvas = new fabric.Canvas(&quot;canvas&quot;, {<br>        backgroundColor: &quot;black&quot;,<br>        isDrawingMode: false,<br>      });<br><br>      // Adding the image to the canvas<br>      fabric.Image.fromURL(<br>        &quot;https://images.unsplash.com/photo-1583795128727-6ec3642408f8?ixlib=rb-4.0.3&amp;ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&amp;auto=format&amp;fit=crop&amp;w=757&amp;q=80&quot;,<br>        function (img) {<br>          img.scale(0.45);<br>          img.set(&quot;top&quot;, 15);<br>          img.set(&quot;left&quot;, 200);<br>          img.set(&quot;originX&quot;, &quot;center&quot;);<br>          img.set(&quot;originY&quot;, &quot;top&quot;);<br><br>          // Applying sobel filter<br>          img.filters.push(<br>            new fabric.Image.filters.Convolute({<br>              matrix: [-1, -2, -1, 0, 0, 0, 1, 2, 1], // horizontal<br>            })<br>          );<br>          img.filters.push(<br>            new fabric.Image.filters.Convolute({<br>              matrix: [-1, 0, 1, -2, 0, 2, -1, 0, 1], // vertical<br>            })<br>          );<br><br>          // using applyFilters() method<br>          img.applyFilters();<br><br>          // Adding the image to the canvas by using the add() method<br>          canvas.add(img);<br>        },<br>        { crossOrigin: &quot;anonymous&quot; } // Not adding this line will result in exception in the console<br>      );<br>    &lt;/script&gt;<br>  &lt;/body&gt;<br>&lt;/html&gt;</pre><p>The output will be:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/628/1*hDOvtMjqZeZuuVjfSTCwxA.png" /><figcaption>Edge detection using Convolute class</figcaption></figure><h4>Image embossing using Convolute class in FabricJS</h4><p>We can also emboss the above image by using the Convolute class. Embossing an image can be done by replacing each pixel of the image by a highlight or shadow.</p><blockquote>Side Note : <strong>Pixel</strong>- short for Picture Element, is the <strong>smallest unit</strong> in a digital display. Each pixel consists of a subpixel that emits RGB color. You might know that when the number of pixels is<strong> high</strong>, it results in a <strong>clearer and sharper</strong> image and vice versa.</blockquote><p>The matrix below is the<strong> emboss filter </strong>matrix:</p><pre> matrix: [1, 1, 1, 1, 0.7, -1, -1, -1, -1]</pre><ul><li>We can <em>add</em> the <strong>emboss filter </strong>like so:</li></ul><pre>          // Applying the emboss filter<br>          img.filters.push(<br>            new fabric.Image.filters.Convolute({<br>              matrix: [1, 1, 1, 1, 0.7, -1, -1, -1, -1],<br>            })<br>          );<br><br>          // using applyFilters() method<br>          img.applyFilters();<br><br>          // Adding the image to the canvas by using the add() method<br>          canvas.add(img);</pre><p>Here is the output image:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/627/1*zccgIG62gCxyLw5GCGI31g.png" /><figcaption>Applying the<strong> Emboss filter</strong> using FabricJS</figcaption></figure><h3>How to apply Pixelate Effect using FabricJS</h3><p>We use the <em>pixelate</em> effect in order to make the pixels of an image larger. <em>Why would we want to use the pixelate effect?</em> We use it whenever we want to conceal a part of the image, brand logos or any sensitive information that we might just want to hide from others.</p><p>FabricJS provides us the <a href="http://fabricjs.com/docs/fabric.Image.filters.Pixelate.html#initialize"><strong>pixelate class</strong></a> which we can leverage to make the pixel size bigger.</p><ul><li>The <strong>first step</strong> is to <em>add </em>the image to the canvas by using the <em>fromURL()</em> method.</li><li>We initiate an instance of new<strong> fabric.Image.filters.Pixelate </strong>and assign the <em>blocksize</em> property <strong>a Number value </strong>in the optional options <strong>Object</strong>.</li><li>Here is a pixelated image where the<strong> blocksize</strong> property is passed a value of 10:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/623/1*9KsRwjl9aowEy5wI8nBCCg.png" /><figcaption>blocksize: 10</figcaption></figure><p>When we assign the <em>blocksize </em>property a <strong>value of 20</strong>, the image looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/628/1*qK4d-0G3S_maXQBondzlmw.png" /><figcaption>blocksize: 20</figcaption></figure><p>You can see the<em> difference</em> in <strong>pixel size</strong> in both of the above images. We can <strong>increase the size </strong>of each pixel by changing the value that we are passing to the <em>blocksize</em> property.</p><pre><br>          // Applying the Pixelate  filter<br>          img.filters.push(<br>            new fabric.Image.filters.Pixelate({<br>              blocksize: 20,<br>            })<br>          );<br><br>          // using applyFilters() method<br>          img.applyFilters();</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=db00a1528a13" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Add Filters to Images using FabricJS- Part 3]]></title>
            <link>https://medium.com/@aditi.6ti/add-filters-to-images-using-fabricjs-part-3-b3ad8337ce31?source=rss-9188150d345a------2</link>
            <guid isPermaLink="false">https://medium.com/p/b3ad8337ce31</guid>
            <category><![CDATA[frontend-development]]></category>
            <category><![CDATA[html5]]></category>
            <category><![CDATA[html-canvas]]></category>
            <category><![CDATA[fabricjs]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Mandev]]></dc:creator>
            <pubDate>Mon, 28 Nov 2022 18:01:19 GMT</pubDate>
            <atom:updated>2022-11-28T18:01:19.981Z</atom:updated>
            <content:encoded><![CDATA[<p>Today we’ll see how easy it is to apply <em>contrast </em>in images. We will also see how <strong>hue rotation effect </strong>can be achieved in FabricJS. So, let’s get right into it!</p><h3>How to make an image high Contrast in FabricJS</h3><p>Contrast in images determines the difference in <em>tone or brightness</em> between light and dark areas.</p><p>When we take a <em>bright</em> yellow and place it next to a blue color, we say that it has<a href="https://www.adobe.com/creativecloud/photography/discover/high-contrast-photography.html"> <strong>high contrast</strong></a>. This is because the <strong>difference of tones</strong> is extremely dramatic and can be easily distinguished by the human eye when they are placed next to each other.</p><p>Let’s see the original image that we are going to work with today:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/626/1*zM8Uyor0OSzULwwhIOz0sQ.png" /><figcaption>An <strong>Image object </strong>added to the <strong>canvas</strong> by using the <strong>fromURL()</strong> method</figcaption></figure><p>We use the Contrast class of FabricJS in order to apply contrast to images loaded onto the canvas.</p><ul><li>The first step is to <strong>add the image</strong> that we want by using the<em> fromURL()</em> method.</li><li>We also need to set <em>crossOrigin</em> as <strong>“anonymous”</strong> in order to avoid any exceptions.</li><li>Lastly, we create an instance of <strong>fabric.Image.filters.Contrast</strong> and add that to the filters array by using the push() method.</li><li>You can see I have followed the above steps in the code example below.</li></ul><blockquote>Previous article: <a href="https://medium.com/@aditi.6ti/add-filters-to-images-using-fabricjs-part-2-7a508fc92808">Change the brightness or add Noise to Images in Canvas</a></blockquote><pre>     var canvas = new fabric.Canvas(&quot;canvas&quot;, {<br>        backgroundColor: &quot;black&quot;,<br>        isDrawingMode: false,<br>      });<br><br>      fabric.Image.fromURL(<br>        &quot;https://images.unsplash.com/photo-1471922694854-ff1b63b20054?ixlib=rb-4.0.3&amp;ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&amp;auto=format&amp;fit=crop&amp;w=872&amp;q=80&quot;,<br>        function (img) {<br>          img.scale(0.45);<br>          img.set(&quot;top&quot;, 15);<br>          img.set(&quot;left&quot;, 200);<br>          img.set(&quot;originX&quot;, &quot;center&quot;);<br>          img.set(&quot;originY&quot;, &quot;top&quot;);<br><br>          // Adding contrast to our image<br>          img.filters.push(<br>            new fabric.Image.filters.Contrast({<br>              contrast: 0.3,<br>            })<br>          );<br><br>          img.applyFilters();<br>          canvas.add(img);<br>        },<br>        { crossOrigin: &quot;anonymous&quot; }<br>      );</pre><p>You can see the output image below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/622/1*RApveMmOKpWCqYkVwq-2iA.png" /><figcaption>Image with high contrast</figcaption></figure><p>You can see in the above image that the difference in tone and brightness between the light and dark areas is considerably high.</p><p>It should also be noted that:</p><ul><li>I have passed the <a href="http://fabricjs.com/docs/fabric.Image.filters.Contrast.html#contrast">contrast property </a>a value of<strong> 0.3 </strong>in the optional options Object.</li><li>The contrast property takes up a <strong>Number</strong> value anywhere between <strong>-1 to 1 </strong>which controls the contrast of the image.</li><li>The <em>default</em> value of <strong>contrast </strong>property is 0.</li></ul><h3>Adding the Hue rotation effect using FabricJS</h3><p>Hue is used to refer to the pure spectrum of colors. It doesn’t signify the brightness of a color but instead it’s a single value denoting the pure color of something.</p><p>The pure spectrum of colors can be seen in the color wheel or more commonly, the rainbow.</p><blockquote>You can read <a href="http://char.txa.cornell.edu/language/ELEMENT/COLOR/color.htm">this really cool article </a>in order to better understand Hue, Value and Color!</blockquote><p>In order to achieve the <strong>hue rotation effect</strong>, we use the<a href="http://fabricjs.com/docs/fabric.Image.filters.HueRotation.html#initialize"> hue rotation class</a> of FabricJS.</p><p>Just how we created an instance of the <em>Contrast class</em> in the previous example, we need to create an<strong> instance</strong> of <strong>fabric.Image.filters.HueRotation</strong> and add it to the filters<strong> Array</strong>.</p><p>We also need to pass the<strong> rotation</strong> property a Number value which lies anywhere between -1 and 1. This is the hue rotation value that we want in <em>radians</em>.</p><pre>      var canvas = new fabric.Canvas(&quot;canvas&quot;, {<br>        backgroundColor: &quot;black&quot;,<br>        isDrawingMode: false,<br>      });<br><br>      fabric.Image.fromURL(<br>        &quot;https://images.unsplash.com/photo-1471922694854-ff1b63b20054?ixlib=rb-4.0.3&amp;ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&amp;auto=format&amp;fit=crop&amp;w=872&amp;q=80&quot;,<br>        function (img) {<br>          img.scale(0.45);<br>          img.set(&quot;top&quot;, 15);<br>          img.set(&quot;left&quot;, 200);<br>          img.set(&quot;originX&quot;, &quot;center&quot;);<br>          img.set(&quot;originY&quot;, &quot;top&quot;);<br><br>          // Applying the hue rotation effect<br>          img.filters.push(<br>            new fabric.Image.filters.HueRotation({<br>              rotation: 0.5,<br>            })<br>          );<br><br>          img.applyFilters();<br>          canvas.add(img);<br>        },<br>        { crossOrigin: &quot;anonymous&quot; }<br>      );</pre><p>You can see the output image down below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/630/1*mY7fCUKCZcvN5o-RCoJ4ZA.png" /><figcaption>Hue rotation effect using FabricJS</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b3ad8337ce31" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>