-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
138 lines (111 loc) · 4.43 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
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
class WidgetFactory {
getRangeWidget(id, labelText, min = 0, val = 50, max = 100, step = 1) {
let labelElement = document.createElement('label')
labelElement.innerHTML = labelText
let inputElement = document.createElement('input')
inputElement.setAttribute('type',"range")
inputElement.setAttribute('id', id)
// inputElement.setAttribute('name',"username")
inputElement.setAttribute('value', val)
inputElement.setAttribute('min', min)
inputElement.setAttribute('max', max)
inputElement.setAttribute('step', step)
labelElement.appendChild(inputElement)
return labelElement;
}
getCanvas(width, height) {
let element = document.createElement('canvas')
element.width = width
element.height = height
return element
}
getButton(id, labelText) {
let element = document.createElement('input')
element.setAttribute('type',"button")
element.setAttribute('id', id)
element.setAttribute('value',labelText)
return element
}
}
class MazeDrawer {
constructor(element) {
this.target = element
}
clear() {
const context = this.target.getContext('2d');
context.clearRect(0, 0, this.target.width, this.target.height);
}
draw(maze, size = 16, spacing = 2) {
console.log("MazeDrawer constructor")
this.clear()
const cellSize = size
const cellSpacing = spacing
var ctx = this.target.getContext('2d')
const background_color = '#07F'
const foreground_color = '#F70'
for (var x = 0; x< maze.width; x++) {
for (var y = 0; y< maze.height; y++) {
if ( maze.cells[x + (y * maze.width)] == 1)
{
this.target.fillStyle = foreground_color
ctx.fillRect(x * cellSize + (x + 1) * cellSpacing,
y * cellSize + (y + 1) * cellSpacing,
cellSize, cellSize)
}
}
}
}
}
class App {
constructor() {
console.log("App constructor")
// this.gui = new GUI()
this.widgetFactory = new WidgetFactory()
this.maze = new BlockMaze()
this.size = 32
this.space = 2
this.width = 21
this.height = 21
let widthWidget = this.widgetFactory.getRangeWidget('width', 'width', 5, this.width, 505, 2)
widthWidget.onchange = (evt) => {
this.width = evt.target.value
this.maze.initialise(this.width, this.height)
this.maze.generate()
this.drawer.draw(this.maze, this.size, this.space)
};
document.body.appendChild(widthWidget)
let heightWidget = this.widgetFactory.getRangeWidget('height', 'height', 5, this.height, 505, 2)
heightWidget.onchange = (evt) => {
this.height = evt.target.value
this.maze.initialise(this.width, this.height)
this.maze.generate()
this.drawer.draw(this.maze, this.size, this.space)
};
document.body.appendChild(heightWidget)
let sizeWidget = this.widgetFactory.getRangeWidget('size', 'size', 0, this.size, 100)
sizeWidget.onchange = (evt) => {
this.size = evt.target.value
this.drawer.draw(this.maze, this.size, this.space)
};
document.body.appendChild(sizeWidget)
let spaceWidget = this.widgetFactory.getRangeWidget('space', 'space', 0, this.space, 100)
spaceWidget.onchange = (evt) => {
this.space = evt.target.value
this.drawer.draw(this.maze, this.size, this.space)
};
document.body.appendChild(spaceWidget)
let generateBtnWidget = this.widgetFactory.getButton('generate', 'generate')
generateBtnWidget.onclick = (evt) => {
this.maze.initialise(this.width, this.height)
this.maze.generate()
this.drawer.draw(this.maze, this.size, this.space)
};
document.body.appendChild(generateBtnWidget)
this.canvas = this.widgetFactory.getCanvas(window.innerWidth, window.innerHeight*0.9)
document.body.appendChild(this.canvas)
this.maze.initialise(this.width, this.height)
this.maze.generate()
this.drawer = new MazeDrawer(this.canvas)
this.drawer.draw(this.maze, this.size, this.space)
}
}