-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathballs.html
160 lines (142 loc) · 5.59 KB
/
balls.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
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
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript" src="dat.gui.js"></script>
<meta charset="utf-8" />
<title>Canvas</title>
</head>
<body>
<canvas id="canvas">
Your Browser does not support the canvas tag
</canvas>
<script>
$(function() {
var startGame = function() {
console.log("SIMULATION START")
$.ajax({
url: "/simulation/start",
type: 'POST',
dataType: 'json',
data: JSON.stringify(config),
contentType: 'application/json; charset=utf-8',
success: function() {
console.log("Started !!")
}
});
};
var stopGame = function(event) {
console.log("SIMULATION STOP")
$.get("/simulation/stop");
};
var Config = function() {
this.BallCount = 10;
this.canvasHeight = 900;
this.canvasWidth = 900;
this.maxRadius = 1;
this.minRadius = 0.1;
this.maxVelocity = 10;
this.minVelocity = 0.5;
this.maxMass = 5;
this.minMass = 1;
this.frameRate = 30;
this.searchAreaFactor = 3;
this.start = startGame;
this.stop = stopGame;
};
var initControls = function() {
var config = new Config();
var gui = new dat.GUI();
gui.add(config, 'start');
gui.add(config, 'stop');
gui.add(config, 'BallCount', 2, 1000).step(1);
gui.add(config, 'frameRate', 1, 100).step(1);
gui.add(config, 'searchAreaFactor', 1, 10).step(1);
gui.add(config, 'canvasHeight', 10, 1000).step(100);
gui.add(config, 'canvasWidth', 10, 1000).step(100);
gui.add(config, 'maxRadius', 0.01, 10).step(0.1);
gui.add(config, 'minRadius', 0.01, 10).step(0.1);
gui.add(config, 'maxVelocity', 0, 10).step(0.1);
gui.add(config, 'minVelocity', 0, 10).step(0.1);
gui.add(config, 'maxMass', 1, 1000).step(0.1);
gui.add(config, 'minMass', 1, 1000).step(0.1);
return config;
};
function initialiseCanvas(width, height) {
//find the canvas element using its id attribute.
canvas = document.getElementById('canvas');
//once canvas is created, create the simulation passing the width and height of canvas
canvas.width = width;
canvas.height = height;
if (!canvas) {
alert('Error: cannot find the canvas element!');
return;
}
if (!canvas.getContext) {
alert('Error: no canvas.getContent!');
return;
}
console.log("CANVAS INITIALIZED");
return canvas;
}
function connectToWs() {
if (window["WebSocket"]) {
conn = new WebSocket("ws://localhost:8080/ws");
conn.onclose = function(evt) {
console.log("connection closed");
}
conn.onmessage = function(evt) {
console.log("Msg received : " + evt.data);
var balls = JSON.parse(evt.data)
if (balls.length > 1)
renderer.draw(context, balls);
}
return conn;
} else {
console.log("Your browser does not support WebSockets");
}
}
var Renderer = (function() {
var canvasColour;
function Renderer(inCanvasColour) {
canvasColour = inCanvasColour;
};
Renderer.prototype.draw = function(context, ballArray) {
//console.log(ballArray);
// draw Canvas Background.
drawCanvasBackground(context);
// draw Balls.
drawBalls(context, ballArray);
}
function drawCanvasBackground(context) {
context.beginPath();
context.fillStyle = canvasColour;
context.fillRect(0, 0, canvas.width, canvas.height);
}
function drawBalls(context, ballArray) {
for (var i = 0; i < ballArray.length; i++) {
context.beginPath();
// draw ball using ball objects data.
context.arc(ballArray[i][0], ballArray[i][1], ballArray[i][2], 0, Math.PI * 2, false);
context.strokeStyle = ballArray[i][3];
context.stroke();
context.fillStyle = ballArray[i][3];
context.fill();
context.closePath();
}
}
return Renderer;
})();
var config = initControls();
var canvas = initialiseCanvas(config.canvasWidth, config.canvasHeight);
var context = canvas.getContext('2d');
if (!context) {
alert('Error: failed to getContent');
return;
}
var renderer = new Renderer('#fff'); // takes colour for canvas.
var conn = connectToWs();
});
</script>
</body>
</html>