Files
ToolboxRelease/assets/scene.js
T

272 lines
10 KiB
JavaScript
Raw Normal View History

// 滚动场景:品牌图标随滚动换形状、换主题色,并在宽屏上停靠到左栏,给右侧内容让出位置。
// 结构取自 FlClash 官网(GPL-3.0),图标与分区改为中育Toolbox 的。
(() => {
'use strict';
const root = document.documentElement;
const $ = (selector) => document.querySelector(selector);
const reducedMotion = matchMedia('(prefers-reduced-motion: reduce)');
const wide = matchMedia('(min-width: 960px)');
const { start: START_MORPH, enter: ENTER_MORPH, cycle: CYCLE_MORPHS } = globalThis.ZytbShapes;
// 页面滚动时依次走过的主题色。取自 style.css 里的 data-accent;
// 只留在蓝与绿之间走:整页铺满大红、明黄或品红时,太像报错或促销。
const ACCENTS = ['blue', 'green'];
const MORPH_MS = 450;
// 首屏保持起始形状;之后每 STEP_VH 的滚动换一个形状、换一套配色。
const HERO_VH = 0.08;
const STEP_VH = 0.85;
// 宽屏下图标停靠完成所需的滚动量,与 style.css 里 .flow 的负边距对应。
const DOCKED_VH = 0.45;
const shape = { pos: 0, origin: 0, to: 0, target: 0, t: 1 };
// 点按钮一次多走一个形状;滚动仍按原位置算,两者相加,所以点完不会立刻被滚动拉回去。
let bias = 0;
let scene = null;
let tickFrame = 0;
let tickLast = 0;
function setAccent(accent) {
if (accent === (root.dataset.accent || ACCENTS[0])) return;
if (accent === ACCENTS[0]) delete root.dataset.accent;
else root.dataset.accent = accent;
}
function morphPath(morph, progress, size = 1) {
const digits = size === 1 ? 4 : 2;
const at = (cubic, j) => ((cubic[j] + (cubic[j + 8] - cubic[j]) * progress) * size).toFixed(digits);
let d = `M${at(morph[0], 0)} ${at(morph[0], 1)}`;
for (const cubic of morph) {
d += `C${at(cubic, 2)} ${at(cubic, 3)} ${at(cubic, 4)} ${at(cubic, 5)} ${at(cubic, 6)} ${at(cubic, 7)}`;
}
return `${d}Z`;
}
// 从第 index 个形状出发的那段形变,通向 index + 1。
function morphOf(index) {
if (index === 0) return START_MORPH;
if (index === 1) return ENTER_MORPH;
return CYCLE_MORPHS[(index - 2) % CYCLE_MORPHS.length];
}
// Flutter 的 easeOutBack 与 easeInCubic,与应用里按钮形变用的曲线一致。
function easeOutBack(t) {
return 1 + 2.70158 * (t - 1) ** 3 + 1.70158 * (t - 1) ** 2;
}
function easeInCubic(t) {
return t * t * t;
}
function unit(value) {
return Math.min(Math.max(value, 0), 1);
}
function smooth(value) {
const t = unit(value);
return t * t * (3 - 2 * t);
}
// 一次只走一个形状,跳过多格时(例如点锚点)直接落位。
function nextShape(from, target) {
const gap = target - from;
return Math.abs(gap) > 3 ? target : from + Math.sign(gap);
}
function tick(now) {
const step = (now - tickLast) / MORPH_MS;
tickLast = now;
if (shape.t < 1) {
shape.t = Math.min(1, shape.t + step);
const eased = shape.to === 0 ? 1 - easeInCubic(1 - shape.t) : easeOutBack(shape.t);
shape.pos = shape.origin + (shape.to - shape.origin) * eased;
if (shape.t === 1 && shape.to !== shape.target) {
shape.origin = shape.to;
shape.to = nextShape(shape.to, shape.target);
shape.t = 0;
}
}
drawEmblem();
tickFrame = shape.t < 1 ? requestAnimationFrame(tick) : 0;
}
function animate() {
if (tickFrame) return;
tickLast = performance.now();
tickFrame = requestAnimationFrame(tick);
}
function aimShape(target) {
if (target === shape.target) return; shape.target = target;
setAccent(ACCENTS[target <= 1 ? 0 : (target - 1) % ACCENTS.length]);
if (reducedMotion.matches) {
shape.pos = shape.origin = shape.to = target;
shape.t = 1;
drawEmblem();
return;
}
const heading = Math.sign(shape.to - shape.pos);
if (shape.t < 1 && heading !== 0 && heading === Math.sign(target - shape.pos)) return;
shape.origin = shape.pos;
shape.to = nextShape(target > shape.pos ? Math.floor(shape.pos) : Math.ceil(shape.pos), target);
shape.t = 0;
animate();
}
function pageTop(node) {
let top = 0;
for (; node; node = node.offsetParent) top += node.offsetTop;
return top;
}
function pageLeft(node) {
let left = 0;
for (; node; node = node.offsetParent) left += node.offsetLeft;
return left;
}
// 图标盒子的最终位置是左栏;首屏那一点是它出发的位置。宽屏下左栏 sticky 在视口顶部,
// 所以图标在原地放大并平移到左边。
function measure() {
const emblem = $('#emblem');
const spot = $('#spot');
const size = emblem.offsetWidth;
const docks = wide.matches;
const dockTop = docks ? 0 : pageTop($('#dock'));
scene = {
vh: innerHeight,
wide: docks,
dx: pageLeft(spot) + spot.offsetWidth / 2 - (emblem.offsetLeft + size / 2),
dy: pageTop(spot) + spot.offsetHeight / 2 - (dockTop + emblem.offsetTop + size / 2),
scale: spot.offsetWidth / size,
sections: ['#features', '#download', '#changelog'].map((id) => pageTop($(id))),
lastScroll: Math.max(0, scrollY),
part: -1,
docking: 0,
spin: 0,
shapeKey: null,
};
emblem.classList.add('ready');
}
function drawEmblem() {
const lower = Math.max(0, Math.floor(shape.pos));
const shapeKey = shape.pos.toFixed(4);
if (shapeKey !== scene.shapeKey) {
scene.shapeKey = shapeKey;
$('#emblem-path').setAttribute('d', morphPath(morphOf(lower), shape.pos - lower));
}
// 每段形变顺带转九分之一圈,再叠上滚动本身带来的旋转。只转形状,箭头保持竖直。
const turn = scene.spin + 40 * shape.pos;
$('.emblem-shape').style.transform = `rotate(${turn.toFixed(2)}deg)`;
// 上传箭头只在首屏出现:往下滑就淡出,图标回到纯色形状,停靠到左栏时也一样。
const fade = unit((scene.lastScroll - scene.vh * 0.06) / (scene.vh * 0.18));
$('.emblem-glyph').style.opacity = String(1 - fade);
}
// 滚动位置对应的形状序号:首屏是起始形状(圆),之后每 STEP_VH 一格。
function targetFor(scroll) {
const hero = innerHeight * HERO_VH;
return scroll < hero ? 0 : 2 + Math.floor((scroll - hero) / (innerHeight * STEP_VH));
}
function moveEmblem(scroll) {
const { vh } = scene;
const still = reducedMotion.matches;
const travel = (scroll - vh * 0.1) / (vh * (DOCKED_VH - 0.1));
const docking = scene.wide && (still ? (travel >= 0.8 ? 1 : 0) : smooth(travel));
const grow = scene.wide ? (still ? docking : smooth(scroll / (vh * 0.25))) : still ? 0 : unit(scroll / (vh * 0.6));
scene.docking = docking;
scene.spin = still ? 0 : (scroll / vh) * 30;
const scale = scene.wide ? scene.scale + (1 - scene.scale) * grow : scene.scale * (1 + 0.15 * grow);
const emblem = $('#emblem');
const x = (scene.dx * (1 - docking)).toFixed(1);
const y = (scene.dy * (1 - docking)).toFixed(1);
emblem.style.transform = `translate(${x}px, ${y}px) scale(${scale.toFixed(4)})`;
// style.css 用 data-part 切换左栏标题、图标里的序号,以及右侧内容的入场。
let part = 0;
if (scene.wide && docking > 0.9) {
const line = scroll + vh * 0.45;
part = 1;
if (line >= scene.sections[1]) part = 2;
if (line >= scene.sections[2]) part = 3;
}
if (part !== scene.part) {
scene.part = part;
root.dataset.part = String(part);
}
aimShape(targetFor(scroll) + bias);
// 更新日志回到品牌蓝:那里用颜色区分「新功能 / 问题修复」,绿色会撞色。
if (part === 3) setAccent(ACCENTS[0]);
drawEmblem();
}
// 点上传按钮:图标换到下一个形状(弹簧 easeOutBack),箭头做一次「往上送」的小动画。
function popEmblem() {
if (reducedMotion.matches) return;
const emblem = $('#emblem');
emblem.classList.remove('pop');
void emblem.offsetWidth;
emblem.classList.add('pop');
clearTimeout(emblem._popTimer);
emblem._popTimer = setTimeout(() => emblem.classList.remove('pop'), 900);
}
function playUpload() {
bias += 1;
aimShape(targetFor(Math.max(0, scrollY)) + bias);
popEmblem();
}
// 滚动驱动的 CSS 动画在 120 Hz 的 Android WebView 上会卡,所以图标和首屏文字跟着滚动位置由脚本摆放。
function followScroll() {
const scroll = Math.max(0, scrollY);
const { vh } = scene;
const delta = scroll - scene.lastScroll;
scene.lastScroll = scroll;
const bar = $('#bar');
if (scroll < vh * 0.1 || delta < -6) bar.classList.remove('away');
else if (delta > 6) bar.classList.add('away');
moveEmblem(scroll);
const copy = $('#hero-copy');
if (reducedMotion.matches) {
copy.style.visibility = scene.docking === 1 ? 'hidden' : '';
return;
}
const fade = unit((scroll - vh * 0.06) / (vh * 0.26));
copy.style.opacity = fade > 0 ? String(1 - fade) : '';
copy.style.transform = fade > 0 ? `translateY(${(-scroll * 0.2).toFixed(1)}px)` : '';
copy.style.visibility = fade === 1 ? 'hidden' : '';
$('#scroll-hint').style.opacity = String(Math.max(0, 1 - scroll / (vh * 0.15)));
}
function relayout() {
measure();
followScroll();
}
function start() {
root.style.setProperty('--circle', `path('${morphPath(START_MORPH, 0, 40)}')`);
root.style.setProperty('--cookie', `path('${morphPath(START_MORPH, 1, 40)}')`);
relayout();
addEventListener('scroll', followScroll, { passive: true });
new ResizeObserver(relayout).observe(document.body);
let resizeFrame = 0;
addEventListener('resize', () => {
cancelAnimationFrame(resizeFrame);
resizeFrame = requestAnimationFrame(relayout);
});
reducedMotion.addEventListener('change', () => {
$('#hero-copy').removeAttribute('style');
$('#scroll-hint').style.removeProperty('opacity');
relayout();
});
document.fonts.ready.then(relayout);
$('#emblem').addEventListener('click', playUpload);
}
start();
})();