-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandomTiles.html
119 lines (105 loc) · 4.26 KB
/
randomTiles.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
<!DOCTYPE html>
<html>
<head>
<style>
body {
margin: 0;
overflow: hidden;
}
#game-container {
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-template-rows: repeat(4, 1fr);
height: 100vh;
width: 100vw;
}
.image-part {
opacity: 0;
transition: opacity 1s;
background-size: cover;
background-repeat: no-repeat;
background-position: center center;
}
</style>
</head>
<body>
<div id="game-container"></div>
<script>
// Specify the path to your image
const imagePath = 'images/1.png';
// Create a 4x4 grid
const numRows = 4;
const numCols = 4;
const numParts = numRows * numCols;
// Load the image
const image = new Image();
image.src = imagePath;
image.crossOrigin = "anonymous"
image.onload = splitImage;
// Split the image into parts
function splitImage() {
// Calculate the width and height of each image part based on the screen size
const screenWidth = window.innerWidth;
const screenHeight = window.innerHeight;
const partWidth = screenWidth / numCols;
const partHeight = screenHeight / numRows;
// Create a canvas to resize the image
const canvas = document.createElement('canvas');
const context = canvas.getContext('2d');
canvas.width = screenWidth;
canvas.height = screenHeight;
context.drawImage(image, 0, 0, canvas.width, canvas.height);
const gameContainer = document.getElementById('game-container');
gameContainer.style.width = `${screenWidth}px`;
gameContainer.style.height = `${screenHeight}px`;
const parts = [];
for (let row = 0; row < numRows; row++) {
for (let col = 0; col < numCols; col++) {
const partCanvas = document.createElement('canvas');
partCanvas.width = partWidth;
partCanvas.height = partHeight;
const partContext = partCanvas.getContext('2d');
partContext.drawImage(
canvas,
col * partWidth,
row * partHeight,
partWidth,
partHeight,
0,
0,
partCanvas.width,
partCanvas.height
);
const part = document.createElement('div');
part.className = 'image-part';
part.style.backgroundImage = `url(${partCanvas.toDataURL()})`;
parts.push(part);
}
}
// Shuffle the parts array
shuffleArray(parts);
// Append the shuffled parts to the game container
parts.forEach((part) => {
gameContainer.appendChild(part);
});
// Fade in the image parts one by one in a random order
let counter = 0;
const interval = setInterval(() => {
if (counter >= numParts) {
clearInterval(interval);
return;
}
parts[counter].style.opacity = '1';
counter++;
}, 250); // Adjust the interval to control the fade-in speed
}
// Utility function to shuffle an array
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
}
</script>
</body>
</html>