-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.html
221 lines (192 loc) · 5.78 KB
/
index.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
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
210
211
212
213
214
215
216
217
218
219
220
221
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="description" content="魔方找茬,在线挑战《最强大脑》项目">
<meta name="keywords" content="Cube Wall,魔方找茬,魔方墙,最强大脑,郑才千">
<title>Cube Wall</title>
<style>
:root {
--blue-grey: #607d8b;
}
html, body {
height: 100vh;
}
body {
margin: 0;
display: flex;
flex-direction: column;
}
header {
display: flex;
align-items: center;
justify-content: space-between;
padding: 0px 40px 0px 40px;
background-color: var(--blue-grey);
color: white;
box-shadow: 0 2px 4px -1px rgba(0,0,0,.2), 0 4px 5px rgba(0,0,0,.14), 0 1px 10px rgba(0,0,0,.12);
}
header #dimension {
box-shadow: none;
font-size: 16px;
flex: 1;
margin: 0;
white-space: nowrap;
text-overflow: ellipsis;
overflow: hidden;
min-width: 0;
height: 30px;
background-color: transparent;
border-style: none;
border-bottom: 1px solid white;
color: white;
}
header #build, #help {
background-color: transparent;
color: white;
padding: 8px 16px 8px 16px;
font-size: 1em;
border-style: none;
}
header #build:hover, #help:hover {
background-color: lightgrey;
color: black;
border-style: none;
}
main {
flex: 1;
display: flex;
margin: 40px;
}
#left, #right {
box-shadow: 0 1px 5px rgba(0,0,0,.2), 0 2px 2px rgba(0,0,0,.14), 0 3px 1px -2px rgba(0,0,0,.12);
}
#gutter {
width: 40px;
}
</style>
</head>
<body>
<header>
<h2 class="title">Cube Wall</h2>
<div class="input">
<label for="dimension">Dimension(维度):</label>
<input id="dimension" type="number" min="2" max="200" value="10"/>
<button id="build">Build(生成)</button>
<button id="help">Help(帮助)</button>
</div>
</header>
<main id="main">
<canvas id="left">left</canvas>
<div id="gutter"></div>
<canvas id="right">right</canvas>
</main>
<script>
class Wall {
constructor() {
this.main = document.getElementById('main')
this.leftCanvas = document.getElementById('left')
this.leftCtx = this.leftCanvas.getContext('2d')
this.gutter = document.getElementById('gutter')
this.rightCanvas = document.getElementById('right')
this.rightCtx = this.rightCanvas.getContext('2d')
this.canvasWidth = (main.clientWidth - gutter.clientWidth) / 2
this.cellWidth = 0
this.colors = ['white', 'red', 'blue', 'yellow', 'green', 'orange']
this.leftCanvas.width = this.canvasWidth
this.leftCanvas.height = this.canvasWidth
this.leftCanvas.style.width = this.canvasWidth + 'px'
this.leftCanvas.style.height = this.canvasWidth + 'px';
this.rightCanvas.width = this.canvasWidth
this.rightCanvas.height = this.canvasWidth
this.rightCanvas.style.width = this.canvasWidth + 'px'
this.rightCanvas.style.height = this.canvasWidth + 'px';
}
}
Wall.prototype.generateMatrix = function() {
let n = this.dimension
let matrix = new Array(n)
for (let x = 0; x < n; x++) {
matrix[x] = new Array(n)
for (let y = 0; y < n; y++) {
let color = this.colors[parseInt(Math.random() * 6)]
matrix[x][y] = color
}
}
return matrix
}
Wall.prototype.drawPoint = function(ctx, x, y, color) {
let w = this.cellWidth
ctx.fillStyle = color
ctx.fillRect(x * w, y * w, w, w);
ctx.lineWidth = 0.5
ctx.strokeRect(x * w, y * w, w, w);
}
Wall.prototype.drawMatrix = function(ctx, matrix) {
let n = this.dimension
for (let x = 0; x < n; x++) {
for (let y = 0; y < n; y++) {
this.drawPoint(ctx, x, y, matrix[x][y])
}
}
ctx.strokeRect(0, 0, this.canvasWidth, this.canvasWidth);
}
Wall.prototype.randomPoint = function() {
return {
x: parseInt(Math.random() * this.dimension),
y: parseInt(Math.random() * this.dimension)
}
}
Wall.prototype.randomColor = function(oldColor) {
let color = oldColor
do {
let colorIndex = parseInt(Math.random() * this.colors.length)
color = this.colors[colorIndex]
} while (color === oldColor)
return color
}
Wall.prototype.build = function() {
if (this.timerId) {
clearInterval(this.timerId)
this.timerId = null
}
this.dimension = parseInt(document.getElementById('dimension').value)
this.cellWidth = this.canvasWidth / this.dimension
let m = this.generateMatrix()
this.drawMatrix(this.leftCtx, m)
this.matrix = m
this.point = this.randomPoint()
let {x, y} = this.point
let color = this.randomColor(m[x][y])
m[x][y] = color
this.drawMatrix(this.rightCtx, m)
}
Wall.prototype.help = function() {
let {x, y} = this.point
let color = this.matrix[x][y]
if (this.timerId) {
clearInterval(this.timerId)
this.timerId = null
this.blink = false
this.drawPoint(this.rightCtx, x, y, color)
return
}
this.timerId = setInterval(() => {
if (this.blink) {
this.drawPoint(this.rightCtx, x, y, 'black')
} else {
this.drawPoint(this.rightCtx, x, y, color)
}
this.blink = !this.blink
}, 300)
this.drawPoint(this.rightCtx, x, y, 'black')
}
let wall = new Wall()
let build = document.getElementById('build')
build.onclick = wall.build.bind(wall);
let help = document.getElementById('help')
help.onclick = wall.help.bind(wall);
wall.build()
</script>
</body>
</html>