Skip to content

Commit 8b27b9c

Browse files
committed
choose image
1 parent 1350e8c commit 8b27b9c

File tree

5 files changed

+86
-6
lines changed

5 files changed

+86
-6
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ Currently only the rubby logo is one of the puzzle pictures, but the plan is to
66

77
TODO:
88
- [x] Best score and best time
9-
- [ ] Multiple images to choose from
10-
- [ ] Upload your own image
9+
- [x] Multiple images to choose from
10+
- [x] Upload your own image
1111
- [ ] Sliding animation
1212
- [ ] Noises?
1313
- [ ] Proper styling

images/1-9.png

-2.91 KB
Loading

index.html

+11
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,17 @@
1010
<h1>Slidey</h1>
1111
<h5>Slide the tiles to correct the picture!</h5>
1212

13+
<p style="text-align: center;">Choose an image</p>
14+
<div id="images">
15+
<button data-src="images/replit.png"><img src="images/replit.png"></button>
16+
<button data-src="images/ruby.png"><img src="images/ruby.png"></button>
17+
<button data-src="images/1-9.png"><img src="images/1-9.png"></button>
18+
<form id="img-form">
19+
<input type="file" id="img-input" accept="image/*" enctype="multipart/form-data" required>
20+
<button type="submit">Use Image</button>
21+
</form>
22+
</div>
23+
1324

1425
<button id="start">Play</button>
1526
<p>Moves: <span id="moves">0</span></p>

script.js

+52-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,30 @@ function _() {
88
return m + ":" + s;
99
}
1010

11+
function resetBoard() {
12+
const tiles = [
13+
"row1 col1",
14+
"row1 col2",
15+
"row1 col3",
16+
"row2 col1",
17+
"row2 col2",
18+
"row2 col3",
19+
"row3 col1",
20+
"row3 col2",
21+
"row3 col3",
22+
];
23+
let boardTiles = document.getElementById("board").children;
24+
for (let row = 0; row < boardTiles.length; row++) {
25+
for (let col=0; col < boardTiles[row].children.length; col++) {
26+
boardTiles[row].children[col].className = tiles[row*3+col] + " tile";
27+
}
28+
}
29+
30+
document.getElementById("moves").innerHTML = "0";
31+
document.getElementById("time").innerHTML = "0:00";
32+
document.getElementById("start").innerHTML = "Play";
33+
}
34+
1135
function shuffleBoard(blank) {
1236
const tiles = [
1337
"row1 col1",
@@ -121,7 +145,7 @@ function _() {
121145
document.getElementById("time").innerHTML = "0:00";
122146

123147
const blank = Math.floor(Math.random() * 9);
124-
document.getElementById("start").innerHTML = "restart";
148+
document.getElementById("start").innerHTML = "Restart";
125149
shuffleBoard(blank);
126150

127151
clearInterval(interval);
@@ -175,7 +199,6 @@ function _() {
175199
});
176200
});
177201
}
178-
179202
document.getElementById("start").addEventListener("click", play);
180203

181204
window.onload = () => {
@@ -187,5 +210,32 @@ function _() {
187210
bestTime = bestTime === null ? "NA" : displayTime(bestTime);
188211
document.getElementById("bestTime").innerHTML = bestTime;
189212
}
213+
214+
document.querySelectorAll("#images button").forEach(button => {
215+
button.addEventListener("click", () =>{
216+
document.querySelectorAll(".tile").forEach(tile => {
217+
tile.style.backgroundImage = "url("+button.getAttribute("data-src")+")";
218+
});
219+
clearInterval(interval);
220+
resetBoard();
221+
});
222+
});
223+
224+
document.getElementById("img-form").onsubmit = function (e) {
225+
const files = e.target.children[0].files;
226+
227+
var fr = new FileReader();
228+
fr.onload = function () {
229+
document.querySelectorAll(".tile").forEach(tile => {
230+
tile.style.backgroundImage = "url("+fr.result+")";
231+
});
232+
}
233+
fr.readAsDataURL(files[0]);
234+
235+
clearInterval(interval);
236+
resetBoard();
237+
238+
e.preventDefault();
239+
}
190240
}
191241
_();

style.css

+21-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ h1, h5 {
88
text-align: center;
99
}
1010

11+
h5 { margin-bottom: 20px; }
12+
1113
#board {
12-
--size: 75vh;
14+
--size: 50vh;
1315
--padding: 5px;
1416
--spacing: 2.5px;
1517
height: var(--size);
@@ -35,7 +37,7 @@ h1, h5 {
3537
}
3638

3739
.blank {
38-
background-image: none;
40+
background-image: none !important;
3941
background-color: transparent;
4042
}
4143

@@ -53,4 +55,21 @@ h1, h5 {
5355
.won {
5456
pointer-events: none;
5557
background-color: #171 !important;
58+
}
59+
60+
#images {
61+
margin: auto;
62+
width: fit-content;
63+
}
64+
65+
#images > button {
66+
width: 100px;
67+
height: 100px;
68+
cursor: pointer;
69+
}
70+
71+
#images img {
72+
width: 100%;
73+
height: 100%;
74+
object-fit: cover;
5675
}

0 commit comments

Comments
 (0)