forked from YoDobchev/manastirskata-biblioteka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpuzzle.js
229 lines (186 loc) · 6.36 KB
/
puzzle.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
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
const path = 'pictures/pic'+Number(Math.floor(Math.random() * 10) + 1)+'.jpg';
const img = new Image(900,900);
img.src = path;
img.onload = cutImageIntoPieces;
// Define variables
const pieces = [];
const canvasWidth = 900;
const canvasHeight = 900;
const canvasHolder = document.getElementById('canvas-container');
const canvasArray = Array.from(document.getElementById('canvas-container').children);
let sortedCanvases;
// Cut the image into 9 pieces
function cutImageIntoPieces() {
const canvas = document.createElement('canvas');
canvas.width = canvasWidth;
canvas.height = canvasHeight;
const ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0, canvasWidth, canvasHeight);
let count = 1;
sortedCanvases = [];
for (let i = 1; i < canvasWidth; i += canvasWidth / 4) {
for (let j = 1; j < canvasHeight; j += canvasHeight / 4) {
const piece = document.getElementById('canvas' + count);
piece.width = canvasWidth / 4;
piece.height = canvasHeight / 4;
piece.setAttribute('data-index', count - 1);
const pieceCtx = piece.getContext('2d');
pieceCtx.drawImage(canvas, i, j, canvasWidth / 4, canvasHeight / 4, 0, 0, canvasWidth / 4, canvasHeight / 4);
pieces.push({ canvas: piece, x: i, y: j });
sortedCanvases.push(piece);
count++;
}
}
// Shuffle the pieces
shuffle();
}
// Shuffle the pieces randomly
function shuffle() {
pieces.sort(() => Math.random() - 0.5);
drawPieces();
}
// Draw the pieces in the correct position on the canvas
function drawPieces() {
pieces.forEach((piece, index) => {
const x = (index % 4) * (canvasWidth / 4);
const y = Math.floor(index / 4) * (canvasHeight / 4);
piece.canvas.style.left = x + 'px';
piece.canvas.style.top = y + 'px';
piece.x = x;
piece.y = y;
document.getElementById('canvas-container').appendChild(piece.canvas);
});
}
let activeCanvasIndex = 0;
canvasHolder.children[activeCanvasIndex].className = 'active';
function setActiveCanvas(index) {
canvasHolder.children[activeCanvasIndex].className = '';
activeCanvasIndex = index;
canvasHolder.children[activeCanvasIndex].className = 'active';
}
function swapCanvases(index1, index2){
const canvasArray = Array.from(document.getElementById('canvas-container').children)
const ctx1 = canvasArray[index1];
const ctx2 = canvasArray[index2];
let id1 = ctx1.id;
let id2 = ctx2.id;
ctx1.id = id2;
ctx2.id = id1;
// Create a temporary canvas to hold the contents of canvas1
const tempCanvas = document.createElement('canvas');
const tempCtx = tempCanvas.getContext('2d');
tempCanvas.width = canvasArray[index1].width;
tempCanvas.height = canvasArray[index1].height;
tempCtx.drawImage(canvasArray[index1], 0, 0);
// Copy the contents of canvas2 to canvas1
ctx1.getContext('2d').drawImage(canvasArray[index2], 0, 0);
// Copy the contents of the temporary canvas to canvas2
ctx2.getContext('2d').drawImage(tempCanvas, 0, 0);
}
function handleKeyDown(event) {
switch (event.key) {
case "ArrowUp":
if (activeCanvasIndex >= 4) {
setActiveCanvas(activeCanvasIndex - 4);
if (event.shiftKey) {
swapCanvases(activeCanvasIndex+4, activeCanvasIndex);
}
}
break;
case "ArrowDown":
if (activeCanvasIndex < 12) {
setActiveCanvas(activeCanvasIndex + 4);
if (event.shiftKey){
swapCanvases(activeCanvasIndex-4, activeCanvasIndex);
}
}
break;
case "ArrowLeft":
if (activeCanvasIndex % 4 !== 0) {
setActiveCanvas(activeCanvasIndex - 1);
if (event.shiftKey) {
swapCanvases(activeCanvasIndex+1, activeCanvasIndex);
}
}
break;
case "ArrowRight":
if ((activeCanvasIndex + 1) % 4 !== 0) {
setActiveCanvas(activeCanvasIndex + 1);
if (event.shiftKey) {
swapCanvases(activeCanvasIndex-1, activeCanvasIndex);
}
}
break;
}
}
let mouseDown = false;
let startMouseX, startMouseY;
function handleMouseDown(event) {
event.preventDefault();
mouseDown = true;
startMouseX = event.pageX - canvasHolder.offsetLeft;
startMouseY = event.pageY - canvasHolder.offsetTop;
dragStartIndex = parseInt(event.target.dataset.index);
}
function handleMouseMove(event) {
event.preventDefault();
if (mouseDown) {
const canvasArray = Array.from(canvasHolder.children);
const dragEndIndex = parseInt(event.target.dataset.index);
const currentMouseX = event.pageX - canvasHolder.offsetLeft;
const currentMouseY = event.pageY - canvasHolder.offsetTop;
// Calculate the difference between the start mouse position and the current mouse position
const diffX = currentMouseX - startMouseX;
const diffY = currentMouseY - startMouseY;
// Move the dragged canvas to the new position
canvasArray[dragStartIndex].style.left = `${canvasArray[dragStartIndex].offsetLeft + diffX}px`;
canvasArray[dragStartIndex].style.top = `${canvasArray[dragStartIndex].offsetTop + diffY}px`;
// Swap canvases if the dragged canvas overlaps with another canvas
for (let i = 0; i < canvasArray.length; i++) {
if (i !== dragStartIndex) {
const canvasRect = canvasArray[i].getBoundingClientRect();
if (
currentMouseX > canvasRect.left &&
currentMouseX < canvasRect.right &&
currentMouseY > canvasRect.top &&
currentMouseY < canvasRect.bottom
) {
swapCanvases(dragStartIndex, i);
dragStartIndex = i;
break;
}
}
}
}
}
function handleMouseUp() {
mouseDown = false;
}
canvasHolder.addEventListener('mousedown', handleMouseDown);
canvasHolder.addEventListener('mousemove', handleMouseMove);
canvasHolder.addEventListener('mouseup', handleMouseUp);
window.addEventListener('keydown', handleKeyDown);
document.getElementById('check-puzzle').addEventListener('click', onClick);
function onClick(event){
let trav = [
0,4, 8, 12,1,5,9,13,2,6,10,14,3,7,11,15
];
let t = true;
console.log(canvasHolder.children);
for (let i = 0; i < 16; ++i){
console.log(canvasHolder.children[i].id, 'canvas'+Number(i+1));
if (canvasHolder.children[i].id !== 'canvas'+Number(1+trav[i])){
t=false;
//break;
}
}
isValid(t)
}
function isValid(bool){
if (bool) {
console.log('equal');
}
else {
console.log('not equal');
}
}