title: How to use a for loop to move a rectangle position in a canvas
body: <p>Firstly, I'm new to canvas and I've been trying to wrap my head around this for 2 days so I'd really appreciate any help. My goal is to make the green rectangle animate upwards 1 pixel at a time (the amount doesn't really matter for now) but i'll eventually also want to do the same for the red one. If I annotate out the for loop you can see the rectangles in my desired starting position, however when i add the for loop back in the canvas is blank when in theory I'm hoping for the green rectangle to move upwards. (Or downwards, doesn't actually matter).</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>$(document).ready(function() {
    var canvas = document.getElementById("canvas"),
      ctx = canvas.getContext("2d"),
      cw = canvas.width,
      ch = canvas.height;
  
  
    function rectangles() {
  
      var y = 0;
      // Starting point rectangles
      ctx.save();
      ctx.clearRect(0, 0, cw, ch);
      ctx.beginPath();
      ctx.fillStyle = "#006847";
      ctx.fillRect(0, 0, 200, 450);
      ctx.closePath();
  
      ctx.beginPath();
      ctx.fillStyle = "#CE1126";
      ctx.fillRect(400, 0, 200, 450);
      ctx.closePath();
  
      ctx.save();
      /*for (i = 0; i &lt; 450; i++) {
          y = y + 1;
          ctx.beginPath();
          ctx.fillRect(0, y, 200, 450);
          ctx.closePath();
          ctx.clearRect(0, 0, cw, ch);
      }*/
    }
  
    setInterval(rectangles, 2000);
  });</code></pre>
  <pre class="snippet-code-css lang-css prettyprint-override"><code>CSS : #canvas {
    border: 3px #000 solid;
    margin: 0 auto;
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"&gt;&lt;/script&gt;
  &lt;div id="canvas-container"&gt;
    &lt;canvas id="canvas" height="450" width="600"&gt;&lt;/canvas&gt;
  &lt;/div&gt;</code></pre>
  </div>
  </div>
  </p>