<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Siddhartha</title>
    <description>The latest articles on DEV Community by Siddhartha (@isneuu).</description>
    <link>https://dev.to/isneuu</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F690669%2F35165d3f-33bc-4789-8262-1758868ea427.png</url>
      <title>DEV Community: Siddhartha</title>
      <link>https://dev.to/isneuu</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/isneuu"/>
    <language>en</language>
    <item>
      <title>Quotes Maker with Javascript using Fabric Js</title>
      <dc:creator>Siddhartha</dc:creator>
      <pubDate>Sun, 26 Sep 2021 02:40:00 +0000</pubDate>
      <link>https://dev.to/isneuu/quotes-maker-with-javascript-using-fabric-js-1op4</link>
      <guid>https://dev.to/isneuu/quotes-maker-with-javascript-using-fabric-js-1op4</guid>
      <description>&lt;p&gt;Fabric Js helps you easily create graphic editors.&lt;/p&gt;

&lt;p&gt;You must know basic Javascript for this purpose.&lt;/p&gt;

&lt;p&gt;It's easy.&lt;/p&gt;

&lt;p&gt;Demo: &lt;a href="https://codepen.io/isneuu/pen/XWgxLPv"&gt;https://codepen.io/isneuu/pen/XWgxLPv&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Download
&lt;/h1&gt;

&lt;p&gt;Download the Fabric Js's Javascript file from here.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://cdn.jsdelivr.net/npm/fabric@4.6.0/dist/fabric.min.js"&gt;https://cdn.jsdelivr.net/npm/fabric@4.6.0/dist/fabric.min.js&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Or visit &lt;a href="http://fabricjs.com/"&gt;http://fabricjs.com/&lt;/a&gt; for better instructions.&lt;/p&gt;

&lt;h1&gt;
  
  
  Our HTML
&lt;/h1&gt;

&lt;p&gt;We will have simple features:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Add quote&lt;/li&gt;
&lt;li&gt;Add author&lt;/li&gt;
&lt;li&gt;Delete text&lt;/li&gt;
&lt;li&gt;Download Image
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div class = "buttons"&amp;gt;

  &amp;lt;button class = "button" type="button" onclick="addTextToCanvas('quote')"&amp;gt;Add Quote&amp;lt;/button&amp;gt;

  &amp;lt;button class = "button" type="button" onclick="addTextToCanvas('author')"&amp;gt;Add Author&amp;lt;/button&amp;gt;

  &amp;lt;a href="" class = "button" onclick = "convertToImage()" id='downloader'&amp;gt;Download&amp;lt;/a&amp;gt;

  &amp;lt;button type="button" id='delete' name="button"&amp;gt;delete text layer&amp;lt;/button&amp;gt;

&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next we'll create the Canvas.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;canvas id="myCanvas" width="800" height="500" style="border:1px solid #000000; margin-top  : 30vh;  margin-left : 25%"&amp;gt;
&amp;lt;/canvas&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can see we created a canvas container with id myCanvas, width, height, and basic styling. &lt;/p&gt;

&lt;p&gt;This is inline styling. Please feel free to create a class and style.&lt;/p&gt;

&lt;h1&gt;
  
  
  Import Fabric Js
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;script src="fabric.js" charset="utf-8"&amp;gt;&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Writing our Javascript
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; const canvas = new fabric.Canvas('myCanvas');
 canvas.backgroundColor = '#ffffff';

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It will create a canvas from the Canvas ID. &lt;/p&gt;

&lt;p&gt;We've set the background color of canvas to white. If not mentioned, it will be transparent.&lt;/p&gt;

&lt;h6&gt;
  
  
  Adding a text element
&lt;/h6&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
textBox = new fabric.IText("My Text", {
          left : 70,
          top: 100});

canvas.add(textBox);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;First we created a text element that will appear on position top 100, left 70 from the Canvas.&lt;/p&gt;

&lt;p&gt;Then we added that text element to the Canvas.&lt;/p&gt;

&lt;p&gt;Easy, right?&lt;/p&gt;

&lt;p&gt;Now let's add more controls.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    textBox = new fabric.IText("Andrea Dykstra", {
          left : 70,
          top: 350,
          fontFamily: 'Inter',
          fontWeight: 'Normal',
          fontSize: '22',
});
    }

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We imported our FontFamily, defined FontWeight and specified fontSize.&lt;/p&gt;

&lt;h1&gt;
  
  
  Hiding Controls
&lt;/h1&gt;

&lt;p&gt;By now, you know you can see the different controls in the text layer such as scale, rotate, etc.&lt;/p&gt;

&lt;p&gt;We may not need all of them. &lt;/p&gt;

&lt;p&gt;How do we hide them?&lt;/p&gt;

&lt;p&gt;Here's what I found in Codepen.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  var HideControls = {
          'tl':true, //topleft
          'tr':true, //topright
          'bl':true, //bottomleft
          'br':true, //bottomright
          'ml':false, //middleleft
          'mt':false, //middletop
          'mr':false, //middleright
          'mb':false, //middlebuttom
          'mtr':false //middlerotate
      };

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;First, you specify what you want to show and what you don't want.&lt;/p&gt;

&lt;p&gt;I have decided to show only four controls on four different directions.&lt;/p&gt;

&lt;p&gt;Just set either true or false depending on your desire.&lt;/p&gt;

&lt;p&gt;Next, before you add the textelement to canvas, add this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;textBox.setControlsVisibility(HideControls);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You'll get the code, relax.&lt;/p&gt;

&lt;h1&gt;
  
  
  Delete Text
&lt;/h1&gt;

&lt;p&gt;To delete the item, you get the active element in the Canvas and remove it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;document.getElementById('delete').onclick = function() {
var activeObject = canvas.getActiveObjects();
canvas.discardActiveObject();
canvas.remove(...activeObject);

      };

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Author's note: I wrote this in my class. My teacher pointed I wasn't paying attention throughout the whole class duration. &lt;/p&gt;

&lt;p&gt;Anyway, we continue.&lt;/p&gt;

&lt;p&gt;This code removes the active selected objects from the canvas.&lt;/p&gt;

&lt;p&gt;I don't know what discard active object does.&lt;/p&gt;

&lt;h1&gt;
  
  
  Download Item
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  var save = document.getElementById('downloader');
        save.addEventListener('click', sveimg, false);

        function sveimg(e) {
            this.href = canvas.toDataURL({
                format: 'png',
                quality: 0.8
            });
            this.download = 'canvas.png'
        }

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;First we attach an event listener to 'downloader' ID.&lt;/p&gt;

&lt;p&gt;On click, it will trigger a function which will add the image data to the 'href' of the download link.&lt;/p&gt;

&lt;p&gt;And the file will be downloaded.&lt;/p&gt;

&lt;p&gt;The format of the download file will be PNG and will be named 'canvas.png'.&lt;/p&gt;

&lt;h1&gt;
  
  
  Complete Code
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;link rel="preconnect" href="https://fonts.googleapis.com"&amp;gt;
&amp;lt;link rel="preconnect" href="https://fonts.gstatic.com" crossorigin&amp;gt;
&amp;lt;link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;700&amp;amp;display=swap" rel="stylesheet"&amp;gt;

&amp;lt;script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"&amp;gt;&amp;lt;/script&amp;gt;

&amp;lt;div class = "buttons"&amp;gt;

  &amp;lt;button class = "button" type="button" onclick="addTextToCanvas('quote')"&amp;gt;Add Quote&amp;lt;/button&amp;gt;

  &amp;lt;button class = "button" type="button" onclick="addTextToCanvas('author')"&amp;gt;Add Author&amp;lt;/button&amp;gt;

  &amp;lt;a href="" class = "button" onclick = "convertToImage()" id='downloader'&amp;gt;Download&amp;lt;/a&amp;gt;

  &amp;lt;button type="button" id='delete' name="button"&amp;gt;delete text layer&amp;lt;/button&amp;gt;

&amp;lt;/div&amp;gt;

&amp;lt;canvas id="myCanvas" width="800" height="500" style="border:1px solid #000000; margin-top  : 30vh;  margin-left : 25%"&amp;gt;
&amp;lt;/canvas&amp;gt;
&amp;lt;script src="fabric.js" charset="utf-8"&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;script type="text/javascript"&amp;gt;


  const canvas = new fabric.Canvas('myCanvas');
  canvas.backgroundColor = '#ffffff';
  //background color of canvas is white. If not mentioned, it will be transparent.


  var HideControls = {
          'tl':true, //topleft
          'tr':true, //topright
          'bl':true, //bottomleft
          'br':true, //bottomright
          'ml':false, //middleleft
          'mt':false, //middletop
          'mr':false, //middleright
          'mb':false, //middlebuttom
          'mtr':false //middlerotate
      };


//function to add text
//function is triggered by the button
  function addTextToCanvas(value){
    console.log(value);
    let textBox;

    if(value.trim()=='quote'){
      textBox = new fabric.IText("In order to love who you are, \nyou cannot hate the experiences \nthat shaped you.", {
          left : 70,
          top: 100,
          fontFamily: 'Inter',
          fontWeight: 'Bold',
          cornerColor: 'blue',
          cornerStrokeColor: 'red',
          borderColor: 'red',
          cornerSize: 12,
          padding: 10,
          textAlign: 'left',
          cornerStyle: 'circle',
          borderDashArray: [0, 0]
        });

    }else{
      textBox = new fabric.IText("Andrea Dykstra", {
          left : 70,
          top: 350,
          fontFamily: 'Inter',
          fontWeight: 'Normal',
          fontSize: '22',
          cornerColor: 'blue',
          cornerStrokeColor: 'red',
          borderColor: 'red',
          cornerSize: 12,
          padding: 10,
          textAlign: 'left',
          cornerStyle: 'circle',
          borderDashArray: [0, 0]});

    }

    canvas.add(textBox); //add the textbox variable into the canvas

    textBox.setControlsVisibility(HideControls); //hide controls such as scale, rotate, etc

    canvas.setActiveObject(textBox); //automatically selects the item in the canvas. Try without using this code.

  }




        var save = document.getElementById('downloader');
        save.addEventListener('click', sveimg, false);

        function sveimg(e) {
            this.href = canvas.toDataURL({
                format: 'png',
                quality: 0.8
            });
            this.download = 'canvas.png'
        }




 document.getElementById('delete').onclick = function() {
   var activeObject = canvas.getActiveObjects();

canvas.discardActiveObject();
canvas.remove(...activeObject);

      };







&amp;lt;/script&amp;gt;

&amp;lt;style media="screen"&amp;gt;

  body{
  color : black;
  font-family: 'Inter', sans-serif;
}


&amp;lt;/style&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;Demo: &lt;a href="https://codepen.io/isneuu/pen/XWgxLPv"&gt;https://codepen.io/isneuu/pen/XWgxLPv&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Fabric Js is a nice one.&lt;/p&gt;

&lt;p&gt;Here are more resources to learn:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://fabricjs.com/"&gt;http://fabricjs.com/&lt;/a&gt; (Official documentation)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://codepen.io/search/pens?q=fabric+js"&gt;https://codepen.io/search/pens?q=fabric+js&lt;/a&gt; (Simple Fabric Js projects with code)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/search?q=fabric+js"&gt;https://github.com/search?q=fabric+js&lt;/a&gt; (Hundreds of projects about Fabric Js)&lt;/p&gt;

&lt;p&gt;Thanks for reading. &lt;/p&gt;

&lt;p&gt;Connect with me at &lt;a href="https://isneuu.com"&gt;https://isneuu.com&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The contents of this code were written with the help of projects in Codepen and FabricJs examples.&lt;/p&gt;

&lt;p&gt;Thank you.&lt;/p&gt;

&lt;p&gt;Cover image: Steve Johnson on Unsplash&lt;/p&gt;

</description>
      <category>fabricjs</category>
      <category>canvas</category>
      <category>javascript</category>
    </item>
    <item>
      <title>What kind of graphics do you want for your product? :D </title>
      <dc:creator>Siddhartha</dc:creator>
      <pubDate>Sat, 21 Aug 2021 08:12:21 +0000</pubDate>
      <link>https://dev.to/isneuu/what-kind-of-graphics-do-you-want-for-your-product-d-3im5</link>
      <guid>https://dev.to/isneuu/what-kind-of-graphics-do-you-want-for-your-product-d-3im5</guid>
      <description>&lt;p&gt;As developers it may be hard to get the right kind of design for your product.&lt;/p&gt;

&lt;p&gt;I want to design open-source product graphics such as product announcements, feature updates, launches, etc. Do you have any specific wants? I can include them. They are going to be free to use.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
