-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
67 lines (59 loc) · 2.3 KB
/
index.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
import './style.css';
import * as htmlToImage from 'html-to-image';
import cubeHandler from './cubeHandler.js';
async function initialize() {
for (let element of document.querySelectorAll(".cubeUIContainer")) {
let faces = Array.from(element.children);
let largestWidth = 0;
let largestHeight = 0;
for (let face of faces) {
const rect = face.getBoundingClientRect();
largestWidth = rect.width > largestWidth ? rect.width : largestWidth;
largestHeight = rect.height > largestHeight ? rect.height : largestHeight;
}
let faceImageURLs = {};
async function updateFaceImage(index) {
faceImageURLs[index] = await htmlToImage.toPng(faces[index], { style: { 'opacity': '1' } });
}
async function updateFaceImages() {
for (let [index] of faces.entries()) {
await updateFaceImage(index);
}
}
await updateFaceImages();
let isDragging = false;
function drag() {
isDragging = true;
}
function releaseDrag() {
setTimeout(() => { //delay is for tween time
isDragging = false;
}, 100);
}
let observer;
function setActiveFace(index) {
for (let face of faces) {
face.classList.add("hide");
}
faces[index].classList.remove("hide");
observer && observer.disconnect();
const config = { attributes: true, childList: true, subtree: true };
async function tryUpdate(){
if (!isDragging) {
await updateFaceImage(index);
cubeHandler.updateCube(cube, faceImageURLs);
}
}
observer = new MutationObserver(tryUpdate);
observer.observe(faces[index], config);
for (let input of faces[index].getElementsByTagName("input")){
input.addEventListener('change', tryUpdate);
}
}
setActiveFace(0);
let cube = cubeHandler.createCube(element, setActiveFace, [largestWidth, largestHeight], [drag, releaseDrag]);
cubeHandler.updateCube(cube, faceImageURLs);
element.classList.remove("loading");
}
}
export default initialize;