-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
232 lines (191 loc) · 8.51 KB
/
script.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
227
228
229
230
231
232
let container = document.querySelector('.container');
let touchStartX, touchStartY;
let dragItem = null;
// Create divs
let itemsArray = ['main-face'];
itemsArray.forEach(item => {
let itemDiv = document.createElement('div');
itemDiv.classList.add(item);
container.appendChild(itemDiv);
});
// Create main image
let mainFaceImage = document.createElement('img');
mainFaceImage.setAttribute('src', './SVG/Asset 2.svg');
document.querySelector('.main-face').appendChild(mainFaceImage);
// Create divs inside the main div referring to facial organs
let organs = ["brow", "eye", "nose", "mouth"];
organs.forEach(item => {
let organsDiv = document.createElement('div');
organsDiv.classList.add(item);
document.querySelector('.main-face').appendChild(organsDiv);
});
// Create boxes containing images of facial organs
let boxImages = {
faces: ['./SVG/face/Asset 8.svg', './SVG/face/Asset 9.svg', "./SVG/face/Asset 10.svg", "./SVG/face/Asset 11.svg", "./SVG/face/Asset 12.svg", "./SVG/face/Asset 13.svg"],
eyes: ["./SVG/eyes/Asset 2.svg", "./SVG/eyes/Asset 3.svg", "./SVG/eyes/Asset 4.svg", "./SVG/eyes/Asset 5.svg"],
brows: ["./SVG/brows/Asset 18.svg", "./SVG/brows/Asset 19.svg", "./SVG/brows/Asset 20.svg"],
noses: ["./SVG/moses/Asset 21.svg", "./SVG/moses/Asset 22.svg", "./SVG/moses/Asset 23.svg"],
lips: ['./SVG/lips/Asset 14.svg', './SVG/lips/Asset 15.svg', "./SVG/lips/Asset 16.svg", "./SVG/lips/Asset 17.svg"]
};
for (const category in boxImages) {
if (Object.hasOwnProperty.call(boxImages, category)) {
const images = boxImages[category];
const categoryBox = document.createElement('div');
categoryBox.classList.add(category);
images.forEach((imageUrl, index) => {
const box = document.createElement('div');
box.classList.add('box');
box.setAttribute('draggable', 'true');
const imageOfBox = document.createElement('img');
imageOfBox.setAttribute('src', imageUrl);
imageOfBox.classList.add(category.slice(0, category.length - 1))
box.appendChild(imageOfBox);
categoryBox.appendChild(box);
});
container.appendChild(categoryBox);
}
}
let mainFace = document.querySelector('.main-face');
container.addEventListener('dragstart', function (e) {
dragItem = e.target;
e.dataTransfer.setData('text/plain', '');
e.target.style.opacity = '0.5';
});
container.addEventListener('dragend', function () {
if (dragItem) {
dragItem.style.opacity = '1';
}
dragItem = null;
});
mainFace.addEventListener('dragover', function (e) {
e.preventDefault();
});
mainFace.addEventListener('drop', function (e) {
e.preventDefault();
if (dragItem && dragItem.classList && dragItem.classList.contains('face')) {
if (mainFace.children[0].tagName === 'IMG') {
mainFace.children[0].src = dragItem.src;
}
} else if (dragItem && dragItem.classList && dragItem.classList.contains('brow')) {
if (mainFace.children[1]) {
dragItem.style.opacity = '1';
mainFace.children[1].innerHTML = '';
mainFace.children[1].appendChild(dragItem.cloneNode(true));
}
} else if (dragItem && dragItem.classList && dragItem.classList.contains('eye')) {
if (mainFace.children[2]) {
dragItem.style.opacity = '1';
mainFace.children[2].innerHTML = '';
mainFace.children[2].appendChild(dragItem.cloneNode(true));
}
} else if (dragItem && dragItem.classList && dragItem.classList.contains('nose')) {
if (mainFace.children[3]) {
dragItem.style.opacity = '1';
mainFace.children[3].innerHTML = '';
mainFace.children[3].appendChild(dragItem.cloneNode(true));
}
} else if (dragItem && dragItem.classList && dragItem.classList.contains('lip')) {
if (mainFace.children[4]) {
dragItem.style.opacity = '1';
mainFace.children[4].innerHTML = '';
mainFace.children[4].appendChild(dragItem.cloneNode(true));
}
}
});
// Prevent the default behavior for drag and drop on the entire document
document.addEventListener('dragover', function (e) {
e.preventDefault();
});
document.addEventListener('drop', function (e) {
e.preventDefault();
});
// Select the container with items you want to make sortable
if (window.matchMedia("(max-width: 600px)").matches) {
// Function to initialize SortableJS for a specific container and items
function initializeSortable(containerSelector, itemSelector, onEndCallback) {
const container = document.querySelector(containerSelector);
// Initialize SortableJS with touch support for the specified container
new Sortable(container, {
animation: 150, // You can customize the animation duration
touchStartThreshold: 10, // Customize the touch threshold as needed
onStart(evt) {
evt.from.querySelectorAll(itemSelector).forEach(item => {
item.style.opacity = '0.5';
});
},
onEnd(evt) {
evt.from.querySelectorAll(itemSelector).forEach(item => {
item.style.opacity = '1';
});
// Callback function for when the sorting ends
if (typeof onEndCallback === 'function') {
onEndCallback(evt);
}
},
});
}
// Callback function to handle replacing the main-face
function replaceMainFace(evt) {
const mainFace = document.querySelector('.main-face');
if (evt.newIndex !== null) {
const selectedFace = evt.from.children[evt.newIndex];
const mainFaceImage = mainFace.querySelector('img');
// Replace the src attribute of the main-face image with the selected face image's src
mainFaceImage.src = selectedFace.querySelector('img').src;
}
}
// Callback function to handle append brows to the main-face
function appendbrowToMainFace(evt) {
if (evt.newIndex !== null) {
const selectedEye = evt.from.children[evt.newIndex];
const mainFaceBrowsDiv = document.querySelector('.main-face .brow');
// Clear the current content of the main-face eye div
mainFaceBrowsDiv.innerHTML = '';
// Append the selected eye to the main-face eye div
mainFaceBrowsDiv.appendChild(selectedEye.cloneNode(true));
}
}
// Callback function to handle append lip to the main-face
function appendLipToMainFace(evt) {
if (evt.newIndex !== null) {
const selectedNose = evt.from.children[evt.newIndex].querySelector('img'); // Get the selected nose image
const mainFaceLipDiv = document.querySelector('.main-face .mouth');
// Clear the current content of the main-face nose div
mainFaceLipDiv.innerHTML = '';
// Append the selected nose image to the main-face nose div
mainFaceLipDiv.appendChild(selectedNose.cloneNode(true));
}
}
// Callback function to handle append nose to the main-face
function appendNoseToMainFace(evt) {
if (evt.newIndex !== null) {
const selectedNose = evt.from.children[evt.newIndex].querySelector('img'); // Get the selected nose image
const mainFaceNoseDiv = document.querySelector('.main-face .nose');
mainFaceNoseDiv.innerHTML = '';
mainFaceNoseDiv.appendChild(selectedNose.cloneNode(true));
}
}
// Callback function to handle append brows to the main-face
function appendBrowToMainFace(evt) {
if (evt.newIndex !== null) {
const selectedNose = evt.from.children[evt.newIndex].querySelector('img'); // Get the selected nose image
const mainFaceBrowDiv = document.querySelector('.main-face .brow');
mainFaceBrowDiv.innerHTML = '';
mainFaceBrowDiv.appendChild(selectedNose.cloneNode(true));
}
}
// Callback function to handle append eyes to the main-face
function appendEyeToMainFace(evt) {
if (evt.newIndex !== null) {
const selectedNose = evt.from.children[evt.newIndex].querySelector('img'); // Get the selected nose image
const mainFaceEyeDiv = document.querySelector('.main-face .eye');
mainFaceEyeDiv.innerHTML = '';
mainFaceEyeDiv.appendChild(selectedNose.cloneNode(true));
}
}
initializeSortable('.eyes', '.box', appendEyeToMainFace);
initializeSortable('.brows', '.box', appendBrowToMainFace);
initializeSortable('.noses', '.box', appendNoseToMainFace);
initializeSortable('.lips', '.box', appendLipToMainFace);
initializeSortable('.faces', '.box', replaceMainFace);
}