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
|
(() => {
const card = document.getElementById("card"),
toast = document.getElementById("toast");
const X_URL = "https://x.com/sleppyeric";
let rotY = 0,
tX = 0,
tY = 0;
let dragging = false,
sx = 0,
sy = 0,
lastX = 0,
lastT = 0,
vel = 0,
moved = 0,
spinAcc = 0;
let animId = 0,
firing = false;
const render = () => {
card.style.transform = `rotateX(${tX}deg) rotateY(${rotY + tY}deg)`;
};
const stopAnim = () => {
cancelAnimationFrame(animId);
};
function animateTo(target, speed, done) {
stopAnim();
const step = () => {
const d = target - rotY;
if (Math.abs(d) < 0.5) {
rotY = target;
render();
done && done();
return;
}
rotY += d * speed;
render();
animId = requestAnimationFrame(step);
};
step();
}
function setGlare(px, py) {
const gx = px * 100 + "%",
gy = py * 100 + "%";
for (const el of [
document.getElementById("holoF"),
document.getElementById("holoB"),
]) {
el.style.setProperty("--gx", gx);
el.style.setProperty("--gy", gy);
el.style.setProperty("--hp", gx);
}
}
setGlare(0.5, 0.42);
render();
card.addEventListener("pointerdown", (e) => {
if (firing) return;
dragging = true;
moved = 0;
spinAcc = 0;
vel = 0;
sx = lastX = e.clientX;
sy = e.clientY;
lastT = performance.now();
stopAnim();
try {
card.setPointerCapture(e.pointerId);
} catch (_) {}
});
card.addEventListener("pointermove", (e) => {
if (!dragging) return;
const dx = e.clientX - lastX;
lastX = e.clientX;
moved = Math.max(moved, Math.hypot(e.clientX - sx, e.clientY - sy));
const now = performance.now();
vel = 0.7 * vel + 0.3 * ((dx / Math.max(now - lastT, 1)) * 16);
lastT = now;
rotY += dx * 0.65;
spinAcc += Math.abs(dx * 0.65);
render();
});
function endDrag() {
if (!dragging) return;
dragging = false;
if (moved < 8) {
const target = (Math.round(rotY / 180) + 1) * 180;
animateTo(target, 0.16);
return;
}
if (spinAcc >= 355) {
fireX();
return;
}
stopAnim();
const step = () => {
if (Math.abs(vel) > 0.15) {
rotY += vel;
spinAcc += Math.abs(vel);
vel *= 0.95;
render();
if (spinAcc >= 355) {
fireX();
return;
}
animId = requestAnimationFrame(step);
} else animateTo(Math.round(rotY / 180) * 180, 0.16);
};
step();
}
card.addEventListener("pointerup", endDrag);
card.addEventListener("pointercancel", endDrag);
function fireX() {
firing = true;
toast.style.opacity = 1;
animateTo(rotY + 720, 0.09, () => {
try {
location.assign(X_URL);
} catch (_) {}
setTimeout(() => {
toast.textContent =
"(navigation blocked in preview — works when hosted)";
setTimeout(() => {
toast.style.opacity = 0;
toast.textContent = "full spin — opening X ✊";
firing = false;
animateTo(Math.round(rotY / 180) * 180, 0.16);
}, 1800);
}, 1200);
});
}
const tiltBtn = document.getElementById("tiltBtn");
function onOrient(e) {
if (e.gamma == null) return;
const g = Math.max(-30, Math.min(30, e.gamma)),
b = Math.max(-30, Math.min(30, (e.beta || 0) - 40));
tY = g * 0.4;
tX = -b * 0.3;
if (!dragging && !firing) render();
setGlare((g + 30) / 60, (b + 30) / 60);
}
if (
typeof DeviceOrientationEvent !== "undefined" &&
typeof DeviceOrientationEvent.requestPermission === "function"
) {
tiltBtn.style.display = "inline-block";
tiltBtn.addEventListener("click", () => {
DeviceOrientationEvent.requestPermission()
.then((s) => {
if (s === "granted") {
window.addEventListener("deviceorientation", onOrient);
tiltBtn.style.display = "none";
} else tiltBtn.textContent = "tilt permission denied";
})
.catch(() => {
tiltBtn.textContent = "tilt unavailable here — works when hosted";
});
});
} else if ("DeviceOrientationEvent" in window) {
window.addEventListener("deviceorientation", onOrient);
}
document.addEventListener("mousemove", (e) => {
if (dragging) return;
const r = card.getBoundingClientRect();
setGlare(
Math.min(1, Math.max(0, (e.clientX - r.left) / r.width)),
Math.min(1, Math.max(0, (e.clientY - r.top) / r.height)),
);
});
})();
|