-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanimationAndHelpers.js
209 lines (189 loc) · 6.36 KB
/
animationAndHelpers.js
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
// Creating canvas and getting values from input fields form HTML
function createCanvas() {
n = Number(document.getElementById("numberOfPoints").value);
r = Number(document.getElementById("radius").value);
animationSpeed = Number(document.getElementById("animationSpeed").value);
canvas = document.getElementById("canvas");
context = canvas.getContext("2d");
disablePreviousNextFinish(true);
disablePointsButton(false);
setStartStopContinue("START");
}
// Calculate the output center and start animation
function start() {
animationStarted = true;
solutionLabel(true);
createPointsAndCircles();
firstDrawPointsAndCircles();
circleIntersections();
findCenter();
startAnimation();
}
function stop() {
clearInterval(animationInterval);
}
function continueButton() {
startAnimation();
}
function previousStep() {
if (animationIndex > 1) {
animationIndex--;
clearContext();
for (let i = 0; i < animationIndex; i++) {
let animationObject = animationList[i];
for (let j = 0; j < animationObject["function"].length; j++) {
window[animationObject["function"][j]].apply(undefined, animationObject["parameters"][j]);
}
}
}
}
function nextStep() {
let animationObject = animationList[animationIndex];
for (let i = 0; i < animationObject["function"].length; i++) {
window[animationObject["function"][i]].apply(undefined, animationObject["parameters"][i]);
}
animationIndex++;
if (animationIndex >= maxAnimationSteps) {
finish();
}
}
// Gets called when requested to move to the end of animation
function finishButton() {
clearInterval(animationInterval);
clearContext();
drawCircle(new Circle(resultCenter.x, resultCenter.y, r), YELLOW);
drawPoint(resultCenter, RED);
drawPoints();
finish();
}
// Gets called when animation is finished
function finish() {
animationStarted = false;
clearInterval(animationInterval);
solutionLabel(false);
setStartStopContinue("START");
disablePreviousNextFinish(true);
// clear input files
document.getElementById("file").value = "";
}
function clearContext() {
context.clearRect(0, 0, canvas.width, canvas.height);
}
// Function that resets everything for new input values
function reset() {
clearContext();
points = [];
circles = [];
animationIndex = 0;
animationStarted = false;
inputFile = false;
animationList = [];
intervals = [];
solution = [];
clearInterval(animationInterval);
createCanvas();
solutionLabel(true);
// clear input files
document.getElementById("file").value = "";
}
function addPointsButton() {
var pointButton = document.getElementById("pointButton");
if (pointButton.innerHTML == "Add points") {
addingPointsMode = true;
pointButton.innerHTML = "Finish adding";
} else {
addingPointsMode = false;
pointButton.innerHTML = "Add points";
}
disableAnimation();
}
// Helper function to handle buttons -> START / STOP / CONTINUE
function handleSSCButton() {
var button = document.getElementById('startStopContinueButton');
if (button.innerHTML == "START") {
reset();
start();
disablePreviousNextFinish(true);
disablePointsButton(true);
setStartStopContinue("STOP");
} else if (button.innerHTML == "STOP") {
stop();
if (animationStarted) {
disablePreviousNextFinish(false);
setStartStopContinue("CONTINUE");
} else {
disablePreviousNextFinish(true);
setStartStopContinue("START");
}
} else {
continueButton();
disablePreviousNextFinish(true);
setStartStopContinue("STOP");
}
}
// Helper funciton to set button to -> START / STOP / CONTINUE
function setStartStopContinue(choice) {
var button = document.getElementById('startStopContinueButton');
if (choice == "START") {
button.innerHTML = "START";
button.style="background-color: green; color: white; height: 50px; width: 120px; text-align: center";
} else if (choice == "STOP"){
button.innerHTML = "STOP";
button.style="background-color: red; color: white; height: 50px; width: 120px; text-align: center";
} else {
button.innerHTML = "CONTINUE";
button.style="background-color: orange; color: white; height: 50px; width: 120px; text-align: center";
}
}
// Helper funciton to set PREVIOUS, NEXT STEP and FINISH buttons to be disabled or not
function disablePreviousNextFinish(toDisable) {
var previousButton = document.getElementById("previousButton");
var nextButton = document.getElementById("nextButton");
var finishButton = document.getElementById("finishButton");
if (toDisable) {
previousButton.disabled = true;
nextButton.disabled = true;
finishButton.disabled = true;
} else {
previousButton.disabled = false;
nextButton.disabled = false;
finishButton.disabled = false;
}
}
// Helper funciton to set button for adding points to be disabled or not
function disablePointsButton(toDisable) {
var pointButton = document.getElementById("pointButton");
if (toDisable) {
pointButton.disabled = true;
} else {
pointButton.disabled = false;
}
}
// Helper function to disable start of animation if user is painting points
function disableAnimation() {
var start = document.getElementById('startStopContinueButton');
var resetButton = document.getElementById('resetButton');
if (addingPointsMode) {
start.disabled = true;
resetButton.disabled = true;
} else {
start.disabled = false;
resetButton.disabled = false;
}
}
// Helper function that shows / hides solution label
function solutionLabel(hide) {
let label = document.getElementById("solutionLabel");
if (hide) {
label.style.display = 'none';
} else if (resultCenter){
label.style.display = 'block';
label.innerHTML = "Solution: " + resultCenter.toString();
}
}
// Returns x and y coordinates of mouse clicked event
function currentMousePosition(event) {
let border = canvas.getBoundingClientRect();
return {"x" : (event.clientX - border.left), "y" : (event.clientY - border.top)};
}
document.getElementById("canvas").addEventListener("click", draw);