Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
133 lines (122 loc) · 3.54 KB
/
Copy pathscript.js
File metadata and controls
133 lines (122 loc) · 3.54 KB
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
let escala = 1;
let escalaInicial = 1;
let distInicial = 0;
let panX = 0, panY = 0;
let panInicialX = 0, panInicialY = 0;
let startX = 0, startY = 0;
let ultimoTap = 0;
function dist(touches) {
return Math.hypot(
touches[0].clientX - touches[1].clientX,
touches[0].clientY - touches[1].clientY
);
}
function limitarPan(img) {
const rect = img.getBoundingClientRect();
const limiteX = (rect.width * (escala - 1)) / 2;
const limiteY = (rect.height * (escala - 1)) / 2;
panX = Math.max(-limiteX, Math.min(limiteX, panX));
panY = Math.max(-limiteY, Math.min(limiteY, panY));
}
function aplicar() {
const img = document.getElementById("lightbox-img");
if (!img) return;
limitarPan(img);
img.style.transform = `translate(${panX}px, ${panY}px) scale(${escala})`;
}
function resetZoom() {
escala = 1;
panX = panY = 0;
const img = document.getElementById("lightbox-img");
if (!img) return;
img.style.transition = "transform 0.3s ease";
img.style.transform = "scale(1)";
setTimeout(() => {
img.style.transition = "none";
}, 300);
}
// ── TOUCH ─────────────────────────────────────
function onTouchStart(e) {
if (e.touches.length === 2) {
e.preventDefault();
distInicial = dist(e.touches);
escalaInicial = escala;
panInicialX = panX;
panInicialY = panY;
} else if (e.touches.length === 1) {
const agora = Date.now();
if (agora - ultimoTap < 300) {
if (escala === 1) {
escala = 2;
} else {
escala = 1;
panX = panY = 0;
}
aplicar();
}
ultimoTap = agora;
startX = e.touches[0].clientX;
startY = e.touches[0].clientY;
panInicialX = panX;
panInicialY = panY;
}
}
function onTouchMove(e) {
if (e.touches.length === 2) {
e.preventDefault();
if (distInicial === 0) return;
const novaEscala = Math.min(
Math.max((escalaInicial * dist(e.touches)) / distInicial, 1),
4
);
escala = novaEscala;
aplicar();
} else if (e.touches.length === 1 && escala > 1) {
e.preventDefault();
const dx = e.touches[0].clientX - startX;
const dy = e.touches[0].clientY - startY;
panX = panInicialX + dx;
panY = panInicialY + dy;
aplicar();
}
}
function onTouchEnd() {
if (escala <= 1) {
escala = 1;
panX = panY = 0;
aplicar();
}
}
// ── LIGHTBOX ─────────────────────────────────
function abrirLightbox(imagem) {
const img = document.getElementById("lightbox-img");
const lightbox = document.getElementById("lightbox");
if (!img || !lightbox) return;
img.src = "";
img.src = imagem;
resetZoom();
lightbox.style.display = "flex";
requestAnimationFrame(() => {
lightbox.classList.add("show");
});
document.body.style.overflow = "hidden";
}
function fecharLightbox() {
const lightbox = document.getElementById("lightbox");
if (!lightbox) return;
lightbox.classList.remove("show");
setTimeout(() => {
lightbox.style.display = "none";
resetZoom();
}, 300);
document.body.style.overflow = "";
}
// ── INICIALIZAÇÃO ─────────────────────────────
document.addEventListener("DOMContentLoaded", () => {
const img = document.getElementById("lightbox-img");
if (img) {
img.addEventListener("touchstart", onTouchStart, { passive: false });
img.addEventListener("touchmove", onTouchMove, { passive: false });
img.addEventListener("touchend", onTouchEnd);
}
});