แสดงโค้ด<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>สวิตช์ที่ปุ่มยืดตัวและออกไปเป็นลูกบอล</title>
<script src="https://cdn.jsdelivr.net/npm/[email protected] /dist/gsap.min.js"></script>
<style>
html, body { margin: 0; height: 100%; overflow: hidden; background: #000; }
#root { position: relative; overflow: hidden; transform-origin: 0 0; }
</style>
<style>
@import url("https://fonts.googleapis.com/css2?family=Geist:wght@400;500;600;700&display=swap");
#root { position: relative; width: 100%; height: 100%; overflow: hidden; background: #0d0b1e; font-family: "Geist", sans-serif; color: #fff; }
.stage { position: absolute; left: 0; top: 0; width: 1px; height: 1px; transform-origin: 0 0; }
.stage > * { position: absolute; }
.track { left: -27px; top: -25.9px; width: 91.4px; height: 51.8px; border-radius: 25.9px; background: #353244; }
.knob { left: 0; top: 0; width: 44px; height: 44px; border-radius: 50%; background: #fff; }
.cursor { left: 0; top: 0; width: 38px; height: 54px; overflow: visible; }
</style>
</head>
<body>
<div id="root" data-composition-id="main" data-start="0" data-duration="4" data-width="1920" data-height="1080">
<div id="s" class="stage clip" data-start="0" data-duration="4">
<div class="track" id="track"></div><div class="knob" id="knob"></div>
<svg class="cursor" id="cursor" viewBox="-1.5 -1.5 38 54"><path d="M1.2 1.9V42.6L11.9 32.8L17.6 47.2L22.1 45.3L18.2 31.4H32.2Z" fill="#0d0b1e" stroke="#fff" stroke-width="2.4" stroke-linejoin="round"/></svg>
</div>
</div>
<script>
// The composition at its own size, hidden, until its timeline plays: the code
// measures its layout as the renderer lays it out, not shrunk to the window.
(() => {
const root = document.getElementById('root');
if (!root) return;
Object.assign(root.style, {
width: (Number(root.dataset.width) || 1080) + 'px',
height: (Number(root.dataset.height) || 1920) + 'px',
visibility: 'hidden',
});
})();
</script>
<script>
const clamp = (v, a = 0, b = 1) => Math.min(b, Math.max(a, v));
const lerp = (a, b, u) => a + (b - a) * u;
// critically damped spring released at rest: 0 -> 1, omega in rad/s
const spring = (tau, w) => (tau <= 0 ? 0 : 1 - (1 + w * tau) * Math.exp(-w * tau));
const smooth = (u) => { u = clamp(u); return u * u * (3 - 2 * u); };
// a curve given at frames (60 fps) after an event: [frame, value] pairs
const keyed = (pairs, f) => { if (f <= pairs[0][0]) return pairs[0][1];
for (let i = 1; i < pairs.length; i++) if (f <= pairs[i][0]) { const [f0, v0] = pairs[i - 1], [f1, v1] = pairs[i]; return lerp(v0, v1, (f - f0) / (f1 - f0)); }
return pairs[pairs.length - 1][1]; };
document.getElementById("s").style.transform = "translate(800px, 540px) scale(3.95)";
const CLICK = 1.0, BACK = 3.0, OFF = [53, 50, 68], ON = [251, 45, 137];
// the knob while pressed: [frame after the click, x, width, height], measured from a real switch move
const PRESSED = [[0, 0, 44, 44], [2, 0.8, 45.8, 41.8], [4, 6.2, 48.1, 40], [6, 15.2, 50.1, 38.5], [8, 27.9, 51.9, 36.5], [9, 35.7, 52.9, 35.9]];
function knobAt(t) {
const f = (t - CLICK) * 60;
if (f < 0) return [0, 0, 44, 44];
if (f <= 9) { const i = PRESSED.findIndex((p) => p[0] >= f), [f0, x0, w0, h0] = PRESSED[Math.max(0, i - 1)], [f1, x1, w1, h1] = PRESSED[i];
const u = f1 === f0 ? 1 : (f - f0) / (f1 - f0); return [lerp(x0, x1, u), 0, lerp(w0, w1, u), lerp(h0, h1, u)]; }
if (t < BACK) { const u = clamp((t - CLICK - 0.15) / (BACK - CLICK - 0.15)); // out on an arc, round and back in as the knob
return [lerp(35.7, 0, u) + 420 * Math.sin(Math.PI * u), -300 * Math.sin(Math.PI * u), 44, 44]; }
return [0, 0, 44, 44];
}
function draw(t) {
const [x, y, w, h] = knobAt(t);
const k = document.getElementById("knob"); k.style.width = w + "px"; k.style.height = h + "px";
k.style.transform = `translate(${x - w / 2}px, ${y - h / 2}px)`;
const on = t < CLICK ? 0 : t < BACK ? spring(t - CLICK, 25) : 1 - smooth((t - BACK) / 0.135);
document.getElementById("track").style.background = `rgb(${OFF.map((v, i) => Math.round(lerp(v, ON[i], on))).join(",")})`;
const u = t < CLICK ? spring(t - 0.3, 24) : 1 - spring(t - CLICK - 0.4, 24); // the cursor comes in, clicks, leaves
document.getElementById("cursor").style.transform = `translate(${lerp(110, 24.3, u)}px, ${lerp(100, 4.4, u)}px)`;
}
const tl = gsap.timeline({ paused: true });
const clock = { t: 0 };
tl.to(clock, { t: 4, duration: 4, ease: "none", onUpdate: () => draw(clock.t) }, 0);
draw(0);
window.__timelines = window.__timelines || {};
window.__timelines["main"] = tl;
</script>
<script type="module">
// Once the code registers its timeline, which some do only after their fonts
// load, fit the composition to the window and play the timeline on a loop.
const root = document.getElementById('root');
const width = Number(root?.dataset.width) || 1080;
const height = Number(root?.dataset.height) || 1920;
const fit = () => {
if (!root) return;
const scale = Math.min(innerWidth / width, innerHeight / height);
Object.assign(root.style, {
width: width + 'px',
height: height + 'px',
transform: 'scale(' + scale + ')',
marginLeft: (innerWidth - width * scale) / 2 + 'px',
marginTop: (innerHeight - height * scale) / 2 + 'px',
});
};
const since = performance.now();
const play = () => {
const timeline = window.__timelines?.main;
if (!timeline && performance.now() - since < 5000) {
// Not a frame callback: those wait while the page is in the background.
setTimeout(play, 30);
return;
}
fit();
addEventListener('resize', fit);
if (root) root.style.visibility = '';
timeline?.repeat(-1).play(0);
};
play();
</script>
</body>
</html>