-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
290 lines (231 loc) · 6.61 KB
/
script.js
File metadata and controls
290 lines (231 loc) · 6.61 KB
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
const canvas = document.querySelector('#canvas')
const ctx = canvas.getContext('2d')
const gameSize = document.querySelector('.game-size')
const x3 = document.querySelector('#x3')
const x4 = document.querySelector('#x4')
const x5 = document.querySelector('#x5')
const x8 = document.querySelector('#x8')
const resizeButton = document.querySelector('#resize')
const uploadButton = document.querySelector('#uploader')
const resetButton = document.querySelector('#reset')
const example = new Image(400, 400)
const audio = new Audio('./tick.mp3')
const defaultScale = 400
const maxScale = 800
const game = {
size: 3,
image: example,
puzzle: Math.floor(canvas.width / 3),
board: [],
resized: false,
win: true,
color: ['#8e44ad', '#9b59b6']
}
example.onload = () => reset()
example.src = './example.png'
audio.volume = 0.5
const draw = () => {
ctx.clearRect(0, 0, canvas.width, canvas.height)
ctx.strokeStyle = "#FFFFFF"
for (const cl in game.board) {
for (const rw in game.board[cl]) {
const square = game.board[cl][rw]
const x = rw * game.puzzle
const y = cl * game.puzzle
if (square.cx > 0 || square.cy > 0) {
ctx.drawImage(game.image, square.cx, square.cy, game.puzzle, game.puzzle, x, y, game.puzzle, game.puzzle)
}
ctx.strokeRect(x, y, game.puzzle, game.puzzle)
}
}
}
const movePuzzle = event => {
if (game.win) {
return
}
const position = getClickedPosition(event)
const clicked = getClickedSquare(position)
if (clicked.square?.empty) {
return
}
switch (true) {
case isEmpty(clicked.column, clicked.row -1): {
swapInBoard(clicked, { row: -1 })
break
}
case (isEmpty(clicked.column -1, clicked.row)): {
swapInBoard(clicked, { column: -1 })
break
}
case (isEmpty(clicked.column, clicked.row +1)): {
swapInBoard(clicked, { row: 1 })
break
}
case (isEmpty(clicked.column +1, clicked.row)): {
swapInBoard(clicked, { column: 1 })
break
}
default: return
}
audio.play()
draw()
win()
}
const win = () => {
for (const cl in game.board) {
for (const rw in game.board[cl]) {
const square = game.board[cl][rw]
const cx = square.cx !== rw * game.puzzle
const cy = square.cy !== cl * game.puzzle
if (cx || cy) {
return
}
}
}
game.win = true
ctx.textBaseline = 'middle'
ctx.textAlign = 'center'
ctx.lineWidth = 8
ctx.font = 'bold 45px Starborn'
ctx.strokeStyle = '#FFFFFF'
ctx.fillStyle = game.color[1]
ctx.drawImage(game.image, 0, 0, canvas.width, canvas.height, 0, 0, canvas.width, canvas.height)
ctx.strokeText('YOU WON!', canvas.width/2, canvas.height/2)
ctx.fillText('YOU WON!', canvas.width/2, canvas.height/2)
}
const isEmpty = (column, row) => {
const square = game.board[column]?.[row]
return square && square.empty
}
const swapInBoard = (square, changes) => {
const column = square.column
const row = square.row
const tmp = game.board[column][row]
if (changes.column) {
const cl = column + changes.column
game.board[column][row] = game.board[cl][row]
game.board[cl][row] = tmp
}
if (changes.row) {
const rw = row + changes.row
game.board[column][row] = game.board[column][rw]
game.board[column][rw] = tmp
}
}
const shuffle = array => {
for (let i = (array.length - 1); i > 0; i--) {
const rn = Math.floor(Math.random() * i)
const tmp = array[i]
array[i] = array[rn]
array[rn] = tmp
}
}
const genBoard = () => {
game.board.length = 0
const tmp = []
for (let column = 0; column < game.size; column++) {
for (let row = 0; row < game.size; row++) {
if (column || row) {
const cy = column * game.puzzle
const cx = row * game.puzzle
const position = tmp.length + 2
tmp.push({ position, cx, cy })
}
}
}
shuffle(tmp)
tmp.unshift({ position: 1, cx: 0, cy: 0, empty: true })
for (let i = 0; i < game.size; i++) {
game.board[i] = tmp.slice(i * game.size, ((i + 1) * game.size))
}
}
const getClickedPosition = window => {
const canvasRect = canvas.getBoundingClientRect()
const x = Math.floor(window.clientX - canvasRect.left)
const y = Math.floor(window.clientY - canvasRect.top)
return { x, y }
}
const getClickedSquare = position => {
for (const column in game.board) {
for (const row in game.board[column]) {
const cx = row * game.puzzle
const cy = column * game.puzzle
const isX = position.x >= cx && position.x <= cx + game.puzzle
const isY = position.y >= cy && position.y <= cy + game.puzzle
const square = game.board[column][row]
if (isX && isY) {
return { column: Number(column), row: Number(row), square }
}
}
}
}
const changeSize = size => {
if (game.size === size) {
return
}
game.size = size
game.puzzle = Math.floor(canvas.width / size)
reset()
}
const resize = () => {
const bg = Math.min(game.image.width, game.image.height)
const scale = game.resized ? defaultScale : (bg > maxScale ? maxScale : bg)
const inPixels = `${scale}px`
canvas.width = scale
canvas.height = scale
gameSize.style.width = inPixels
gameSize.style.height = inPixels
game.resized = !game.resized
game.puzzle = Math.floor(canvas.width / game.size)
reset()
}
const reset = () => {
game.win = false
ctx.lineWidth = 2
genBoard()
draw()
}
const upload = event => {
const reader = new FileReader()
reader.onload = evt => {
const img = new Image()
game.image = img
img.onload = () => {
if (game.resized) {
resize()
} else {
reset()
}
}
img.src = evt.target.result
}
reader.readAsDataURL(event.target.files[0])
}
const randomBackgroundColor = () => {
const colors = [
['#16a085', '#1abc9c'],
['#27ae60', '#2ecc71'],
['#2980b9', '#3498db'],
['#8e44ad', '#9b59b6'],
['#2c3e50', '#34495e'],
['#d35400', '#e67e22'],
['#c0392b', '#e74c3c']
]
const random = Math.floor(Math.random() * colors.length)
const background = colors[random]
const menu = document.querySelector('#menu')
game.color = background
document.body.style.background = background[0]
menu.style.background = background[1]
}
// Events
uploadButton.addEventListener('change', upload, false)
x3.addEventListener('click', () => changeSize(3))
x4.addEventListener('click', () => changeSize(4))
x5.addEventListener('click', () => changeSize(5))
x8.addEventListener('click', () => changeSize(8))
canvas.addEventListener('click', movePuzzle)
resizeButton.addEventListener('click', resize)
resetButton.addEventListener('click', reset)
// Change Background Color
randomBackgroundColor()