File size: 4,915 Bytes
602a007 | 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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 | // Global state management
let globalState = {
network: {
id: "network_1",
properties: {},
layers: [
{
id: "layer_1",
type: "Input",
properties: {},
neurons: [
{
id: "neuron_1a",
type: "InputNeuron",
properties: {
bias: 0.1,
activationFunction: "relu"
}
},
{
id: "neuron_1b",
type: "InputNeuron",
properties: {
bias: 0.2,
activationFunction: "sigmoid"
}
}
]
},
{
id: "layer_2",
type: "Hidden",
properties: {},
neurons: [
{
id: "neuron_2a",
type: "HiddenNeuron",
properties: {
bias: 0.05,
activationFunction: "tanh"
}
}
]
},
{
id: "layer_3",
type: "Output",
properties: {},
neurons: [
{
id: "neuron_3a",
type: "OutputNeuron",
properties: {
bias: -0.1,
activationFunction: "softmax"
}
}
]
}
]
}
};
// Initialize Konva stage
document.addEventListener('DOMContentLoaded', function() {
const width = document.getElementById('konva-stage').offsetWidth;
const height = document.getElementById('konva-stage').offsetHeight;
const stage = new Konva.Stage({
container: 'konva-stage',
width: width,
height: height,
draggable: true
});
const layer = new Konva.Layer();
// Add some sample shapes to represent neural network components
const inputLayer = new Konva.Rect({
x: 100,
y: 100,
width: 150,
height: 80,
fill: '#0ea5e9',
stroke: '#0284c7',
strokeWidth: 2,
cornerRadius: 5
});
const hiddenLayer = new Konva.Rect({
x: 300,
y: 100,
width: 150,
height: 80,
fill: '#8b5cf6',
stroke: '#7c3aed',
strokeWidth: 2,
cornerRadius: 5
});
const outputLayer = new Konva.Rect({
x: 500,
y: 100,
width: 150,
height: 80,
fill: '#ec4899',
stroke: '#db2777',
strokeWidth: 2,
cornerRadius: 5
});
// Add neurons
const neuron1 = new Konva.Circle({
x: 175,
y: 120,
radius: 15,
fill: '#0284c7',
stroke: '#0369a1',
strokeWidth: 1
});
const neuron2 = new Konva.Circle({
x: 175,
y: 160,
radius: 15,
fill: '#0284c7',
stroke: '#0369a1',
strokeWidth: 1
});
const neuron3 = new Konva.Circle({
x: 375,
y: 140,
radius: 15,
fill: '#6d28d9',
stroke: '#5b21b6',
strokeWidth: 1
});
const neuron4 = new Konva.Circle({
x: 575,
y: 140,
radius: 15,
fill: '#be185d',
stroke: '#9d174d',
strokeWidth: 1
});
// Add connections
const connection1 = new Konva.Line({
points: [190, 120, 360, 135],
stroke: '#94a3b8',
strokeWidth: 2,
tension: 0
});
const connection2 = new Konva.Line({
points: [190, 160, 360, 145],
stroke: '#94a3b8',
strokeWidth: 2,
tension: 0
});
const connection3 = new Konva.Line({
points: [390, 140, 560, 140],
stroke: '#94a3b8',
strokeWidth: 2,
tension: 0
});
// Add labels
const inputLabel = new Konva.Text({
x: 110,
y: 110,
text: 'Input Layer',
fontSize: 14,
fill: 'white'
});
const hiddenLabel = new Konva.Text({
x: 310,
y: 110,
text: 'Hidden Layer',
fontSize: 14,
fill: 'white'
});
const outputLabel = new Konva.Text({
x: 510,
y: 110,
text: 'Output Layer',
fontSize: 14,
fill: 'white'
});
// Add all elements to layer
layer.add(connection1);
layer.add(connection2);
layer.add(connection3);
layer.add(inputLayer);
layer.add(hiddenLayer);
layer.add(outputLayer);
layer.add(neuron1);
layer.add(neuron2);
layer.add(neuron3);
layer.add(neuron4);
layer.add(inputLabel);
layer.add(hiddenLabel);
layer.add(outputLabel);
// Add layer to stage
stage.add(layer);
// Zoom functionality
stage.on('wheel', (e) => {
e.evt.preventDefault();
const oldScale = stage.scaleX();
const pointer = stage.getPointerPosition();
const mousePointTo = {
x: (pointer.x - stage.x()) / oldScale,
y: (pointer.y - stage.y()) / oldScale,
};
const newScale = e.evt.deltaY > 0 ? oldScale * 0.9 : oldScale * 1.1;
stage.scale({ x: newScale, y: newScale });
const newPos = {
x: pointer.x - mousePointTo.x * newScale,
y: pointer.y - mousePointTo.y * newScale,
};
stage.position(newPos);
});
// Console functionality
const consoleInput = document.querySelector('#console-output + div input');
const consoleOutput = document.getElementById('console-output'); |