-
Notifications
You must be signed in to change notification settings - Fork 1
/
sketch.js
140 lines (119 loc) · 3.2 KB
/
sketch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
var neuronList = {};
var actionPotentialList = {};
var state = 'neuronPlacement';
var selectedNeuron = false;
var sketchOptions = {
cellSize : 30,
actionPotentialDivisor: 5,
bgColor : 50,
gridColor : 75,
controlPanelSize: 100,
canvasId: 'defaultCanvas0'
};
function setup() {
createCanvas(window.innerWidth, window.innerHeight - sketchOptions.controlPanelSize);
background(sketchOptions.bgColor);
drawGraphLines();
}
function drawGraphLines() {
var cols = _.ceil(width/sketchOptions.cellSize);
var rows = _.ceil(height/sketchOptions.cellSize);
stroke(sketchOptions.gridColor);
_.times(cols, c => {
c = c*sketchOptions.cellSize;
line(c, 0, c, height);
});
_.times(rows, r => {
r = r*sketchOptions.cellSize;
line(0, r, width, r);
});
}
// For non p5.js users, this methods gets called over and over again
function draw() {
switch(state) {
case 'live':
clearScreen(false);
Neuron.displayAllConnections();
ActionPotential.displayAllActionPotentials();
Neuron.displayAllNeurons();
Neuron.allNeuronSpecificRoutines();
break;
default:
clearScreen();
mouseHover();
ActionPotential.displayAllActionPotentials();
Neuron.displayAllNeurons();
Neuron.displayAllConnections();
}
}
// Was using default param but not enough browser support
function clearScreen(keepGrid) {
keepGrid = keepGrid === undefined;
fill(sketchOptions.bgColor);
noStroke();
rect(0, 0, width, height);
if (keepGrid) {
drawGraphLines();
}
}
function mouseClicked(event) {
if (event.target.id !== sketchOptions.canvasId) {
return;
}
var x = mouseX - (mouseX % sketchOptions.cellSize);
var y = mouseY - (mouseY % sketchOptions.cellSize);
switch(state) {
case 'neuronPlacement':
placeNeurons(x, y);
break;
case 'connectionPlacement':
connectNeurons(x, y);
break;
case 'live':
live(x, y);
break;
}
}
function mouseHover() {
if (mouseY < 0) {
return;
}
var x = mouseX - (mouseX % sketchOptions.cellSize);
var y = mouseY - (mouseY % sketchOptions.cellSize);
// Add grey hover state over cells that don't have a neuron
if (!neuronList[`${x} ${y}`] && state === 'neuronPlacement') {
fill(sketchOptions.gridColor);
rect(x, y, sketchOptions.cellSize, sketchOptions.cellSize);
}
if (selectedNeuron) {
var half = sketchOptions.cellSize/2;
line(selectedNeuron.x + half, selectedNeuron.y + half, x+half, y+half);
}
}
// Create neuron on spot if none exists, otherwise delete neuron at that spot
function placeNeurons(x, y) {
if (!neuronList[`${x} ${y}`]) {
Neuron.createNeuron(x, y);
} else {
Neuron.deleteNeuron(x, y);
}
}
// Set selected, else check if selected is connecting to another neuron to add connections
function connectNeurons(x, y) {
if (!selectedNeuron) {
Neuron.selectNeuron(x, y);
} else if (neuronList[`${x} ${y}`]) {
Neuron.addConnection(x, y);
} else {
Neuron.unSelectNeuron();
}
}
function live(x, y) {
if (neuronList[`${x} ${y}`]) {
neuronList[`${x} ${y}`].addCharge(1);
}
}
function windowResized() {
resizeCanvas(window.innerWidth, window.innerHeight - sketchOptions.controlPanelSize);
clearScreen();
}