-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
79 lines (62 loc) · 2.5 KB
/
app.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
import Conway from './conway.js';
import NODE from './node.js';
export default class App{
constructor(){
// the target div which will be used to render the game
const div_app = document.getElementById('app');
// finding the total number of rows and columns to be rendered
const ROW = Math.ceil(div_app.clientHeight / NODE.height);
const COL = Math.ceil(div_app.clientWidth / NODE.width);
// setting the properties in css. see style.css for clarity
div_app.style.setProperty('--app-rows', ROW);
div_app.style.setProperty('--app-cols', COL);
// saving instance variables for later
this.target = div_app;
this.ROW = ROW;
this.COL = COL;
// setup the Conway board
this.app = new Conway(this.target, this.ROW, this.COL);
// setup click listeners for the buttons
const start_stop_btn = document.getElementById('start-pause-btn');
start_stop_btn.addEventListener('click', this.start_stop.bind(this));
const clear_btn = document.getElementById('clear-btn');
clear_btn.addEventListener('click', this.clear.bind(this));
const random_btn = document.getElementById('random-btn');
random_btn.addEventListener('click', this.random.bind(this));
const step_btn = document.getElementById('step-btn');
step_btn.addEventListener('click', this.step.bind(this));
// setup change listener for drag bar for iteration speed
const speed_slider = document.getElementById('speed-input');
speed_slider.addEventListener('change', this.change_speed.bind(this));
}
start_stop(){
if(this.app.isRunning){
// stop the app from running
this.app.stop();
}else{
// start the app
this.app.start();
}
}
clear(){
// we will refresh the app
this.app.clear();
// set the start-stop-refresh button to start state
document.getElementById('start-pause-btn').innerText = "Start";
}
random(){
// generate a random state for the app
this.app.random();
// set the start-stop-refresh button to start stats
document.getElementById('start-pause-btn').innerText = "Start";
}
step(){
// take the next step
this.app.step(true);
}
change_speed(e){
// change the iterations speed
const speed = e.target.value;
this.app.step_duration = Number(speed);
}
}