A Pen by Dave Campion

Thumbnail
This awesome code was written by gasolinewaltz, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright gasolinewaltz ©
  • HTML
  • JavaScript
<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  <title>A Pen by  Dave Campion</title>
  
  
  
  
  
</head>

<body>

  
  
  

    <script  src="js/index.js"></script>




</body>

</html>

/*Downloaded from https://www.codeseek.co/gasolinewaltz/a-pen-by-dave-campion-zpPYOO */
let matrix = [
  
  0, 1, 2, 3, 4, 
  5, 6, 7, 8, 9,
  10, 11, 12, 13, 14,
  15, 16, 17, 18, 19,
  20, 21, 22, 23, 24, 
  
];

let spiralOut = function(matrix, width){
  let dx = 0;
  let dy = 0;
  
  let segmentLength = 1;
  
  let x = Math.floor(width/2);
  let y = Math.floor(width/2);
  
  let segmentPassed = 0;
  
  for(let i = 0; i < matrix.length; i++){
    x += dx;
    y += dy;
    segmentPassed++;
    let idx = y * width + x;
    console.log( matrix[idx]);
    if(segmentPassed == segmentLength){
      segmentPassed = 0;
      let buffer = dx;
      dx = -dy;
      dy = buffer;
      if(dx == 0 && dy == 0){
        dx = 1;
      }else if(dy == 0){
        segmentLength++;
      }
    }
  }
  
}
console.clear();
spiralOut(matrix, 5);

Comments