-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathready.js
170 lines (144 loc) · 5.6 KB
/
ready.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
window.onload = function () {
var parent = document.getElementsByClassName("puzzleBox")[0];
var div = [];
var rows = 4,
columns = 4;
var margin = 11;
var border = 3;
var divTop = margin;
var divLeft = margin;
var divWidth = 75;
var i = 0;
var shiftX;
var shiftY;
// clock
var h = 0,
min = 0,
sec = 0,
textclock;
///////////// clock clock
textclock = setInterval(clockgame, 1000);
clockgame();
function clockgame() {
var clock = document.getElementsByClassName("clock")[0];
var time = new Date();
h = (time.getHours() % 24).toString();
min = time.getMinutes().toString();
sec = time.getSeconds().toString();
if (h.length < 2) {
h = "0" + h;
}
if (min.length < 2) {
min = "0" + min;
}
if (sec.length < 2) {
sec = "0" + sec;
}
textclock = h + ":" + min + ":" + sec;
clock.innerHTML = textclock;
}
//for(var i=0;i<15;i++){
for (columns = 0; columns < 4; columns++) {
for (rows = 0; rows < 4; rows++) {
div[i] = document.createElement("div");
div[i].className = "puzzleBlock";
div[i].innerHTML = i + 1;
div[i].style.top = divTop + "px";
div[i].style.left = divLeft + "px";
parent.appendChild(div[i]);
i = i + 1;
if (i > 14) break;
divLeft = divLeft + divWidth;
}
divLeft = margin;
divTop = divTop + divWidth;
}
document.onmousedown = function (e) {
i = e.target.innerHTML - 1;
var coordsmousedownElem = getCoords(div[i]);
shiftX = e.pageX - coordsmousedownElem.left;
shiftY = e.pageY - coordsmousedownElem.top;
document.onmousemove = function (e) {
let currentElem = div[i];
// выход за коробку - return
if ((e.pageX + (div[i].offsetWidth - shiftX) > parent.offsetWidth + margin - 6)
|| (e.pageX - shiftX < margin)
|| (e.pageY - shiftY < margin)
|| (e.pageY + border * 2 + (div[i].offsetHeight - shiftY) > parent.offsetHeight + margin
))
return;
let newX = e.pageX - shiftX;
let newY = e.pageY - shiftY;
let allowX = true;
let allowY = true;
for (let index in div) {
if (index == i) continue;
let otherElem = div[index];
let currentX = getCoords(currentElem).left;
let currentY = getCoords(currentElem).top;
if (!isLeftSideCollide(newX, otherElem) || !isRightSideCollide(newX, otherElem)) {
// allowX = true;
} else {
if (!isTopCollide(currentY, otherElem) || !isBottomCollide(currentY, otherElem)) {
// allowX = true;
} else {
allowX = false;
continue;
}
}
if (!isTopCollide(newY, otherElem) || !isBottomCollide(newY, otherElem)) {
} else {
if (!isLeftSideCollide(newX, otherElem) || !isRightSideCollide(newX, otherElem)) {
} else {
allowY = false;
continue;
}
}
}
if (allowX)
currentElem.style.left = newX + "px";
if (allowY)
currentElem.style.top = newY + "px";
};
// проверка на столкновение правой стороны с другим эл-том
function isRightSideCollide(newX, otherElem) {
let width = otherElem.offsetWidth;
let otherElemLeftSide = getCoords(otherElem).left;
let newRightSide = newX + width;
return newRightSide > otherElemLeftSide;
}
// проверка на столкновение левой стороны с другим эл-том
function isLeftSideCollide(newX, otherElem) {
let width = otherElem.offsetWidth;
let otherElemRightSide = getCoords(otherElem).left + width;
let newLeftSide = newX;
return newLeftSide < otherElemRightSide;
}
// проверка на столкновение нижней стороны с другим эл-том
function isTopCollide(newY, otherElem) {
let height = otherElem.offsetHeight;
let newBottom = newY + height;
let otherElemTop = getCoords(otherElem).top;
return newBottom > otherElemTop;
}
// проверка на столкновение с верхней стороны с другим эл-том
function isBottomCollide(newY, otherElem) {
let height = otherElem.offsetHeight;
let newTop = newY;
let otherElemBottom = getCoords(otherElem).top + height;
return newTop < otherElemBottom;
}
document.onmouseup = function () {
document.onmousemove = null;
div[i].onmouseup = null;
};
};
function getCoords(elem) {
// кроме IE8-
var box = elem.getBoundingClientRect();
return {
top: box.top,
left: box.left
};
}
};