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 CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

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.

            
              /*Neural Network Visualization
Peter Farrell
May 30, 2017
*/

var nodeList = [];
var node2List = []; //second column of hidden nodes
var inList = [];
var outList = [];
var pathList = [];

function setup(){
    createCanvas(600,600);
   //Nodes
    for(var i=0; i<4; i++){
        nodeList.push(new Node(220,100+150*i))
    }
  //Nodes2
  for(var i=0; i<4; i++){
        node2List.push(new Node2(380,100+150*i))
    }
    for(var j=1; j< 4; j++){
        inList.push(new Input(150*j+25))
    }
  for (var j = 0; j<2; j++){
        outList.push(new Output(250+150*j));
    }
  //Paths from inputs to hidden1
    for (var n=0; n<inList.length; n++){ 
        for (var h=0; h<nodeList.length; h++){ 
            pathList.push(new Path(inList[n],nodeList[h],false));
        }
    }
  //Paths between hidden nodes
  for (var n=0; n<nodeList.length; n++){ 
        for (var h=0; h<node2List.length; h++){ 
            pathList.push(new Path(nodeList[n],node2List[h],false));
        }
    }
  //Paths from hidden2 to outputs
    for (var h=0; h<node2List.length; h++){  
        for(var o=0; o<outList.length; o++){ 
            pathList.push(new Path(node2List[h],outList[o],false));
        }
    }
}
   
    
function draw(){
    background(0)
    for (var p=0; p<pathList.length; p++){
        pathList[p].update();
      }
    for (var h=0; h<nodeList.length; h++){
        nodeList[h].update();
    }
  for (var h=0; h<node2List.length; h++){
        node2List[h].update();
    }
    for (var n=0; n<inList.length; n++){ 
        inList[n].update(nodeList,node2List,outList,pathList);
    }
    for (var o=0; o<outList.length; o++){
        outList[o].update();
    }
    
    }
        
function Node(x,y){
        this.x = x;
        this.y = y;
        this.sz = 40;
        this.col = color(255);
        this.energy = 0;
        
    this.update = function(){
        
        fill(this.col);
        noStroke();
        
        ellipse(this.x,this.y,this.sz,this.sz)
    }
}

function Node2(x,y){
        this.x = x;
        this.y = y;
        this.sz = 40;
        this.col = color(255);
        this.energy = 0;
        
    this.update = function(){
        
        fill(this.col);
        noStroke();
        
        ellipse(this.x,this.y,this.sz,this.sz)
    }
}
function Input(y){
        this.col = color(237,168,93);
        this.x = 70;
        this.y = y;
        this.sz = 40;
        this.energy = 0;
        
    this.update = function(nodeList,node2List,outList,pathList){
        //this.col = color(237,168,93);
        fill(this.col);
        noStroke();
        
        ellipse(this.x,this.y,this.sz,this.sz);
        if (mouseIsPressed){
            if (dist(mouseX,mouseY,this.x,this.y) < this.sz){
                var hidden = int(random(nodeList.length));
              var middle = int(random(node2List.length));
                var out = int(random(outList.length));
                pathList.push(new Path(node2List[middle],outList[out],true));
              pathList.push(new Path(nodeList[hidden],node2List[middle],true));
                pathList.push(new Path(this,nodeList[hidden],true));
            }
            }
    }
}
                
        
function Output(y){
        //this.col = color(93,168,237);
        this.x = 530;
        this.y = y;
        this.sz = 40;
        this.energy = 0;
        this.col = color(93,168,237);//this.energy%255);
        
    this.update = function(){
        this.col = color(93,168,237);//this.energy%255);
        fill(this.col);
        noStroke();
        
        ellipse(this.x,this.y,this.sz,this.sz)
        if (mouseIsPressed){
            if (dist(mouseX,mouseY,this.x,this.y) < this.sz){
                this.energy += 10;
                this.col = color(93,168,this.energy%255);
                }
            }
    }
}
    
function Path(node1,node2,clicked){
        this.node1 = node1
        this.node2 = node2
        this.energy = random(10)
        this.clicked = clicked
        this.clickedTime = 10
        
    this.update = function(){
        stroke(255,255,0)
        if (this.clicked){
          this.energy ++;
            this.clickedTime -= 1
            if (this.clickedTime == 0){
                this.clicked = false;
            }
            strokeWeight(this.clickedTime);
        }
        else{
            strokeWeight(this.energy/5)
        }
        line(this.node1.x,this.node1.y,
             this.node2.x,this.node2.y)
        
    }
}
        
        
            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console