UNPKG

@ssgoi/core

Version:

Core animation engine for SSGOI - Native app-like page transitions with spring physics

132 lines (131 loc) 4.59 kB
function R(i, a) { function o(d) { let l = 0, y = 0; const t = d.offsetWidth, g = d.offsetHeight; let s = d; for (; s; ) l += s.offsetTop, y += s.offsetLeft, s = s.offsetParent; return l -= window.scrollY, y -= window.scrollX, { top: l, left: y, width: t, height: g }; } const e = o(i), n = o(a); return new DOMRect( n.left - e.left, n.top - e.top, n.width, n.height ); } const O = (i, a) => { var o; i.style.position = "absolute", i.style.width = "100%", i.style.top = `${-1 * (((o = a == null ? void 0 : a.scrollOffset) == null ? void 0 : o.y) ?? 0)}px`, i.style.left = "0"; }; function v(i, a = "viewport") { if (a === "positioned-parent") { const o = i.positionedParent.getBoundingClientRect(); return { top: o.top, left: o.left, width: o.width, height: o.height }; } else { const o = R(document.body, i.positionedParent); return { top: i.scroll.y, left: 0, width: o.width, height: window.innerHeight - o.top }; } } const F = (i = {}) => M(i), M = (i = {}) => { var l, y; const a = { // Much slower animation - 4x duration by reducing stiffness further stiffness: ((l = i.spring) == null ? void 0 : l.stiffness) ?? 50, damping: ((y = i.spring) == null ? void 0 : y.damping) ?? 30 }, o = i.initialRotation ?? 45, e = i.initialScale ?? 0.01, n = i.rotationTriggerPoint ?? 0.8, d = i.containerMode ?? "viewport"; return { out: async (t, g) => { const s = t.style.opacity; return { spring: { stiffness: 80, damping: 25 }, from: 1, to: 0, prepare: () => { O(t, g), t.style.opacity = "1"; }, tick: (w) => { t.style.opacity = String(w); }, onEnd: () => { t.style.opacity = s; } }; }, in: async (t, g) => { const s = v(g, d), w = t.style.transform, b = t.style.transformOrigin, x = t.style.position, $ = t.style.zIndex; return { spring: a, from: 0, to: 1, prepare: () => { const r = Math.min(s.width, s.height) * 0.4; if (t.style.setProperty( "--max-border-radius", `${r}px` ), t.style.setProperty("--border-radius-scale", "1"), t.style.willChange = "transform", t.style.backfaceVisibility = "hidden", d === "positioned-parent") t.style.transformOrigin = "50% 50%", t.style.position = "absolute", t.style.top = "0px", t.style.left = "0px", t.style.width = `${s.width}px`, t.style.height = `${s.height}px`; else { const f = s.left + s.width / 2, h = s.top + s.height / 2; t.style.transformOrigin = `${f}px ${h}px`, t.style.position = "fixed", t.style.top = `${s.top}px`, t.style.left = `${s.left}px`, t.style.width = `${s.width}px`, t.style.height = `${s.height}px`; } t.style.zIndex = "1000", t.style.overflow = "hidden", t.style.transform = `rotate(${o}deg) scale(${e}) translateZ(0)`, t.style.borderRadius = "calc(var(--max-border-radius) * var(--border-radius-scale))", t.style.opacity = "1"; }, tick: (r) => { let f; if (r <= 0.05) f = e; else if (r <= n) { const c = (r - 0.05) / (n - 0.05), u = Math.pow(c, 9); f = e + (0.8 - e) * u; } else { const c = (r - n) / (1 - n); f = 0.8 + 0.2 * (1 - Math.pow(1 - c, 3)); } let h; const p = 0.7; if (r <= p) h = o; else { const c = (r - p) / (1 - p), u = 1 - Math.pow(1 - c, 2); h = o * (1 - u); } let P; if (r <= p) P = 1; else { const c = (r - p) / (1 - p); P = 1 - Math.pow(c, 0.5); } t.style.transform = `rotate(${h.toFixed(2)}deg) scale(${f.toFixed(4)}) translateZ(0)`, t.style.setProperty( "--border-radius-scale", P.toFixed(4) ); }, onEnd: () => { t.style.willChange = "", t.style.backfaceVisibility = "", t.style.removeProperty("--max-border-radius"), t.style.removeProperty("--border-radius-scale"), t.style.transform = w, t.style.transformOrigin = b, t.style.position = x, t.style.zIndex = $, t.style.top = "", t.style.left = "", t.style.width = "", t.style.height = "", t.style.overflow = "", t.style.borderRadius = ""; } }; } }; }; export { F as a, R as g, M as j, O as p };