Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="svgcontainer">
  <svg
    id="targ"
    preserveaspectratio="xMidYMin slice"
    style="padding-bottom: 56.25%;"
    xmlns="http://www.w3.org/2000/svg"
    xmlns:xlink="http://www.w3.org/1999/xlink"
    viewBox="0 0 36 36"
  >
  </svg>
</div>
<px></px>
              
            
!

CSS

              
                body {padding: 0; margin: 2em;}
html {
  font-family: sans-serif;
  background-color: #19191a;
  color: #fff;
}
.svgcontainer {
  position: relative;
  font-size: 1px !important;
  line-height: 1px !important;
  margin-left: auto;
  margin-right: auto;
  max-width: 100%;
  /* overflow: hidden; */
}
.svgcontainer svg {
  width: 100%;
  padding-bottom: 50%;
  height: 1px !important;
  height: 0.1px !important;
  overflow: visible;
  box-sizing: content-box;
  fill: none;
}
svg {
  /* border: #fff 1px solid; */
}
svg path {
  stroke: #ffffff;
  stroke-miterlimit: 10;
  stroke-width: .1;
}
              
            
!

JS

              
                function random(max) {
  // return Math.floor(Math.random() * (max - min + 1)) + min;
  return Math.floor(Math.random() * max);
}
const sortArrayOfArray = (a, b) => {
  //sorts by integers properly, array[0] then array[1]
  if (a[0] < b[0]) {
    return -1;
  }
  if (a[0] > b[0]) {
    return 1;
  }

  if (a[0] == b[0]) {
    if (a[1] < b[1]) {
      return -1;
    }
    if (a[1] > b[1]) {
      return 1;
    }
    return 0;
  }
  return 0;
};
function arrayArraySearch(set, query) {
  var setX = set.slice(); // dup set array
  setX.sort(sortArrayOfArray);
  for (var i = 0; i < setX.length; i++) {
    if (setX[i][0] < query[0]) {
      // if not correct column
    } else if (
      setX[i][0] > query[0] ||
      (setX[i][0] == query[0] && setX[i][1] > query[1])
    ) {
      // if beyond column or if correct column and beyond row
      return false;
    } else if (setX[i][1] == query[1] && setX[i][0] == query[0]) {
      // found exact match
      return true;
    }
  }
  // fallback in instance column is never eached
  return false;
}

// where the magic happens
var svg = $("svg");

// define right, bottom limits
var limit = [
  parseInt(svg.attr("viewBox").split(" ")[2], 10), // x
  parseInt(svg.attr("viewBox").split(" ")[3], 10) // y
];

var D = [],
  Dall = [],
  lastvalid = [],
  newpoint,
  saveRandom,
  validDirection = ["up", "right", "down", "left"],
  validDirectionIndex,
  curatemode = 0,
  totalspaces = (limit[0] + 1) * (limit[1] + 1),
  rerolllimiter = 10,
  rerollcount = 0;
//console.log("limit[0]: " + limit[0] + "limit[1]: " + limit[1] + "totalspaces: " + totalspaces);

function snakescape() {
  // clear svg
  $("svg").html("");
  //clear d
  D = [];
  // reset VDI
  validDirectionIndex = [0, 1, 2, 3];
  // get the starting point
  newpoint = [0,0];
  newpoint = [limit[0] / 2, limit[1] / 2]; // center
  // newpoint = [random(limit[0]), random(limit[1])]; // randomstart

  // TEST CASE
  // push newpoint to valid starting point loop
  // Dall.push( newpoint );

  // Check if newpoint isn't used before

  D.push(newpoint);
  if (newpoint != undefined) {
    lastvalid = D[D.length - 1].slice();
    while (
      validDirectionIndex.length != 0 &&
      lastvalid[0] < limit[0] &&
      lastvalid[1] < limit[1] &&
      lastvalid[0] > 0 &&
      lastvalid[1] > 0
    ) {
      newpoint = lastvalid.slice();
      saveRandom = random(validDirectionIndex.length);

      // compute that new move
      if (validDirectionIndex[saveRandom] === 0) {
        newpoint[1] += 1;
      } else if (validDirectionIndex[saveRandom] === 1) {
        newpoint[0] += 1;
      } else if (validDirectionIndex[saveRandom] === 2) {
        newpoint[1] -= 1;
      } else if (validDirectionIndex[saveRandom] === 3) {
        newpoint[0] -= 1;
      } else {
        break;
      }
      
      if (
        arrayArraySearch(D, newpoint) ||
        newpoint[0] >= limit[0] ||
        newpoint[1] >= limit[1] ||
        newpoint[0] <= 0 ||
        newpoint[1] <= 0
      ) {
        validDirectionIndex.splice(saveRandom, 1);
      } else {
        D.push(newpoint);
        validDirectionIndex = [0, 1, 2, 3];
        lastvalid = D[D.length - 1].slice();
      }
    }
    
    for (var i = 0; i < D.length; i++) {
      D[i] = D[i].join(" ");
    }
    D = D.join(" ");

    var svgactual = document.getElementById("targ");
    var ns = "http://www.w3.org/2000/svg";
    var path = document.createElementNS(ns, "path");
    path.setAttribute("d", "M" + D);
    svgactual.appendChild(path);
  }
}

setInterval(snakescape, 500);

              
            
!
999px

Console