<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Coder Legion - Recent posts tagged firsttime</title>
<link>https://coderlegion.com/tag/firsttime</link>
<description>Powered by Question2Answer</description>
<item>
<title>Freehand drawing with the Canvas API</title>
<link>https://coderlegion.com/30207/freehand-drawing-with-the-canvas-api</link>
<description>&lt;p&gt;When I was trying Canvas, I was completely puzzled. It felt so frustrating; I felt like I was writing in a completely different language instead of my usual JavaScript. But after reading the MDN doc and watching some videos...it still feels difficult and frustrating. But despite everything, let's try to build something. Polishing can come later.&lt;/p&gt;
&lt;p&gt;First, we need to select the ID from HTML&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;const canvas = document.querySelector(&quot;#draw&quot;);
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We need &lt;code&gt;canvas.getContext&lt;/code&gt; here to tell the browser what type of drawing we are going to do. For now, the screen is blank, so we need to tell it what we are going to do. The &lt;code&gt;&amp;lt;canvas&amp;gt;&lt;/code&gt; element has a method called &lt;code&gt;getContext&lt;/code&gt;. That's the one we are targeting here&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;const ctx = canvas.getContext(&quot;2d&quot;);
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We are going to draw on the whole window, so we need to target the whole window. But we can't just let HTML decide the window. We need to overwrite it, and JavaScript can help here with its &lt;code&gt;innerHeight&lt;/code&gt; and &lt;code&gt;innerWidth&lt;/code&gt; properties&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;That's a lot. I know. So, let's slow down and draw our first rectangle.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;function draw() {
  const canvas = document.querySelector(&quot;#draw&quot;);
  const ctx = canvas.getContext(&quot;2d&quot;);

  ctx.fillStyle = &quot;rgb(200 0 0 / 50%)&quot;; // setting the rect color with the fillStyle property
  ctx.fillRect(10, 10, 50, 50); // drawing the rectangle using the fillRect canvas api's method
}
draw();
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;That was easy, but we are not going to draw shapes. Our goal is to use freehand. Comment out or delete the previous &lt;code&gt;draw&lt;/code&gt; function.&lt;/p&gt;
&lt;p&gt;For now, we need four things before going forward. Set the color of the pen, how we want our pen to be, how the ends of lines look, and the size of the pen.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;ctx.strokeStyle = &quot;rgb(200 135 90)&quot;;
ctx.lineJoin = &quot;round&quot;;
ctx.lineCap = &quot;round&quot;;
ctx.lineWidth = 5;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here we can set the pen color with &lt;code&gt;strokeStyle&lt;/code&gt;, how corners between connected lines look with &lt;code&gt;lineJoin&lt;/code&gt;, the ending of the lines with &lt;code&gt;lineCap&lt;/code&gt; (default to butt), and the size with &lt;code&gt;lineWidth&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;We don't want to draw with every movement our mouse do. Only when we click the mouse and move it. So here it is&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;let isDrawing = false;
&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;let lastX = 0;
let lastY = 0;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;lastX/Y start at 0 as a placeholder. Later, they get replaced by the real mouse X/Y position from e.offsetX/Y.&lt;/p&gt;
&lt;p&gt;Let's draw it now:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;function draw(e) {
  if (!isDrawing) return;
  console.log(e);
  ctx.beginPath();
  ctx.moveTo(lastX, lastY);
  ctx.lineTo(e.offsetX, e.offsetY);
  ctx.stroke();
  [lastX, lastY] = [e.offsetX, e.offsetY];
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The condition prevents drawing when the mouse button is not being held down&lt;br&gt;
&lt;code&gt;beginPath()&lt;/code&gt; starts a new path every time&lt;br&gt;
&lt;code&gt;moveTo()&lt;/code&gt; sets the starting point of the line&lt;br&gt;
&lt;code&gt;lineTo()&lt;/code&gt; draws a line from the current point to the current mouse position&lt;br&gt;
&lt;code&gt;stroke()&lt;/code&gt; draws the path onto the canvas using the current stroke settings&lt;br&gt;
&lt;code&gt;[lastX, lastY] = [e.offsetX, e.offsetY]&lt;/code&gt; updates to the current mouse position by destructuring&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;// Mouse button pressed down → start drawing
canvas.addEventListener(&quot;mousedown&quot;, (e) =&amp;gt; {
  isDrawing = true;
  [lastX, lastY] = [e.offsetX, e.offsetY];
});

// Mouse button released → stop drawing
canvas.addEventListener(&quot;mouseup&quot;, () =&amp;gt; {
  isDrawing = false;
});

// Mouse leaves canvas → also stop
canvas.addEventListener(&quot;mouseout&quot;, () =&amp;gt; {
  isDrawing = false;
});

// Mouse moves → only draw if the flag is true
canvas.addEventListener(&quot;mousemove&quot;, draw);
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;That's it for today. Tada! Leave a comment if you want to. &lt;/p&gt;
&lt;p&gt;Further reading:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial/Drawing_shapes&quot;&gt;Drawing shapes with canvas&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial&quot;&gt;Canvas tutorial&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://www.w3schools.com/html/html5_canvas.asp&quot;&gt;HTML Canvas&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;
</description>
<category>Articles</category>
<guid isPermaLink="true">https://coderlegion.com/30207/freehand-drawing-with-the-canvas-api</guid>
<pubDate>Thu, 08 Oct 2026 02:55:43 +0000</pubDate>
</item>
</channel>
</rss>