-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.html
73 lines (64 loc) · 2.28 KB
/
graph.html
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
<html>
<body>
<style>
#canvas1_div {
text-align: center;
display: block;
margin-left: auto;
margin-right: auto;
}
</style>
<div id="canvas1_div" style="overflow-x: auto;">
<canvas id="canvas1" width="560" height="560"></canvas><br>
<input type="text" name="number" id="num"><br>
<button onclick="javascript:addPoint();">Submit</button>
<button onclick="javascript:clearArea();points.length = 0;">Clear Area</button>
</div>
<script>
let canvas1 = document.getElementById("canvas1");
let ctx1 = canvas1.getContext("2d");
let points = new Array();
function addPoint() {
clearArea();
let val = document.getElementById("num").value;
points.push(val);
console.log(points.length);
if (points.length === 1) {
ctx1.beginPath();
ctx1.arc(0, (canvas1.height * (1 - val)), 2, 0, 2 * Math.PI);
ctx1.fillStyle = "#7D9DDF";
ctx1.fill();
ctx1.strokeStyle = "#7D9DDF";
ctx1.stroke();
}
else {
for (let i = 0; i < points.length; i++) {
let x = canvas1.width / (points.length - 1) * i;
let y = (canvas1.height * (1 - points[i]));
ctx1.beginPath();
ctx1.arc(x, y, 2, 0, 2 * Math.PI);
ctx1.fillStyle = "#7D9DDF";
ctx1.fill();
ctx1.strokeStyle = "#7D9DDF";
ctx1.stroke();
}
ctx1.beginPath();
for (let i = 0; i < points.length; i++) {
let x = canvas1.width / (points.length - 1) * i;
let y = (canvas1.height * (1 - points[i]));
if (i === 0) {
ctx1.moveTo(x, y);
} else {
ctx1.lineTo(x, y);
}
}
ctx1.strokeStyle = "#7D9DDF";
ctx1.stroke();
}
}
function clearArea() {
ctx1.clearRect(0, 0, canvas1.width, canvas1.height);
}
</script>
</body>
</html>