motion-hub 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +44 -0
- package/README.md +141 -0
- package/bin/install-skill.mjs +39 -0
- package/package.json +48 -0
- package/skill/motion-hub/SKILL.md +127 -0
- package/skill/motion-hub/effects/contour.html +279 -0
- package/skill/motion-hub/effects/dots-quiet.html +257 -0
- package/skill/motion-hub/effects/dots.html +263 -0
- package/skill/motion-hub/effects/flow.html +253 -0
- package/skill/motion-hub/effects/follow-tip.html +241 -0
- package/skill/motion-hub/effects/glow-card.html +236 -0
- package/skill/motion-hub/effects/grid.html +266 -0
- package/skill/motion-hub/effects/intro.html +302 -0
- package/skill/motion-hub/effects/pipeline.html +257 -0
- package/skill/motion-hub/effects/pull.html +373 -0
- package/skill/motion-hub/effects/scramble.html +248 -0
- package/skill/motion-hub/effects/sun-moon.html +235 -0
- package/skill/motion-hub/effects/theme-blinds.html +381 -0
- package/skill/motion-hub/effects/theme-circle.html +381 -0
- package/skill/motion-hub/effects/theme-dots.html +381 -0
- package/skill/motion-hub/effects/theme-fade.html +381 -0
- package/skill/motion-hub/effects/theme-flip.html +381 -0
- package/skill/motion-hub/effects/theme-halftone.html +381 -0
- package/skill/motion-hub/effects/theme-ink.html +381 -0
- package/skill/motion-hub/effects/theme-iris.html +381 -0
- package/skill/motion-hub/effects/theme-slice.html +381 -0
- package/skill/motion-hub/effects/theme-soft.html +381 -0
- package/skill/motion-hub/effects/theme-wave.html +381 -0
- package/skill/motion-hub/index.json +667 -0
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<!--
|
|
3
|
+
「跟随信息卡」 · Motion Hub · MIT + Commons Clause(可免费用于任何项目,不能售卖效果本身)
|
|
4
|
+
|
|
5
|
+
【怎么用】
|
|
6
|
+
这个文件可以直接双击打开,先看效果。
|
|
7
|
+
想放进自己的项目:把整个文件交给 AI(Claude / ChatGPT / Cursor 等),再说一句你的技术栈和想要的调整,
|
|
8
|
+
例如“把这个效果改写成 React 组件,放在首屏当背景,颜色换成我的品牌色”。下面的说明就是写给它看的。
|
|
9
|
+
|
|
10
|
+
【效果】
|
|
11
|
+
悬停在列表某一行时,一张信息卡带着一点“拖拽感”跟随指针;靠近右边缘时自动翻到指针左侧。
|
|
12
|
+
|
|
13
|
+
【实现原理】
|
|
14
|
+
- 每帧判断指针在哪一行,更新卡片内容。
|
|
15
|
+
- 目标位置 = 指针 + 偏移(右侧放不下时改到左侧),当前位置每帧向目标插值 18%,形成拖拽感。
|
|
16
|
+
- 用 CSS translate 移动,显隐用透明度和缩放过渡。
|
|
17
|
+
|
|
18
|
+
【可以调的参数】
|
|
19
|
+
- 插值系数 0.18
|
|
20
|
+
- 偏移 18px / 16px
|
|
21
|
+
|
|
22
|
+
【注意】
|
|
23
|
+
- 卡片要 pointer-events: none,不然会挡住指针。
|
|
24
|
+
- 颜色都来自 CSS 变量(paper / ink / dim / rule / accent),换成项目自己的颜色即可。
|
|
25
|
+
- 要处理 prefers-reduced-motion:系统要求减少动态效果时只显示静止画面。
|
|
26
|
+
- 组件卸载时要停掉动画循环、移除事件监听(mount 返回的 destroy)。
|
|
27
|
+
|
|
28
|
+
【opts 参数】(mount 的第二个参数,不传就用默认值;文件末尾的 mount 调用里是你在 Motion Hub 上调好的值)
|
|
29
|
+
- lag:跟随灵敏度,默认 0.18(0.04–1)
|
|
30
|
+
- offset:离指针距离,默认 18(0–60px)
|
|
31
|
+
|
|
32
|
+
【代码结构】
|
|
33
|
+
- <style>:配色变量和这个效果用到的样式
|
|
34
|
+
- 第一段 <script>:MH 核心(注册表、噪声、缓动;canvasHost 负责画布尺寸、指针坐标、动画循环和暂停)
|
|
35
|
+
- 第二段 <script>:效果本身,核心是 MH.register 里的 mount(el, opts),返回 { pause, resume, destroy }
|
|
36
|
+
- 最后一段 <script>:把效果挂到 #fx 上并启动
|
|
37
|
+
-->
|
|
38
|
+
<html lang="zh-CN">
|
|
39
|
+
<head>
|
|
40
|
+
<meta charset="utf-8">
|
|
41
|
+
<meta name="viewport" content="width=device-width,initial-scale=1">
|
|
42
|
+
<title>跟随信息卡</title>
|
|
43
|
+
<style>
|
|
44
|
+
/* Motion Hub 效果样式:配色变量(Acid:浅色暖灰 + 紫罗兰,深色近黑 + 酸橙绿)+ 各效果用到的样式 + 整页过渡遮罩。按“@css 名称”标记分段,复制代码时只带用到的段。 */
|
|
45
|
+
:root{
|
|
46
|
+
--paper:#F1F0EC;--paper2:#E4E2DC;--ink:#141316;--body:#3A383F;--dim:#85828C;--rule:#D6D3CC;--accent:#6B4EFF;--on-accent:#fff;--hi:#C6F21E;
|
|
47
|
+
--serif:"Songti SC","STSong","Noto Serif SC",serif;
|
|
48
|
+
--sans:"PingFang SC","Helvetica Neue","Noto Sans SC",sans-serif;
|
|
49
|
+
--latin:"Iowan Old Style",Palatino,Georgia,serif;
|
|
50
|
+
--mono:"SF Mono",Menlo,monospace;
|
|
51
|
+
}
|
|
52
|
+
[data-theme="night"]{--paper:#111113;--paper2:#1B1B1F;--ink:#F2F1F5;--body:#C4C2CC;--dim:#7E7B88;--rule:#2A2A30;--accent:#C6F21E;--on-accent:#111113;--hi:#8E75FF}
|
|
53
|
+
[data-theme="day"]{--paper:#F1F0EC;--paper2:#E4E2DC;--ink:#141316;--body:#3A383F;--dim:#85828C;--rule:#D6D3CC;--accent:#6B4EFF;--on-accent:#fff;--hi:#C6F21E}
|
|
54
|
+
|
|
55
|
+
/* 跟随信息卡 */
|
|
56
|
+
.ft{position:absolute;inset:0;padding:8cqh 8cqw;display:flex;flex-direction:column;justify-content:center}
|
|
57
|
+
.ft-row{display:flex;gap:3cqw;align-items:baseline;padding:2.4cqh 0;border-top:1px solid var(--rule)}
|
|
58
|
+
.ft-row:last-of-type{border-bottom:1px solid var(--rule)}
|
|
59
|
+
.ft-row span{font:500 1.6cqw/1 var(--mono);color:var(--dim)}
|
|
60
|
+
.ft-row b{font:700 6cqh/1.1 var(--serif);transition:transform .4s cubic-bezier(.3,.8,.2,1),color .2s}
|
|
61
|
+
.ft-row.hot b{transform:translateX(1.4cqw);color:var(--accent)}
|
|
62
|
+
.ft-tip{position:absolute;left:0;top:0;width:max(180px,24cqw);padding:12px 14px;border:1px solid var(--ink);background:var(--paper);pointer-events:none;opacity:0;transform:scale(.92);transition:opacity .2s,transform .25s cubic-bezier(.3,.8,.2,1)}
|
|
63
|
+
.ft-tip.on{opacity:1;transform:scale(1)}
|
|
64
|
+
.ft-tip em{display:block;font:500 11px/1 var(--mono);font-style:normal;letter-spacing:.1em;color:var(--accent)}
|
|
65
|
+
.ft-tip i{display:block;margin-top:8px;font:500 12px/1.5 var(--mono);font-style:normal;color:var(--body)}
|
|
66
|
+
html,body{height:100%;margin:0;background:var(--paper)}
|
|
67
|
+
#fx{position:relative;height:100vh;overflow:hidden;container-type:size}
|
|
68
|
+
</style>
|
|
69
|
+
</head>
|
|
70
|
+
<body>
|
|
71
|
+
<div id="fx"></div>
|
|
72
|
+
<script>
|
|
73
|
+
// Motion Hub 核心:注册表 + 公共工具。所有效果文件都是普通 <script>(不是 ES 模块),
|
|
74
|
+
// 这样直接双击 index.html(file://)也能运行。每个效果调用 MH.register({...}) 把自己登记进来。
|
|
75
|
+
//
|
|
76
|
+
// 效果对象的约定:
|
|
77
|
+
// id 唯一标识(小写短横线)
|
|
78
|
+
// name 中文名
|
|
79
|
+
// cat 分类:背景 / 过渡 / 翻页 / 片头 / 交互
|
|
80
|
+
// tech 用到的技术(展示用)
|
|
81
|
+
// desc 一句话说明
|
|
82
|
+
// mount(el, opts) → { pause(), resume(), destroy() }
|
|
83
|
+
// el 是一个已经有尺寸的容器;opts.expanded 为 true 表示全屏体验(关掉幽灵光标、允许滚轮等)
|
|
84
|
+
// params 可调参数(可选),实验台据此生成参数面板,每项 { k, label, type, def, ... }:
|
|
85
|
+
// type 'range':数值滑块,带 min / max / step,可选 unit;'text':单行文字;'lines':多行文字,一行一项
|
|
86
|
+
// mount 拿到的 opts 已经按 def 补齐(register 里包了一层),效果代码直接读 opts.k 即可
|
|
87
|
+
window.MH = (() => {
|
|
88
|
+
const effects = [];
|
|
89
|
+
// 记下效果来自哪个文件(复制代码时只带这个文件)
|
|
90
|
+
const register = e => {
|
|
91
|
+
const src = document.currentScript && document.currentScript.src; e.file = e.file || (src ? src.split('/').pop() : '');
|
|
92
|
+
if (e.params) { const m = e.mount; e.mount = (el, opts = {}) => m.call(e, el, { ...defaults(e), ...opts }); }
|
|
93
|
+
effects.push(e);
|
|
94
|
+
};
|
|
95
|
+
const defaults = e => Object.fromEntries((e.params || []).map(p => [p.k, p.def]));
|
|
96
|
+
// 'lines' 参数:数组原样返回;字符串按行拆开,去掉空行
|
|
97
|
+
const lines = v => Array.isArray(v) ? v : String(v || '').split('\n').map(x => x.trim()).filter(Boolean);
|
|
98
|
+
const still = () => matchMedia('(prefers-reduced-motion: reduce)').matches;
|
|
99
|
+
const clamp = (v, a = 0, b = 1) => Math.max(a, Math.min(b, v));
|
|
100
|
+
const lerp = (a, b, k) => a + (b - a) * k;
|
|
101
|
+
const ease = {
|
|
102
|
+
in: t => t * t * t,
|
|
103
|
+
out: t => 1 - (1 - t) ** 3,
|
|
104
|
+
inOut: t => t < .5 ? 4 * t * t * t : 1 - (-2 * t + 2) ** 3 / 2,
|
|
105
|
+
back: t => { const c = 1.4; return 1 + (c + 1) * (t - 1) ** 3 + c * (t - 1) ** 2; },
|
|
106
|
+
};
|
|
107
|
+
const hash = i => { const x = Math.sin(i * 127.1 + 311.7) * 43758.5453; return x - Math.floor(x); };
|
|
108
|
+
|
|
109
|
+
// 二维 Perlin 噪声,seed 决定排列表
|
|
110
|
+
function perlin(seed = 1) {
|
|
111
|
+
const P = new Uint8Array(512), p = [...Array(256).keys()];
|
|
112
|
+
let s = seed;
|
|
113
|
+
for (let i = 255; i > 0; i--) { s = (s * 16807) % 2147483647; const j = s % (i + 1); [p[i], p[j]] = [p[j], p[i]]; }
|
|
114
|
+
for (let i = 0; i < 512; i++) P[i] = p[i & 255];
|
|
115
|
+
const fade = t => t * t * t * (t * (t * 6 - 15) + 10), grad = (h, x, y) => ((h & 1) ? -x : x) + ((h & 2) ? -y : y);
|
|
116
|
+
return (x, y) => {
|
|
117
|
+
const X = Math.floor(x) & 255, Y = Math.floor(y) & 255; x -= Math.floor(x); y -= Math.floor(y);
|
|
118
|
+
const u = fade(x), v = fade(y), a = P[X] + Y, b = P[X + 1] + Y;
|
|
119
|
+
const l1 = grad(P[a], x, y) + u * (grad(P[b], x - 1, y) - grad(P[a], x, y));
|
|
120
|
+
const l2 = grad(P[a + 1], x, y - 1) + u * (grad(P[b + 1], x - 1, y - 1) - grad(P[a + 1], x, y - 1));
|
|
121
|
+
return l1 + v * (l2 - l1);
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// 画布按设备像素比放大,画的时候仍用 CSS 像素坐标
|
|
126
|
+
function fit(cv, w, h) {
|
|
127
|
+
const dpr = Math.min(devicePixelRatio || 1, 2);
|
|
128
|
+
cv.width = Math.round(w * dpr); cv.height = Math.round(h * dpr);
|
|
129
|
+
const ctx = cv.getContext('2d'); ctx.setTransform(dpr, 0, 0, dpr, 0, 0); return ctx;
|
|
130
|
+
}
|
|
131
|
+
// 从容器读主题色(--paper / --ink / --dim / --rule / --accent)
|
|
132
|
+
function colors(el) {
|
|
133
|
+
const cs = getComputedStyle(el), v = n => cs.getPropertyValue(n).trim();
|
|
134
|
+
const t = el.closest('[data-theme]');
|
|
135
|
+
return { paper: v('--paper'), ink: v('--ink'), dim: v('--dim'), rule: v('--rule'), accent: v('--accent'), night: !!t && t.dataset.theme === 'night' };
|
|
136
|
+
}
|
|
137
|
+
// 一段时长 ms 的补间,fn(k) 中 k 从 0 到 1;减少动态效果时直接到终点
|
|
138
|
+
const tween = (ms, fn) => new Promise(res => {
|
|
139
|
+
if (still()) { fn(1); return res(); }
|
|
140
|
+
const t0 = performance.now();
|
|
141
|
+
const f = now => { const k = clamp((now - t0) / ms); fn(k); k < 1 ? requestAnimationFrame(f) : res(); };
|
|
142
|
+
requestAnimationFrame(f);
|
|
143
|
+
});
|
|
144
|
+
// 尺寸缩放系数:全屏约 1,缩略图约 0.45,让小画面像“整页缩小”而不是挤满细节
|
|
145
|
+
const scaleOf = W => clamp(W / 1200, .45, 1);
|
|
146
|
+
|
|
147
|
+
// 幽灵光标:没人操作时,沿一条利萨如曲线在容器里慢慢移动,让缩略图自己演示交互
|
|
148
|
+
const ghostAt = (now, W, H, seed = 0) => {
|
|
149
|
+
const t = now / 1000 + seed * 7;
|
|
150
|
+
return { x: W * (.5 + .32 * Math.sin(t * .55) * Math.cos(t * .21)), y: H * (.5 + .3 * Math.sin(t * .83 + 1.3)) };
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
// 效果宿主:(可选)建画布、跟随尺寸、换算局部指针坐标、管动画循环和暂停。
|
|
154
|
+
// setup(h) 返回 { resize(W,H), frame(now), down(x,y), theme(), ghostClick },h 提供 ctx / W / H / P / s / colors()
|
|
155
|
+
// ghostClick: false 表示幽灵光标不替它“点击”(例如片头,点一下会重播)
|
|
156
|
+
function canvasHost(el, opts, setup, { canvas = true } = {}) {
|
|
157
|
+
const cv = canvas ? document.createElement('canvas') : null;
|
|
158
|
+
if (cv) { cv.className = 'mh-canvas'; el.append(cv); }
|
|
159
|
+
let ctx, W = 0, H = 0, raf = 0, running = false, lastReal = -1e9, lastGhostClick = 0;
|
|
160
|
+
const P = { x: -9999, y: -9999, real: false };
|
|
161
|
+
const seed = hash(effects.length + el.childElementCount);
|
|
162
|
+
const h = { cv, get ctx() { return ctx; }, get W() { return W; }, get H() { return H; }, P, s: 1, colors: () => colors(el), expanded: !!opts.expanded };
|
|
163
|
+
const api = setup(h);
|
|
164
|
+
const size = () => {
|
|
165
|
+
const r = el.getBoundingClientRect(); if (!r.width || !r.height) return;
|
|
166
|
+
W = r.width; H = r.height; h.s = scaleOf(W); if (cv) ctx = fit(cv, W, H); api.resize && api.resize(W, H);
|
|
167
|
+
if (!running) api.frame(performance.now());
|
|
168
|
+
};
|
|
169
|
+
const ro = new ResizeObserver(size); ro.observe(el);
|
|
170
|
+
const local = e => { const r = el.getBoundingClientRect(); return [e.clientX - r.left, e.clientY - r.top]; };
|
|
171
|
+
const onMove = e => { [P.x, P.y] = local(e); P.real = true; lastReal = performance.now(); };
|
|
172
|
+
const onLeave = () => { P.real = false; P.x = P.y = -9999; };
|
|
173
|
+
const onDown = e => { if (e.target.closest('a, button')) return; lastReal = performance.now(); const [x, y] = local(e); api.down && api.down(x, y); };
|
|
174
|
+
el.addEventListener('pointermove', onMove); el.addEventListener('pointerleave', onLeave); el.addEventListener('pointerdown', onDown);
|
|
175
|
+
const mo = new MutationObserver(() => { api.theme && api.theme(); if (!running) api.frame(performance.now()); });
|
|
176
|
+
mo.observe(document.documentElement, { attributes: true, attributeFilter: ['data-theme', 'data-palette'] });
|
|
177
|
+
const tick = now => {
|
|
178
|
+
if (!running) return;
|
|
179
|
+
// 缩略图里 2.5 秒没人碰,就让幽灵光标接管;偶尔“点”一下展示点击效果
|
|
180
|
+
if (!h.expanded && now - lastReal > 2500) {
|
|
181
|
+
const g = ghostAt(now, W, H, seed); P.x = g.x; P.y = g.y;
|
|
182
|
+
if (api.down && api.ghostClick !== false && now - lastGhostClick > 4200) { lastGhostClick = now; api.down(g.x, g.y); }
|
|
183
|
+
}
|
|
184
|
+
if (W && H) api.frame(now);
|
|
185
|
+
raf = requestAnimationFrame(tick);
|
|
186
|
+
};
|
|
187
|
+
size();
|
|
188
|
+
return {
|
|
189
|
+
pause() { running = false; cancelAnimationFrame(raf); },
|
|
190
|
+
resume() { if (running) return; if (still()) { api.frame(performance.now()); return; } running = true; raf = requestAnimationFrame(tick); },
|
|
191
|
+
destroy() { this.pause(); ro.disconnect(); mo.disconnect(); el.removeEventListener('pointermove', onMove); el.removeEventListener('pointerleave', onLeave); el.removeEventListener('pointerdown', onDown); cv && cv.remove(); },
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// 让 CSS 能对自定义属性做补间(遮罩半径等)
|
|
196
|
+
if (window.CSS && CSS.registerProperty) {
|
|
197
|
+
for (const name of ['--vr', '--vw']) { try { CSS.registerProperty({ name, syntax: '<length>', inherits: false, initialValue: '0px' }); } catch (e) { /* 已注册 */ } }
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
return { effects, register, defaults, lines, still, clamp, lerp, ease, hash, perlin, fit, colors, tween, scaleOf, ghostAt, canvasHost };
|
|
201
|
+
})();
|
|
202
|
+
</script>
|
|
203
|
+
<script>
|
|
204
|
+
// 小交互:DOM + CSS,没有画布。用 canvasHost 的无画布模式拿到“真实指针或幽灵光标”的位置。
|
|
205
|
+
|
|
206
|
+
// ───── 跟随光标的信息卡:悬停列表行时,一张小卡片带一点延迟地跟着指针;靠近右边缘时翻到指针左侧 ─────
|
|
207
|
+
MH.register({
|
|
208
|
+
id: 'follow-tip', name: '跟随信息卡', cat: '交互', tech: 'Lerp · Pointer Tracking',
|
|
209
|
+
desc: '悬停在某一行时,信息卡带着一点“拖拽感”跟随指针,靠近边缘会自动翻到另一侧。',
|
|
210
|
+
params: [
|
|
211
|
+
{ k: 'lag', label: '跟随灵敏度', type: 'range', min: 0.04, max: 1, step: 0.01, def: 0.18 },
|
|
212
|
+
{ k: 'offset', label: '离指针距离', type: 'range', min: 0, max: 60, step: 1, def: 18, unit: 'px' },
|
|
213
|
+
],
|
|
214
|
+
mount(el, opts) {
|
|
215
|
+
const ROWS = [['01', '等高线', 'Canvas · Marching Squares'], ['02', '磁性点阵', 'Canvas · 弹簧阻尼'], ['03', '风场', 'Canvas · 粒子'], ['04', '拉扯翻页', 'DOM 切条']];
|
|
216
|
+
el.insertAdjacentHTML('beforeend', `<div class="ft">${ROWS.map(([n, t]) => `<div class="ft-row"><span>${n}</span><b>${t}</b></div>`).join('')}<div class="ft-tip"><em></em><i></i></div></div>`);
|
|
217
|
+
const box = el.lastElementChild, rows = [...box.querySelectorAll('.ft-row')], tip = box.querySelector('.ft-tip');
|
|
218
|
+
const p = { x: 0, y: 0 };
|
|
219
|
+
return MH.canvasHost(el, opts, h => ({
|
|
220
|
+
frame() {
|
|
221
|
+
const r0 = el.getBoundingClientRect(), W = h.W;
|
|
222
|
+
const i = rows.findIndex(r => { const b = r.getBoundingClientRect(), y = h.P.y + r0.top, x = h.P.x + r0.left; return y >= b.top && y <= b.bottom && x >= b.left && x <= b.right; });
|
|
223
|
+
tip.classList.toggle('on', i >= 0);
|
|
224
|
+
rows.forEach((r, k) => r.classList.toggle('hot', k === i));
|
|
225
|
+
if (i >= 0) { tip.querySelector('em').textContent = `${ROWS[i][0]} / 0${ROWS.length}`; tip.querySelector('i').textContent = ROWS[i][2]; }
|
|
226
|
+
const tw = tip.offsetWidth, tx = h.P.x + (h.P.x > W - tw - 30 ? -tw - opts.offset : opts.offset), ty = h.P.y + opts.offset;
|
|
227
|
+
const k = MH.still() ? 1 : opts.lag; p.x += (tx - p.x) * k; p.y += (ty - p.y) * k;
|
|
228
|
+
tip.style.translate = `${p.x}px ${p.y}px`;
|
|
229
|
+
},
|
|
230
|
+
}), { canvas: false });
|
|
231
|
+
},
|
|
232
|
+
});
|
|
233
|
+
</script>
|
|
234
|
+
<script>
|
|
235
|
+
MH.effects.find(e => e.id === 'follow-tip').mount(document.getElementById('fx'), {
|
|
236
|
+
"lag": 0.18,
|
|
237
|
+
"offset": 18
|
|
238
|
+
}).resume();
|
|
239
|
+
</script>
|
|
240
|
+
</body>
|
|
241
|
+
</html>
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<!--
|
|
3
|
+
「边框高光卡片」 · Motion Hub · MIT + Commons Clause(可免费用于任何项目,不能售卖效果本身)
|
|
4
|
+
|
|
5
|
+
【怎么用】
|
|
6
|
+
这个文件可以直接双击打开,先看效果。
|
|
7
|
+
想放进自己的项目:把整个文件交给 AI(Claude / ChatGPT / Cursor 等),再说一句你的技术栈和想要的调整,
|
|
8
|
+
例如“把这个效果改写成 React 组件,放在首屏当背景,颜色换成我的品牌色”。下面的说明就是写给它看的。
|
|
9
|
+
|
|
10
|
+
【效果】
|
|
11
|
+
指针在卡片上移动时,边框沿着指针亮起一段光;卡片同时朝指针方向轻轻倾斜。文字底下不铺颜色。
|
|
12
|
+
|
|
13
|
+
【实现原理】
|
|
14
|
+
- 卡片的 ::before 伪元素铺一层 radial-gradient,圆心跟随 CSS 变量 --mx / --my(指针相对卡片的位置)。
|
|
15
|
+
- 用 mask: linear-gradient content-box, linear-gradient + mask-composite: exclude,只露出 1.5px 的边框。
|
|
16
|
+
- 倾斜:rotateX((0.5 - y) × 8deg) rotateY((x - 0.5) × 10deg),父元素加 perspective。
|
|
17
|
+
|
|
18
|
+
【可以调的参数】
|
|
19
|
+
- 光斑半径 260px
|
|
20
|
+
- 倾斜幅度 8° / 10°
|
|
21
|
+
- 回正过渡 0.5s
|
|
22
|
+
|
|
23
|
+
【注意】
|
|
24
|
+
- WebKit 需要 -webkit-mask-composite: xor。
|
|
25
|
+
- 颜色都来自 CSS 变量(paper / ink / dim / rule / accent),换成项目自己的颜色即可。
|
|
26
|
+
- 要处理 prefers-reduced-motion:系统要求减少动态效果时只显示静止画面。
|
|
27
|
+
- 组件卸载时要停掉动画循环、移除事件监听(mount 返回的 destroy)。
|
|
28
|
+
|
|
29
|
+
【opts 参数】(mount 的第二个参数,不传就用默认值;文件末尾的 mount 调用里是你在 Motion Hub 上调好的值)
|
|
30
|
+
- tilt:倾斜角度,默认 10(0–25°)
|
|
31
|
+
|
|
32
|
+
【代码结构】
|
|
33
|
+
- <style>:配色变量和这个效果用到的样式
|
|
34
|
+
- 第一段 <script>:MH 核心(注册表、噪声、缓动;canvasHost 负责画布尺寸、指针坐标、动画循环和暂停)
|
|
35
|
+
- 第二段 <script>:效果本身,核心是 MH.register 里的 mount(el, opts),返回 { pause, resume, destroy }
|
|
36
|
+
- 最后一段 <script>:把效果挂到 #fx 上并启动
|
|
37
|
+
-->
|
|
38
|
+
<html lang="zh-CN">
|
|
39
|
+
<head>
|
|
40
|
+
<meta charset="utf-8">
|
|
41
|
+
<meta name="viewport" content="width=device-width,initial-scale=1">
|
|
42
|
+
<title>边框高光卡片</title>
|
|
43
|
+
<style>
|
|
44
|
+
/* Motion Hub 效果样式:配色变量(Acid:浅色暖灰 + 紫罗兰,深色近黑 + 酸橙绿)+ 各效果用到的样式 + 整页过渡遮罩。按“@css 名称”标记分段,复制代码时只带用到的段。 */
|
|
45
|
+
:root{
|
|
46
|
+
--paper:#F1F0EC;--paper2:#E4E2DC;--ink:#141316;--body:#3A383F;--dim:#85828C;--rule:#D6D3CC;--accent:#6B4EFF;--on-accent:#fff;--hi:#C6F21E;
|
|
47
|
+
--serif:"Songti SC","STSong","Noto Serif SC",serif;
|
|
48
|
+
--sans:"PingFang SC","Helvetica Neue","Noto Sans SC",sans-serif;
|
|
49
|
+
--latin:"Iowan Old Style",Palatino,Georgia,serif;
|
|
50
|
+
--mono:"SF Mono",Menlo,monospace;
|
|
51
|
+
}
|
|
52
|
+
[data-theme="night"]{--paper:#111113;--paper2:#1B1B1F;--ink:#F2F1F5;--body:#C4C2CC;--dim:#7E7B88;--rule:#2A2A30;--accent:#C6F21E;--on-accent:#111113;--hi:#8E75FF}
|
|
53
|
+
[data-theme="day"]{--paper:#F1F0EC;--paper2:#E4E2DC;--ink:#141316;--body:#3A383F;--dim:#85828C;--rule:#D6D3CC;--accent:#6B4EFF;--on-accent:#fff;--hi:#C6F21E}
|
|
54
|
+
|
|
55
|
+
/* 边框高光卡片 */
|
|
56
|
+
.gc-wrap{position:absolute;inset:0;display:grid;grid-template-columns:repeat(3,1fr);gap:3cqw;align-items:center;padding:0 7cqw;perspective:900px}
|
|
57
|
+
.gc{position:relative;aspect-ratio:3/4;padding:3cqw;border:1px solid var(--rule);background:var(--paper);display:flex;flex-direction:column;transition:transform .5s cubic-bezier(.3,.8,.2,1)}
|
|
58
|
+
.gc::before{content:"";position:absolute;inset:-1px;padding:1.5px;background:radial-gradient(260px circle at var(--mx,50%) var(--my,50%),var(--accent),transparent 70%);-webkit-mask:linear-gradient(#000 0 0) content-box,linear-gradient(#000 0 0);-webkit-mask-composite:xor;mask-composite:exclude;opacity:0;transition:opacity .3s}
|
|
59
|
+
.gc.hot::before{opacity:1}
|
|
60
|
+
.gc span{font:italic 400 2.4cqw/1 var(--latin);color:var(--accent)}
|
|
61
|
+
.gc b{margin-top:auto;font:900 3.2cqw/1.2 var(--serif)}
|
|
62
|
+
.gc em{margin-top:1cqw;font:500 1.6cqw/1 var(--sans);font-style:normal;color:var(--dim)}
|
|
63
|
+
html,body{height:100%;margin:0;background:var(--paper)}
|
|
64
|
+
#fx{position:relative;height:100vh;overflow:hidden;container-type:size}
|
|
65
|
+
</style>
|
|
66
|
+
</head>
|
|
67
|
+
<body>
|
|
68
|
+
<div id="fx"></div>
|
|
69
|
+
<script>
|
|
70
|
+
// Motion Hub 核心:注册表 + 公共工具。所有效果文件都是普通 <script>(不是 ES 模块),
|
|
71
|
+
// 这样直接双击 index.html(file://)也能运行。每个效果调用 MH.register({...}) 把自己登记进来。
|
|
72
|
+
//
|
|
73
|
+
// 效果对象的约定:
|
|
74
|
+
// id 唯一标识(小写短横线)
|
|
75
|
+
// name 中文名
|
|
76
|
+
// cat 分类:背景 / 过渡 / 翻页 / 片头 / 交互
|
|
77
|
+
// tech 用到的技术(展示用)
|
|
78
|
+
// desc 一句话说明
|
|
79
|
+
// mount(el, opts) → { pause(), resume(), destroy() }
|
|
80
|
+
// el 是一个已经有尺寸的容器;opts.expanded 为 true 表示全屏体验(关掉幽灵光标、允许滚轮等)
|
|
81
|
+
// params 可调参数(可选),实验台据此生成参数面板,每项 { k, label, type, def, ... }:
|
|
82
|
+
// type 'range':数值滑块,带 min / max / step,可选 unit;'text':单行文字;'lines':多行文字,一行一项
|
|
83
|
+
// mount 拿到的 opts 已经按 def 补齐(register 里包了一层),效果代码直接读 opts.k 即可
|
|
84
|
+
window.MH = (() => {
|
|
85
|
+
const effects = [];
|
|
86
|
+
// 记下效果来自哪个文件(复制代码时只带这个文件)
|
|
87
|
+
const register = e => {
|
|
88
|
+
const src = document.currentScript && document.currentScript.src; e.file = e.file || (src ? src.split('/').pop() : '');
|
|
89
|
+
if (e.params) { const m = e.mount; e.mount = (el, opts = {}) => m.call(e, el, { ...defaults(e), ...opts }); }
|
|
90
|
+
effects.push(e);
|
|
91
|
+
};
|
|
92
|
+
const defaults = e => Object.fromEntries((e.params || []).map(p => [p.k, p.def]));
|
|
93
|
+
// 'lines' 参数:数组原样返回;字符串按行拆开,去掉空行
|
|
94
|
+
const lines = v => Array.isArray(v) ? v : String(v || '').split('\n').map(x => x.trim()).filter(Boolean);
|
|
95
|
+
const still = () => matchMedia('(prefers-reduced-motion: reduce)').matches;
|
|
96
|
+
const clamp = (v, a = 0, b = 1) => Math.max(a, Math.min(b, v));
|
|
97
|
+
const lerp = (a, b, k) => a + (b - a) * k;
|
|
98
|
+
const ease = {
|
|
99
|
+
in: t => t * t * t,
|
|
100
|
+
out: t => 1 - (1 - t) ** 3,
|
|
101
|
+
inOut: t => t < .5 ? 4 * t * t * t : 1 - (-2 * t + 2) ** 3 / 2,
|
|
102
|
+
back: t => { const c = 1.4; return 1 + (c + 1) * (t - 1) ** 3 + c * (t - 1) ** 2; },
|
|
103
|
+
};
|
|
104
|
+
const hash = i => { const x = Math.sin(i * 127.1 + 311.7) * 43758.5453; return x - Math.floor(x); };
|
|
105
|
+
|
|
106
|
+
// 二维 Perlin 噪声,seed 决定排列表
|
|
107
|
+
function perlin(seed = 1) {
|
|
108
|
+
const P = new Uint8Array(512), p = [...Array(256).keys()];
|
|
109
|
+
let s = seed;
|
|
110
|
+
for (let i = 255; i > 0; i--) { s = (s * 16807) % 2147483647; const j = s % (i + 1); [p[i], p[j]] = [p[j], p[i]]; }
|
|
111
|
+
for (let i = 0; i < 512; i++) P[i] = p[i & 255];
|
|
112
|
+
const fade = t => t * t * t * (t * (t * 6 - 15) + 10), grad = (h, x, y) => ((h & 1) ? -x : x) + ((h & 2) ? -y : y);
|
|
113
|
+
return (x, y) => {
|
|
114
|
+
const X = Math.floor(x) & 255, Y = Math.floor(y) & 255; x -= Math.floor(x); y -= Math.floor(y);
|
|
115
|
+
const u = fade(x), v = fade(y), a = P[X] + Y, b = P[X + 1] + Y;
|
|
116
|
+
const l1 = grad(P[a], x, y) + u * (grad(P[b], x - 1, y) - grad(P[a], x, y));
|
|
117
|
+
const l2 = grad(P[a + 1], x, y - 1) + u * (grad(P[b + 1], x - 1, y - 1) - grad(P[a + 1], x, y - 1));
|
|
118
|
+
return l1 + v * (l2 - l1);
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// 画布按设备像素比放大,画的时候仍用 CSS 像素坐标
|
|
123
|
+
function fit(cv, w, h) {
|
|
124
|
+
const dpr = Math.min(devicePixelRatio || 1, 2);
|
|
125
|
+
cv.width = Math.round(w * dpr); cv.height = Math.round(h * dpr);
|
|
126
|
+
const ctx = cv.getContext('2d'); ctx.setTransform(dpr, 0, 0, dpr, 0, 0); return ctx;
|
|
127
|
+
}
|
|
128
|
+
// 从容器读主题色(--paper / --ink / --dim / --rule / --accent)
|
|
129
|
+
function colors(el) {
|
|
130
|
+
const cs = getComputedStyle(el), v = n => cs.getPropertyValue(n).trim();
|
|
131
|
+
const t = el.closest('[data-theme]');
|
|
132
|
+
return { paper: v('--paper'), ink: v('--ink'), dim: v('--dim'), rule: v('--rule'), accent: v('--accent'), night: !!t && t.dataset.theme === 'night' };
|
|
133
|
+
}
|
|
134
|
+
// 一段时长 ms 的补间,fn(k) 中 k 从 0 到 1;减少动态效果时直接到终点
|
|
135
|
+
const tween = (ms, fn) => new Promise(res => {
|
|
136
|
+
if (still()) { fn(1); return res(); }
|
|
137
|
+
const t0 = performance.now();
|
|
138
|
+
const f = now => { const k = clamp((now - t0) / ms); fn(k); k < 1 ? requestAnimationFrame(f) : res(); };
|
|
139
|
+
requestAnimationFrame(f);
|
|
140
|
+
});
|
|
141
|
+
// 尺寸缩放系数:全屏约 1,缩略图约 0.45,让小画面像“整页缩小”而不是挤满细节
|
|
142
|
+
const scaleOf = W => clamp(W / 1200, .45, 1);
|
|
143
|
+
|
|
144
|
+
// 幽灵光标:没人操作时,沿一条利萨如曲线在容器里慢慢移动,让缩略图自己演示交互
|
|
145
|
+
const ghostAt = (now, W, H, seed = 0) => {
|
|
146
|
+
const t = now / 1000 + seed * 7;
|
|
147
|
+
return { x: W * (.5 + .32 * Math.sin(t * .55) * Math.cos(t * .21)), y: H * (.5 + .3 * Math.sin(t * .83 + 1.3)) };
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
// 效果宿主:(可选)建画布、跟随尺寸、换算局部指针坐标、管动画循环和暂停。
|
|
151
|
+
// setup(h) 返回 { resize(W,H), frame(now), down(x,y), theme(), ghostClick },h 提供 ctx / W / H / P / s / colors()
|
|
152
|
+
// ghostClick: false 表示幽灵光标不替它“点击”(例如片头,点一下会重播)
|
|
153
|
+
function canvasHost(el, opts, setup, { canvas = true } = {}) {
|
|
154
|
+
const cv = canvas ? document.createElement('canvas') : null;
|
|
155
|
+
if (cv) { cv.className = 'mh-canvas'; el.append(cv); }
|
|
156
|
+
let ctx, W = 0, H = 0, raf = 0, running = false, lastReal = -1e9, lastGhostClick = 0;
|
|
157
|
+
const P = { x: -9999, y: -9999, real: false };
|
|
158
|
+
const seed = hash(effects.length + el.childElementCount);
|
|
159
|
+
const h = { cv, get ctx() { return ctx; }, get W() { return W; }, get H() { return H; }, P, s: 1, colors: () => colors(el), expanded: !!opts.expanded };
|
|
160
|
+
const api = setup(h);
|
|
161
|
+
const size = () => {
|
|
162
|
+
const r = el.getBoundingClientRect(); if (!r.width || !r.height) return;
|
|
163
|
+
W = r.width; H = r.height; h.s = scaleOf(W); if (cv) ctx = fit(cv, W, H); api.resize && api.resize(W, H);
|
|
164
|
+
if (!running) api.frame(performance.now());
|
|
165
|
+
};
|
|
166
|
+
const ro = new ResizeObserver(size); ro.observe(el);
|
|
167
|
+
const local = e => { const r = el.getBoundingClientRect(); return [e.clientX - r.left, e.clientY - r.top]; };
|
|
168
|
+
const onMove = e => { [P.x, P.y] = local(e); P.real = true; lastReal = performance.now(); };
|
|
169
|
+
const onLeave = () => { P.real = false; P.x = P.y = -9999; };
|
|
170
|
+
const onDown = e => { if (e.target.closest('a, button')) return; lastReal = performance.now(); const [x, y] = local(e); api.down && api.down(x, y); };
|
|
171
|
+
el.addEventListener('pointermove', onMove); el.addEventListener('pointerleave', onLeave); el.addEventListener('pointerdown', onDown);
|
|
172
|
+
const mo = new MutationObserver(() => { api.theme && api.theme(); if (!running) api.frame(performance.now()); });
|
|
173
|
+
mo.observe(document.documentElement, { attributes: true, attributeFilter: ['data-theme', 'data-palette'] });
|
|
174
|
+
const tick = now => {
|
|
175
|
+
if (!running) return;
|
|
176
|
+
// 缩略图里 2.5 秒没人碰,就让幽灵光标接管;偶尔“点”一下展示点击效果
|
|
177
|
+
if (!h.expanded && now - lastReal > 2500) {
|
|
178
|
+
const g = ghostAt(now, W, H, seed); P.x = g.x; P.y = g.y;
|
|
179
|
+
if (api.down && api.ghostClick !== false && now - lastGhostClick > 4200) { lastGhostClick = now; api.down(g.x, g.y); }
|
|
180
|
+
}
|
|
181
|
+
if (W && H) api.frame(now);
|
|
182
|
+
raf = requestAnimationFrame(tick);
|
|
183
|
+
};
|
|
184
|
+
size();
|
|
185
|
+
return {
|
|
186
|
+
pause() { running = false; cancelAnimationFrame(raf); },
|
|
187
|
+
resume() { if (running) return; if (still()) { api.frame(performance.now()); return; } running = true; raf = requestAnimationFrame(tick); },
|
|
188
|
+
destroy() { this.pause(); ro.disconnect(); mo.disconnect(); el.removeEventListener('pointermove', onMove); el.removeEventListener('pointerleave', onLeave); el.removeEventListener('pointerdown', onDown); cv && cv.remove(); },
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// 让 CSS 能对自定义属性做补间(遮罩半径等)
|
|
193
|
+
if (window.CSS && CSS.registerProperty) {
|
|
194
|
+
for (const name of ['--vr', '--vw']) { try { CSS.registerProperty({ name, syntax: '<length>', inherits: false, initialValue: '0px' }); } catch (e) { /* 已注册 */ } }
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
return { effects, register, defaults, lines, still, clamp, lerp, ease, hash, perlin, fit, colors, tween, scaleOf, ghostAt, canvasHost };
|
|
198
|
+
})();
|
|
199
|
+
</script>
|
|
200
|
+
<script>
|
|
201
|
+
// 小交互:DOM + CSS,没有画布。用 canvasHost 的无画布模式拿到“真实指针或幽灵光标”的位置。
|
|
202
|
+
|
|
203
|
+
// ───── 边框高光卡片:只在边框那 1.5px 上画一个跟着指针的径向渐变(不给文字铺底色),外加轻微 3D 倾斜 ─────
|
|
204
|
+
MH.register({
|
|
205
|
+
id: 'glow-card', name: '边框高光卡片', cat: '交互', tech: 'CSS Mask · mask-composite · 3D Tilt',
|
|
206
|
+
desc: '指针在卡片上移动时,边框沿着指针亮起一段光,卡片同时朝指针方向轻轻倾斜。',
|
|
207
|
+
params: [
|
|
208
|
+
{ k: 'tilt', label: '倾斜角度', type: 'range', min: 0, max: 25, step: 1, def: 10, unit: '°' },
|
|
209
|
+
],
|
|
210
|
+
mount(el, opts) {
|
|
211
|
+
el.insertAdjacentHTML('beforeend', `<div class="gc-wrap">${['01', '02', '03'].map((n, i) => `<div class="gc"><span>${n}</span><b>${['等高线', '点阵扩散', '拉扯翻页'][i]}</b><em>${['背景', '过渡', '翻页'][i]}</em></div>`).join('')}</div>`);
|
|
212
|
+
const wrap = el.lastElementChild, cards = [...wrap.children];
|
|
213
|
+
return MH.canvasHost(el, opts, h => ({
|
|
214
|
+
frame() {
|
|
215
|
+
const r0 = el.getBoundingClientRect();
|
|
216
|
+
for (const c of cards) {
|
|
217
|
+
const r = c.getBoundingClientRect(), x = h.P.x + r0.left - r.left, y = h.P.y + r0.top - r.top;
|
|
218
|
+
const inside = x >= 0 && y >= 0 && x <= r.width && y <= r.height;
|
|
219
|
+
c.classList.toggle('hot', inside);
|
|
220
|
+
if (inside) {
|
|
221
|
+
c.style.setProperty('--mx', x + 'px'); c.style.setProperty('--my', y + 'px');
|
|
222
|
+
if (!MH.still()) c.style.transform = `rotateX(${(.5 - y / r.height) * opts.tilt * .8}deg) rotateY(${(x / r.width - .5) * opts.tilt}deg)`;
|
|
223
|
+
} else c.style.transform = '';
|
|
224
|
+
}
|
|
225
|
+
},
|
|
226
|
+
}), { canvas: false });
|
|
227
|
+
},
|
|
228
|
+
});
|
|
229
|
+
</script>
|
|
230
|
+
<script>
|
|
231
|
+
MH.effects.find(e => e.id === 'glow-card').mount(document.getElementById('fx'), {
|
|
232
|
+
"tilt": 10
|
|
233
|
+
}).resume();
|
|
234
|
+
</script>
|
|
235
|
+
</body>
|
|
236
|
+
</html>
|