Spaces:
Sleeping
Sleeping
Create canvas.js
Browse files- static/canvas.js +128 -0
static/canvas.js
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Initialize Konva stage
|
| 2 |
+
const stage = new Konva.Stage({
|
| 3 |
+
container: 'container',
|
| 4 |
+
width: 800,
|
| 5 |
+
height: 600
|
| 6 |
+
});
|
| 7 |
+
|
| 8 |
+
const layer = new Konva.Layer();
|
| 9 |
+
stage.add(layer);
|
| 10 |
+
|
| 11 |
+
// Store nodes and connections
|
| 12 |
+
let nodes = [];
|
| 13 |
+
let connections = [];
|
| 14 |
+
let selectedNode = null;
|
| 15 |
+
|
| 16 |
+
// Add a new node
|
| 17 |
+
function addNode() {
|
| 18 |
+
const node = new Konva.Group({
|
| 19 |
+
x: Math.random() * (stage.width() - 100),
|
| 20 |
+
y: Math.random() * (stage.height() - 100),
|
| 21 |
+
draggable: true
|
| 22 |
+
});
|
| 23 |
+
|
| 24 |
+
// Node rectangle
|
| 25 |
+
const box = new Konva.Rect({
|
| 26 |
+
width: 100,
|
| 27 |
+
height: 50,
|
| 28 |
+
fill: '#ffeb3b',
|
| 29 |
+
stroke: 'black',
|
| 30 |
+
strokeWidth: 2,
|
| 31 |
+
cornerRadius: 5
|
| 32 |
+
});
|
| 33 |
+
|
| 34 |
+
// Node text (function name)
|
| 35 |
+
const text = new Konva.Text({
|
| 36 |
+
text: 'Function',
|
| 37 |
+
fontSize: 12,
|
| 38 |
+
fontFamily: 'Arial',
|
| 39 |
+
fill: 'black',
|
| 40 |
+
width: 100,
|
| 41 |
+
align: 'center',
|
| 42 |
+
y: 20
|
| 43 |
+
});
|
| 44 |
+
|
| 45 |
+
node.add(box);
|
| 46 |
+
node.add(text);
|
| 47 |
+
|
| 48 |
+
// Node data
|
| 49 |
+
node.data = {
|
| 50 |
+
id: nodes.length,
|
| 51 |
+
function: 'sampleFunction', // Placeholder function name
|
| 52 |
+
x: node.x(),
|
| 53 |
+
y: node.y()
|
| 54 |
+
};
|
| 55 |
+
|
| 56 |
+
// Handle node click for connections
|
| 57 |
+
node.on('click', () => {
|
| 58 |
+
if (!selectedNode) {
|
| 59 |
+
selectedNode = node;
|
| 60 |
+
} else {
|
| 61 |
+
// Create connection
|
| 62 |
+
const line = new Konva.Line({
|
| 63 |
+
points: [
|
| 64 |
+
selectedNode.x() + 50, selectedNode.y() + 25,
|
| 65 |
+
node.x() + 50, node.y() + 25
|
| 66 |
+
],
|
| 67 |
+
stroke: 'black',
|
| 68 |
+
strokeWidth: 2
|
| 69 |
+
});
|
| 70 |
+
layer.add(line);
|
| 71 |
+
connections.push({
|
| 72 |
+
from: selectedNode.data.id36
|
| 73 |
+
to: node.data.id
|
| 74 |
+
});
|
| 75 |
+
selectedNode = null;
|
| 76 |
+
saveNodes();
|
| 77 |
+
}
|
| 78 |
+
});
|
| 79 |
+
|
| 80 |
+
// Update position on drag
|
| 81 |
+
node.on('dragmove', () => {
|
| 82 |
+
node.data.x = node.x();
|
| 83 |
+
node.data.y = node.y();
|
| 84 |
+
updateConnections();
|
| 85 |
+
saveNodes();
|
| 86 |
+
});
|
| 87 |
+
|
| 88 |
+
nodes.push(node);
|
| 89 |
+
layer.add(node);
|
| 90 |
+
layer.draw();
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
// Update connection lines when nodes move
|
| 94 |
+
function updateConnections() {
|
| 95 |
+
connections.forEach((conn, index) => {
|
| 96 |
+
const fromNode = nodes.find(n => n.data.id === conn.from);
|
| 97 |
+
const toNode = nodes.find(n => n.data.id === conn.to);
|
| 98 |
+
if (fromNode && toNode) {
|
| 99 |
+
const line = layer.find('Line')[index];
|
| 100 |
+
if (line) {
|
| 101 |
+
line.points([
|
| 102 |
+
fromNode.x() + 50, fromNode.y() + 25,
|
| 103 |
+
toNode.x() + 50, toNode.y() + 25
|
| 104 |
+
]);
|
| 105 |
+
}
|
| 106 |
+
}
|
| 107 |
+
});
|
| 108 |
+
layer.draw();
|
| 109 |
+
}
|
| 110 |
+
|
| 111 |
+
// Save nodes and connections to backend
|
| 112 |
+
function saveNodes() {
|
| 113 |
+
fetch('/save_nodes', {
|
| 114 |
+
method: 'POST',
|
| 115 |
+
headers: {
|
| 116 |
+
'Content-Type': 'application/json'
|
| 117 |
+
},
|
| 118 |
+
body: JSON.stringify({
|
| 119 |
+
nodes: nodes.map(n => n.data),
|
| 120 |
+
connections: connections
|
| 121 |
+
})
|
| 122 |
+
}).then(response => response.json())
|
| 123 |
+
.then(data => console.log('Saved:', data))
|
| 124 |
+
.catch(error => console.error('Error:', error));
|
| 125 |
+
}
|
| 126 |
+
|
| 127 |
+
// Initial draw
|
| 128 |
+
layer.draw();
|