-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphsVisualiser.js
More file actions
75 lines (50 loc) · 1.8 KB
/
GraphsVisualiser.js
File metadata and controls
75 lines (50 loc) · 1.8 KB
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
import React from "react";
export default class GraphsVisualiser extends React.Component {
constructor(props) {
super(props);
this.state = {nodes: [], mouseIsPressed: false, mouseX: 0, mouseY: 0};
this.nodesRef = React.createRef();
}
componentDidMount() {
this.drawNodes();
}
drawNodes() {
const canvas = this.nodesRef.current;
const ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = 'lightgrey';
ctx.strokeStyle = 'black';
for (let i = 0; i < this.state.nodes.length; i++) {
ctx.beginPath();
ctx.arc(this.state.nodes[i].x, this.state.nodes[i].y, 20, 0, 2 * Math.PI);
ctx.fill();
ctx.lineWidth = 2;
ctx.stroke();
ctx.closePath();
}
ctx.closePath();
}
addNode = (event) => {
let newX = event.clientX - event.target.offsetLeft;
let newY = event.clientY - event.target.offsetTop;
const nodes2 = this.state.nodes
nodes2.push({x: newX, y: newY});
this.setState({nodes: nodes2});
this.drawNodes();
};
clearNodes = () => {
const canvas = this.nodesRef.current;
const ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, canvas.width, canvas.height);
this.setState({nodes: []});
}
render() {
return (
<div className="graphDiv">
<canvas className="graphCanv" style={{backgroundColor: "whitesmoke"}}
ref={this.nodesRef} width={window.innerWidth} height={window.innerHeight - 200}
onClick={this.addNode}/>
<button onClick={this.clearNodes}>Clear Nodes</button>
</div>);
}
}