cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

            
              .buttons
  i#info.fas.fa-question-circle
  i#download.fas.fa-download
  i#clear.fas.fa-sync-alt
  i#new.fas.fa-plus-circle
canvas#canvas
#popup
  p "The Collatz conjecture is defined as follows: start with any positive integer n.
    br
    | If the term is even, divide it by 2.
    br
    | Otherwise, multiply by 3 and add 1.
    br
    | The conjecture is that no matter what value of n, the sequence will always reach 1."
    br
    a(href="https://en.wikipedia.org/wiki/Collatz_conjecture") Learn more !
  p Move the numbers around to reoganize the graph
  p Built using 
    a(href="http://getspringy.com/" target="_blank") springy.js 
    | and made with 💗 by 
    a(href="https://codepen.io/ninivert" target="_blank") ninivert
            
          
!
            
              // CSS isn't the best, but hey it works

@import url('https://fonts.googleapis.com/css?family=Roboto');

html,
body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: #ffdd59;
}
#canvas {
  display: block;
  position: absolute;
  top: 0;
  left: 0;
  background: white;
  transition: all .4s;
  transform-origin: 100% 50%;
  &.animated {
    transform: scale(.6);
    box-shadow: 0 .5em 2em rgba(black, .6);
  }
  z-index: 1;
}
.buttons {
  position: absolute;
  top: 0;
  left: 0;
  i {
    padding: .5em;
    box-sizing: border-box;
    color: #1e272e;
    font-size: 1.5em;
    cursor: pointer;
  }
  z-index: 2;
}
#popup {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  width: 40vw;
  padding: 1em;
  box-sizing: border-box;
  z-index: 0;
  p {
    color: #1e272e;
    font-family: 'Roboto', Arial;
  }
}
            
          
!
            
              const dom = {
  info: document.getElementById('info'),
  popup: document.getElementById('popup'),
  canvas: document.getElementById('canvas'),
  download: document.getElementById('download'),
  clear: document.getElementById('clear'),
  new: document.getElementById('new')
};

/**
 * GRAPH STUFF
 */

// Canvas stuff

dom.canvas.width = window.innerWidth;
dom.canvas.height = window.innerHeight;
window.onresize = () => {
  dom.canvas.width = window.innerWidth;
  dom.canvas.height = window.innerHeight;
}

// Graph and variable init

let graph = new Springy.Graph();
let numbers = [], nodes = [];
$("#canvas").springy({ graph: graph });

function addBranches(N, min, max) {
  for (let i=0; i<N; i++) {
    // Find valid number to start with
    n = 10;
    let j;
    for (j=0; j<100 && numbers.indexOf(n) !== -1; j++) {
      n = Math.floor(Math.random() * (max-min) + min);
    }
    if (j < 100) addBranch(n);
  }
}

addBranches(10, 2, 20)

// Add new branch 

function addBranch(n) {
  /**
    create starting node that DOES NOT EXIST
    do Collatz
    if new node exists, connect to it and break
    else create that new node and connect, continue in the loop
    */
  
  // Node already exists in the graph
  if (numbers.indexOf(n) !== -1) return false;
  
  // Create the node
  currNode = graph.newNode({ label: n.toString() });
  numbers.push(n);
  nodes.push(currNode);

  while (n !== 1) {
    // Collatz conjecture
    if (n % 2 === 0) {
      n = n / 2;
    } else {
      n = n * 3 + 1;
    }
    // Connect to next step
    nextNodeIndex = numbers.indexOf(n);
    if (nextNodeIndex === -1) {
      // Next node does not exist
      // Create a new node
      nextNode = graph.newNode({ label: n.toString() });
      // Connect to it
      graph.newEdge(currNode, nextNode);
      // Recursion
      currNode = nextNode;
      numbers.push(n);
      nodes.push(currNode);
    } else {
      // Node exists
      graph.newEdge(currNode, nodes[nextNodeIndex]);
      break;
    }
  }
}

// Clear the nodes

function clear() {
  for (node of nodes) {
    graph.removeNode(node);
  }
  numbers = [];
  nodes = [];
}

/**
 * UI STUFF
 */
// I don't know jQuery, and even though I imported it because I needed it, I'm not going to use it to look smart :P

// Question/Info

dom.info.addEventListener('click', function(ev) {
  canvas.classList.toggle('animated');
});

// Download (that needs fixing)

dom.download.addEventListener('click', function() {
  // Creating new canvas to display a white background
  let c = document.createElement('canvas'),
      ctx = c.getContext('2d');
  c.width = dom.canvas.width;
  c.height = dom.canvas.height;
  
  ctx.fillStyle = '#fff';
  ctx.fillRect(0, 0, c.width, c.height);
  ctx.drawImage(dom.canvas, 0, 0);
  
  // Download new canvas
  let link = document.createElement('a');
  link.download = `Collatz conjecture visualization.jpg`;
  link.href = c.toDataURL('image/png');
  // Need to add the link to the body for Firefox
  document.body.appendChild(link);
  link.setAttribute("type", "hidden");
  link.click();
  document.body.removeChild(link);
});

dom.clear.addEventListener('click', function() {
  clear();
  addBranches(10, 2, 20);
});

dom.new.addEventListener('click', function() {
  let n = prompt('Enter n: ');
  n = parseInt(n);
  n = Math.max(2, n);
  if (isNaN(n)) return false;
  addBranches(1, n, n);
})
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console