title: HTML5 Canvas: Using constructor functions to create complex Canvas shapes
body: <p>I'm currently working on a HTML5 Canvas project (I wrote a separate question about it <a href="https://stackoverflow.com/questions/67259351/callback-function-inside-if-statement-not-working">here</a>). I think one of the solutions to the problems I am facing would be to create a reference for the path (saved as an object), and use the method <code>ispointinpath</code> to check if my mouse position is within the path or not – if its not, it resets the game.</p>
  <p>I took a stab at creating a constructor function for a complex path shape. Here's what the complex shape looks like as raw code:</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>var canvas = document.querySelector('canvas');
  
  // returning a drawing context to a variable 'c'
  // allows you to draw 2d elements
  var c = canvas.getContext('2d'); 
  
    canvas.width = 1000;
    canvas.height = 700;
    canvas.style.width = 1000;
    canvas.style.height = 700;
    
          c.beginPath();
          c.moveTo(350, 650); //start
          c.lineTo(350, 200);
          c.lineTo(900, 200);
          c.lineTo(900, 250);
          c.lineTo(700, 250);
          c.lineTo(600, 250);
          c.lineTo(600, 650);
          c.fillStyle = "#C1EEFF";
          c.fill();</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;canvas&gt;&lt;/canvas&gt;</code></pre>
  </div>
  </div>
  </p>
  <p>Here's what it looks like as a constructor function that I tried to make:</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>var canvas = document.querySelector('canvas');
  
  // returning a drawing context to a variable 'c'
  // allows you to draw 2d elements
  var c = canvas.getContext('2d');
  
    canvas.width = 1000;
    canvas.height = 700;
    canvas.style.width = 1000;
    canvas.style.height = 700;
    
  
  var points = [(350, 200), (900, 200), (900, 250), (700, 250), (600, 250), (600, 650)];
  
  function Path(startX, startY, array, color){
        c.beginPath();
        c.moveTo(startX, startY);
  
        // For however many element pairs in the array, create a lineTo statement
        for(i = 1; i &lt; array.length; i++){
          c.lineTo(array[i][0], array[i][1]);
        }
    
        c.fillStyle = color;
        c.fill();
  }
  
  var blue = new Path(350, 200, points, '#C1EEFF');</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;canvas&gt;&lt;/canvas&gt;</code></pre>
  </div>
  </div>
  </p>
  <p>It doesn't seem to be working. Does anyone know why that is? Also, what would be the best syntax for what I'm trying to do?</p>