<?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" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ The Iron Yard - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ The Iron Yard - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Tue, 25 Aug 2026 22:05:12 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/tag/the-iron-yard/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ HTML Canvas Explained: An Intro to  HTML5 Canvas and JavaScript Functions ]]>
                </title>
                <description>
                    <![CDATA[ By Adam Recvlohe Before the Emoji, some background… I started working in the web development field about 6 months ago after spending most of my career in education. The transition has been great and I am very thankful for the opportunity to work on r... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/javascript-functions-af6f9186a553/</link>
                <guid isPermaLink="false">66c358ecaf2b7c40e7d7eb00</guid>
                
                    <category>
                        <![CDATA[ The Iron Yard ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Design ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ General Programming ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Development ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Fri, 25 Mar 2016 01:58:19 +0000</pubDate>
                <media:content url="https://cdn-media-1.freecodecamp.org/images/1*fzz0bNoOcUnsihyppXAMEA.jpeg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Adam Recvlohe</p>
<h2 id="heading-before-the-emoji-some-background">Before the Emoji, some background…</h2>
<p>I started working in the web development field about 6 months ago after spending most of my career in education. The transition has been great and I am very thankful for the opportunity to work on real-world web applications.</p>
<p>I very happy working in the industry but from my perspective, there is still plenty to learn. Therefore, from the day I started as a JavaScript Developer I have continued to spend time studying each evening to level up my skills.</p>
<p>Along with studying, I recently began teaching an ‘Intro to JavaScript Course’ to Tampa Bay teenagers (at The Iron Yard in St.Pete, Florida). This has been a great experience for many reasons. First, it has challenged me to learn more about the intricacies and nuances of the JavaScript language.</p>
<p>Second, I have gotten a chance to teach again, which is one of my passions. And third, I got to reexamine how I learned to program and how that differs drastically from beginners who aren’t even sure if they like coding and in some cases couldn’t care less about what I have to say.</p>
<p>You see, the curriculum that I originally thought would be great for this class was JavaScript in three ways: JS in the DOM, JS on the server, and functional JS programming.</p>
<p>After the first day, and a good talking to from my Teaching Assistants, I realized I was totally off base. These topics may interest me, but certainly don’t entertain a youth who just wants to play add-sponsored games in the browser. I totally reevaluated what I would teach, and in the process began to have fun!</p>
<p>Below is the first lesson I gave to the students where I start out discussing functions and end up creating a smiley face emoji. Enjoy!</p>
<p>If you want to follow along as we talk about functions, open up a browser and go to <a target="_blank" href="http://repl.it">repl.it</a> and under popular languages choose NodeJS. A REPL (Read Evaluate Print Loop) should open up for you and you can follow along with the code.</p>
<h2 id="heading-what-are-functions">What are functions?</h2>
<p>To understand how we will use HTML5 canvas we have to understand a little bit about functions.</p>
<p>“Functions are self-contained modules of code that accomplish a specific task. Functions usually “take in” data, process it, and “return” a result. Once a function is written, it can be used over and over again.”</p>
<p>Now let me give you a few examples of the type of functions we will be dealing with.</p>
<h2 id="heading-function-types">Function Types</h2>
<h3 id="heading-regular-ole-function">Regular Ole’ Function</h3>
<p>We declare a basic function using the JavaScript keyword <em>function</em>.</p>
<pre><code><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">sayHelloTo</span>(<span class="hljs-params">name</span>) </span>{    <span class="hljs-keyword">return</span> ‘Hello ‘ + name;}sayHelloTo(‘Adam’);
</code></pre><p>This function takes one parameter called <em>name</em>. It is a variable that is passed to the <em>sayHelloTo</em> function. Therefore, when the program executes it will pass in what is provided. In my case it is <em>Adam</em>, so in the console you will see <em>Hello Adam</em>.</p>
<h3 id="heading-constructor-pattern">Constructor Pattern</h3>
<p>We can also create a function using the constructor pattern.</p>
<pre><code><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Person</span>(<span class="hljs-params">name</span>) </span>{    <span class="hljs-built_in">this</span>.name = name;    <span class="hljs-built_in">this</span>.sayHello = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{        <span class="hljs-keyword">return</span> “Hello, my name is “ + <span class="hljs-built_in">this</span>.name;    }}<span class="hljs-keyword">var</span> me = <span class="hljs-keyword">new</span> Person(“Adam”);me.sayHello();
</code></pre><p>The Javascript keyword <em>this</em> refers to the function. What that means is when we pass in a variable like <em>name</em>, just as we did before, we can assign it to the function and any instance of that function. To create an instance we use the JavaScript keyword <em>new</em>. When this new instance of the function is created it also has as its properties a <em>this.name</em> value and a <em>this.sayHello</em> method. When we created the instance of the method we passed in our name: <em>var me = new Person(‘Adam’)</em>. When you look at the <em>sayHello</em> method, it uses that <em>name</em>, that is now part of that instance, to create a sentence. If you execute this code in the NodeJS REPL on repl.it you should see it output <em>Hello, my name is Adam</em>.</p>
<p>Now that we got the boring stuff out the way, let’s draw on some canvas. The way I taught this next section was using codepen.io. What I suggest is if you want to follow along, go to codepen.io, create an account, then create a new pen and you should be set. Be sure to activate you account if you want to save your work.</p>
<h2 id="heading-lets-draw-on-canvas">Let’s Draw on Canvas</h2>
<p>First, we need to create the canvas to be able to draw on it. In your HTML create a canvas tag.</p>
<pre><code>&lt;canvas id=“canvas”&gt;&lt;/canvas&gt;
</code></pre><p>Now it’s JavaScript from here on!</p>
<p>We need to grab our canvas element from the DOM and declare it as a variable. This will allow us to set its context. We aren’t that skilled with ‘3d’ yet, so we will stick with a ‘2d’ context.</p>
<pre><code><span class="hljs-keyword">var</span> canvas = <span class="hljs-built_in">document</span>.getElementById(“canvas”);<span class="hljs-keyword">var</span> context = canvas.getContext(“<span class="hljs-number">2</span>d”);
</code></pre><p>We can also give the canvas its <em>width</em> and <em>height</em> properties.</p>
<pre><code><span class="hljs-keyword">var</span> canvas = <span class="hljs-built_in">document</span>.getElementById(“canvas”);canvas.width = <span class="hljs-number">800</span>;canvas.height = <span class="hljs-number">800</span>;<span class="hljs-keyword">var</span> context = canvas.getContext(“<span class="hljs-number">2</span>d”);
</code></pre><p>I want to point out here that the <em>canvas</em> is acting exactly like an object. It has properties and methods just like we saw from our constructor function above. Slightly different in how we declared it but functionally it operates very similar. So you see that it has <em>height</em> and <em>width</em> properties as well as a <em>getContext</em> method.</p>
<p>Now let’s put all of that into a function so that you can get somewhat familiar with functional programming.</p>
<pre><code><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">draw</span>(<span class="hljs-params"></span>) </span>{  <span class="hljs-keyword">var</span> canvas = <span class="hljs-built_in">document</span>.getElementById(“canvas”);  canvas.width = <span class="hljs-number">800</span>;  canvas.height = <span class="hljs-number">800</span>;  <span class="hljs-keyword">var</span> context = canvas.getContext(“<span class="hljs-number">2</span>d”);}
</code></pre><p>Nothing will show up on the screen just yet, we will use the <em>fillRect</em> method to help us with that.</p>
<pre><code><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">draw</span>(<span class="hljs-params"></span>) </span>{  <span class="hljs-keyword">var</span> canvas = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"canvas"</span>);  canvas.width = <span class="hljs-number">800</span>;  canvas.height = <span class="hljs-number">800</span>;  <span class="hljs-keyword">var</span> context = canvas.getContext(<span class="hljs-string">"2d"</span>);  context.fillRect(<span class="hljs-number">10</span>,<span class="hljs-number">10</span>, <span class="hljs-number">100</span>, <span class="hljs-number">200</span>);}
</code></pre><p>If you haven’t guessed it, the <em>fillRect</em> method takes four parameters: x coordinate, y coordinate, width, and height. On canvas, the x-axis starts at 0 on the left and to infinity going right. The y-axis starts at 0 from the top and to infinity going down. So when we start at (10, 10) we are placing the imaginary cursor on point (x = 10, y = 10) and going 100 to the right and 200 down from that point.</p>
<p>As you may have noticed, it still hasn’t been added to the page yet. Add a simple <em>window.onload</em> function and have it equal our finished draw function.</p>
<pre><code><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">draw</span>(<span class="hljs-params"></span>) </span>{  <span class="hljs-keyword">var</span> canvas = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"canvas"</span>);  canvas.width = <span class="hljs-number">800</span>;  canvas.height = <span class="hljs-number">800</span>;  <span class="hljs-keyword">var</span> context = canvas.getContext(<span class="hljs-string">"2d"</span>);  context.fillRect(<span class="hljs-number">10</span>,<span class="hljs-number">10</span>, <span class="hljs-number">100</span>, <span class="hljs-number">200</span>);}<span class="hljs-built_in">window</span>.onload = draw;
</code></pre><p>You might be wondering why the draw function was executed even though we didn’t execute it with parens <em>( ).</em> That’s because <em>window.onload</em> is a function. That’s the same as saying:</p>
<pre><code><span class="hljs-built_in">window</span>.onload = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{<span class="hljs-comment">// Do stuff here like what we put in draw();}</span>
</code></pre><p>That means <em>window.onload</em> executes a function when the window is loaded, so what ends up happening is <em>window.onload</em> with its magical powers puts invisible parens around draw, thus executing it. A lot of magic is involved. But now you know the hocus pocus.</p>
<p>Let’s add some color for fun! Here we use the <em>fillStyle</em> method for that. It needs to come before <em>fillRect</em> or it won’t show.</p>
<pre><code><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">draw</span>(<span class="hljs-params"></span>) </span>{  <span class="hljs-keyword">var</span> canvas = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"canvas"</span>);  canvas.width = <span class="hljs-number">800</span>;  canvas.height = <span class="hljs-number">800</span>;  <span class="hljs-keyword">var</span> context = canvas.getContext(<span class="hljs-string">"2d"</span>);  context.fillStyle = <span class="hljs-string">"blue"</span>;  context.fillRect(<span class="hljs-number">10</span>,<span class="hljs-number">10</span>, <span class="hljs-number">100</span>, <span class="hljs-number">200</span>);}<span class="hljs-built_in">window</span>.onload = draw;
</code></pre><p>Here is a sample of that on codepen:</p>
<h2 id="heading-lets-draw-some-other-shapes">Let’s draw some other shapes!</h2>
<p>That was pretty simple. Let’s draw some other shapes now. Just as we did before we will create a function and instantiate our canvas with a <em>width</em>, <em>height</em>, and <em>context</em>.</p>
<pre><code><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">triangle</span>(<span class="hljs-params"></span>) </span>{  <span class="hljs-keyword">var</span> canvas = <span class="hljs-built_in">document</span>.getElementById(“canvas”);  <span class="hljs-keyword">var</span> context = canvas.getContext(“<span class="hljs-number">2</span>d”);  canvas.width = <span class="hljs-number">400</span>;  canvas.height = <span class="hljs-number">400</span>;}
</code></pre><p>So we don’t forget, change the <em>onload</em> function to take the triangle function now.</p>
<pre><code><span class="hljs-built_in">window</span>.onload = triangle;
</code></pre><p>Now that we have our canvas, let’s start to draw lines on the canvas to create our triangle.</p>
<pre><code><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">triangle</span>(<span class="hljs-params"></span>) </span>{  <span class="hljs-keyword">var</span> canvas = <span class="hljs-built_in">document</span>.getElementById(“canvas”);  <span class="hljs-keyword">var</span> context = canvas.getContext(“<span class="hljs-number">2</span>d”);  canvas.width = <span class="hljs-number">400</span>;  canvas.height = <span class="hljs-number">400</span>;      context.beginPath();  context.moveTo(<span class="hljs-number">75</span>, <span class="hljs-number">50</span>);}
</code></pre><p>Here we are starting our path and moving the cursor to point (x = 75, y = 50).</p>
<p>Now let’s go to town drawing some lines.</p>
<pre><code><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">triangle</span>(<span class="hljs-params"></span>) </span>{  <span class="hljs-keyword">var</span> canvas = <span class="hljs-built_in">document</span>.getElementById(“canvas”);  <span class="hljs-keyword">var</span> context = canvas.getContext(“<span class="hljs-number">2</span>d”);  canvas.width = <span class="hljs-number">400</span>;  canvas.height = <span class="hljs-number">400</span>;      context.beginPath();  context.moveTo(<span class="hljs-number">75</span>, <span class="hljs-number">50</span>);  context.lineTo(<span class="hljs-number">100</span>, <span class="hljs-number">75</span>);  context.lineTo(<span class="hljs-number">100</span>, <span class="hljs-number">25</span>);  context.stroke();}
</code></pre><p>This created the first two lines that we needed. To finish it off we go back to where we started.</p>
<pre><code><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">triangle</span>(<span class="hljs-params"></span>) </span>{  <span class="hljs-keyword">var</span> canvas = <span class="hljs-built_in">document</span>.getElementById(“canvas”);  <span class="hljs-keyword">var</span> context = canvas.getContext(“<span class="hljs-number">2</span>d”);  canvas.width = <span class="hljs-number">400</span>;  canvas.height = <span class="hljs-number">400</span>;      context.beginPath();  context.moveTo(<span class="hljs-number">75</span>, <span class="hljs-number">50</span>);  context.lineTo(<span class="hljs-number">100</span>, <span class="hljs-number">75</span>);  context.lineTo(<span class="hljs-number">100</span>, <span class="hljs-number">25</span>);  context.lineTo(<span class="hljs-number">75</span>, <span class="hljs-number">50</span>); <span class="hljs-comment">// Back to where we started  context.stroke();}</span>
</code></pre><p>To fill in the triangle we can use the fill method.</p>
<pre><code><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">triangle</span>(<span class="hljs-params"></span>) </span>{  <span class="hljs-keyword">var</span> canvas = <span class="hljs-built_in">document</span>.getElementById(“canvas”);  <span class="hljs-keyword">var</span> context = canvas.getContext(“<span class="hljs-number">2</span>d”);  canvas.width = <span class="hljs-number">400</span>;  canvas.height = <span class="hljs-number">400</span>;      context.beginPath();  context.moveTo(<span class="hljs-number">75</span>, <span class="hljs-number">50</span>);  context.lineTo(<span class="hljs-number">100</span>, <span class="hljs-number">75</span>);  context.lineTo(<span class="hljs-number">100</span>, <span class="hljs-number">25</span>);  context.lineTo(<span class="hljs-number">75</span>, <span class="hljs-number">50</span>);  context.stroke();  context.fill();}
</code></pre><p>Here is what that looks like in the wild:</p>
<p>We can do the same thing now and easily create a giant pyramid.</p>
<pre><code><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">pyramid</span>(<span class="hljs-params"></span>) </span>{  <span class="hljs-keyword">var</span> canvas = <span class="hljs-built_in">document</span>.getElementById(“canvas”);  <span class="hljs-keyword">var</span> context = canvas.getContext(“<span class="hljs-number">2</span>d”);  canvas.width = <span class="hljs-number">400</span>;  canvas.height = <span class="hljs-number">400</span>;}
</code></pre><p>Remember to change the <em>onload</em> function to pyramid.</p>
<pre><code><span class="hljs-built_in">window</span>.onload = pyramid;
</code></pre><p>Let’s now move the cursor to where we want it to be.</p>
<pre><code><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">pyramid</span>(<span class="hljs-params"></span>) </span>{  <span class="hljs-keyword">var</span> canvas = <span class="hljs-built_in">document</span>.getElementById(“canvas”);  <span class="hljs-keyword">var</span> context = canvas.getContext(“<span class="hljs-number">2</span>d”);  canvas.width = <span class="hljs-number">400</span>;  canvas.height = <span class="hljs-number">400</span>;      context.beginPath();  context.moveTo(<span class="hljs-number">200</span>, <span class="hljs-number">0</span>);}
</code></pre><p>I want my pyramid to take up as much space as possible, so I am starting out at the very top of my canvas and exactly in the middle of the x-axis.</p>
<p>Now we can begin drawing our shape and filling it in.</p>
<pre><code>context.lineTo(<span class="hljs-number">0</span>, <span class="hljs-number">400</span>);context.lineTo(<span class="hljs-number">400</span>, <span class="hljs-number">400</span>);context.lineTo(<span class="hljs-number">200</span>, <span class="hljs-number">0</span>);context.stroke();context.fillStyle = “orange”;context.fill();
</code></pre><p>Done! You should now have a nice orange pyramid on your screen because of course you are part the Illuminati. Don’t lie!</p>
<p>Here is the finished product that you can play with:</p>
<h2 id="heading-emojis">Emojis!</h2>
<p>Now for what you came for: Emojis!</p>
<p>Just as we did before we set up our canvas.</p>
<pre><code><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">smileyFaceEmoji</span>(<span class="hljs-params"></span>) </span>{    <span class="hljs-keyword">var</span> canvas = <span class="hljs-built_in">document</span>.getElementById(“canvas”);    <span class="hljs-keyword">var</span> context = canvas.getContext(“<span class="hljs-number">2</span>d”);    canvas.width = <span class="hljs-number">500</span>;    canvas.height = <span class="hljs-number">500</span>;}
</code></pre><p>Remember to change <em>onload</em> to this function.</p>
<pre><code><span class="hljs-built_in">window</span>.onload = smileyFaceEmoji;
</code></pre><p>Now let’s draw our face.</p>
<pre><code>context.beginPath();context.arc(<span class="hljs-number">250</span>, <span class="hljs-number">250</span>, <span class="hljs-number">100</span>,<span class="hljs-number">0</span>,<span class="hljs-built_in">Math</span>.PI*<span class="hljs-number">2</span>, <span class="hljs-literal">true</span>);context.stroke();
</code></pre><p>I kind of switched things up here using the <em>arc</em> function. The <em>arc</em> function takes quite a few arguments: x coordinate, y coordinate, radius, starting point in radians, ending point in radians, and whether it is drawn clockwise (we said true). As opposed to how a rectangle is made starting at one point and moving to the next, the point (x = 250, y = 250) is actually the middle of the circle and then extending out 100 pixels.</p>
<p>Cool huh?! Next comes the eyes.</p>
<pre><code>context.moveTo(<span class="hljs-number">235</span>, <span class="hljs-number">225</span>);context.arc(<span class="hljs-number">225</span>, <span class="hljs-number">225</span>, <span class="hljs-number">10</span>, <span class="hljs-number">0</span>, <span class="hljs-built_in">Math</span>.PI*<span class="hljs-number">2</span>, <span class="hljs-literal">true</span>);context.moveTo(<span class="hljs-number">285</span>, <span class="hljs-number">225</span>);context.arc(<span class="hljs-number">275</span>, <span class="hljs-number">225</span>, <span class="hljs-number">10</span>, <span class="hljs-number">0</span>, <span class="hljs-built_in">Math</span>.PI*<span class="hljs-number">2</span>, <span class="hljs-literal">true</span>);context.stroke();
</code></pre><p>Then the mouth.</p>
<pre><code>context.moveTo(<span class="hljs-number">250</span>, <span class="hljs-number">275</span>);context.arc(<span class="hljs-number">250</span>, <span class="hljs-number">275</span>, <span class="hljs-number">50</span>, <span class="hljs-number">0</span>, <span class="hljs-built_in">Math</span>.PI, <span class="hljs-literal">false</span>); <span class="hljs-comment">// Why is this last value false? Why did you just use Math.PI?context.moveTo(250, 275);context.lineTo(200, 275);context.stroke();</span>
</code></pre><p>Here is what the finished product looks like:</p>
<p>You did it, you just made a smiley face emoji! Gosh darn it I am proud of you! If you want to take your canvas skills to the next level try out one of the exercises below.</p>
<h3 id="heading-exercises">Exercises</h3>
<ol>
<li>Draw a poop emoji.</li>
<li>Draw your initials in cursive.</li>
</ol>
<h2 id="heading-in-summary">In summary</h2>
<p>In this lesson you learned about functions: how to create functions, execute functions, and use functions to build small programs that draw lines on a canvas. We learned that functions take many forms and can be given properties and methods. I hope you enjoyed this lesson as it was my intention to show you the power of functions without bogging you down with jargon, instead using visual stimuli to bring them to life!</p>
<p>If you want to see all the code for this lesson go to my codepen <a target="_blank" href="http://codepen.io/arecvlohe/pen/QNGjBr/">here</a>.</p>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
