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,248 @@
|
|
|
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
|
+
- 每 55ms 多确定一个字符;还没确定的位置每帧从符号池里随机取;空格保持不动。
|
|
15
|
+
- 用 requestAnimationFrame 驱动,结束时写回原文。
|
|
16
|
+
|
|
17
|
+
【可以调的参数】
|
|
18
|
+
- 每个字符 55ms
|
|
19
|
+
- 符号池 01#/<>+=*[]{}%$&
|
|
20
|
+
|
|
21
|
+
【注意】
|
|
22
|
+
- 原文存在 data 属性里,避免多次触发后拿到乱码当原文。
|
|
23
|
+
- 动画进行中再次悬停直接忽略。
|
|
24
|
+
- 颜色都来自 CSS 变量(paper / ink / dim / rule / accent),换成项目自己的颜色即可。
|
|
25
|
+
- 要处理 prefers-reduced-motion:系统要求减少动态效果时只显示静止画面。
|
|
26
|
+
- 组件卸载时要停掉动画循环、移除事件监听(mount 返回的 destroy)。
|
|
27
|
+
|
|
28
|
+
【opts 参数】(mount 的第二个参数,不传就用默认值;文件末尾的 mount 调用里是你在 Motion Hub 上调好的值)
|
|
29
|
+
- words:文字(一行一条),默认 "MOTION HUB\n背景 · 过渡 · 翻页\nHELLO, LJY"
|
|
30
|
+
- pool:乱码字符,默认 "01#/<>+=*[]{}%$&"
|
|
31
|
+
- step:每字落定间隔,默认 55(15–160ms)
|
|
32
|
+
|
|
33
|
+
【代码结构】
|
|
34
|
+
- <style>:配色变量和这个效果用到的样式
|
|
35
|
+
- 第一段 <script>:MH 核心(注册表、噪声、缓动;canvasHost 负责画布尺寸、指针坐标、动画循环和暂停)
|
|
36
|
+
- 第二段 <script>:效果本身,核心是 MH.register 里的 mount(el, opts),返回 { pause, resume, destroy }
|
|
37
|
+
- 最后一段 <script>:把效果挂到 #fx 上并启动
|
|
38
|
+
-->
|
|
39
|
+
<html lang="zh-CN">
|
|
40
|
+
<head>
|
|
41
|
+
<meta charset="utf-8">
|
|
42
|
+
<meta name="viewport" content="width=device-width,initial-scale=1">
|
|
43
|
+
<title>乱码落定</title>
|
|
44
|
+
<style>
|
|
45
|
+
/* Motion Hub 效果样式:配色变量(Acid:浅色暖灰 + 紫罗兰,深色近黑 + 酸橙绿)+ 各效果用到的样式 + 整页过渡遮罩。按“@css 名称”标记分段,复制代码时只带用到的段。 */
|
|
46
|
+
:root{
|
|
47
|
+
--paper:#F1F0EC;--paper2:#E4E2DC;--ink:#141316;--body:#3A383F;--dim:#85828C;--rule:#D6D3CC;--accent:#6B4EFF;--on-accent:#fff;--hi:#C6F21E;
|
|
48
|
+
--serif:"Songti SC","STSong","Noto Serif SC",serif;
|
|
49
|
+
--sans:"PingFang SC","Helvetica Neue","Noto Sans SC",sans-serif;
|
|
50
|
+
--latin:"Iowan Old Style",Palatino,Georgia,serif;
|
|
51
|
+
--mono:"SF Mono",Menlo,monospace;
|
|
52
|
+
}
|
|
53
|
+
[data-theme="night"]{--paper:#111113;--paper2:#1B1B1F;--ink:#F2F1F5;--body:#C4C2CC;--dim:#7E7B88;--rule:#2A2A30;--accent:#C6F21E;--on-accent:#111113;--hi:#8E75FF}
|
|
54
|
+
[data-theme="day"]{--paper:#F1F0EC;--paper2:#E4E2DC;--ink:#141316;--body:#3A383F;--dim:#85828C;--rule:#D6D3CC;--accent:#6B4EFF;--on-accent:#fff;--hi:#C6F21E}
|
|
55
|
+
|
|
56
|
+
/* 乱码落定 */
|
|
57
|
+
.sc{position:absolute;inset:0;display:flex;flex-direction:column;justify-content:center;gap:2.4cqh;padding:0 8cqw}
|
|
58
|
+
.sc-w{font:600 8cqh/1.1 var(--mono);letter-spacing:.04em;color:var(--dim);transition:color .2s;white-space:nowrap}
|
|
59
|
+
.sc-w.hot{color:var(--accent)}
|
|
60
|
+
html,body{height:100%;margin:0;background:var(--paper)}
|
|
61
|
+
#fx{position:relative;height:100vh;overflow:hidden;container-type:size}
|
|
62
|
+
</style>
|
|
63
|
+
</head>
|
|
64
|
+
<body>
|
|
65
|
+
<div id="fx"></div>
|
|
66
|
+
<script>
|
|
67
|
+
// Motion Hub 核心:注册表 + 公共工具。所有效果文件都是普通 <script>(不是 ES 模块),
|
|
68
|
+
// 这样直接双击 index.html(file://)也能运行。每个效果调用 MH.register({...}) 把自己登记进来。
|
|
69
|
+
//
|
|
70
|
+
// 效果对象的约定:
|
|
71
|
+
// id 唯一标识(小写短横线)
|
|
72
|
+
// name 中文名
|
|
73
|
+
// cat 分类:背景 / 过渡 / 翻页 / 片头 / 交互
|
|
74
|
+
// tech 用到的技术(展示用)
|
|
75
|
+
// desc 一句话说明
|
|
76
|
+
// mount(el, opts) → { pause(), resume(), destroy() }
|
|
77
|
+
// el 是一个已经有尺寸的容器;opts.expanded 为 true 表示全屏体验(关掉幽灵光标、允许滚轮等)
|
|
78
|
+
// params 可调参数(可选),实验台据此生成参数面板,每项 { k, label, type, def, ... }:
|
|
79
|
+
// type 'range':数值滑块,带 min / max / step,可选 unit;'text':单行文字;'lines':多行文字,一行一项
|
|
80
|
+
// mount 拿到的 opts 已经按 def 补齐(register 里包了一层),效果代码直接读 opts.k 即可
|
|
81
|
+
window.MH = (() => {
|
|
82
|
+
const effects = [];
|
|
83
|
+
// 记下效果来自哪个文件(复制代码时只带这个文件)
|
|
84
|
+
const register = e => {
|
|
85
|
+
const src = document.currentScript && document.currentScript.src; e.file = e.file || (src ? src.split('/').pop() : '');
|
|
86
|
+
if (e.params) { const m = e.mount; e.mount = (el, opts = {}) => m.call(e, el, { ...defaults(e), ...opts }); }
|
|
87
|
+
effects.push(e);
|
|
88
|
+
};
|
|
89
|
+
const defaults = e => Object.fromEntries((e.params || []).map(p => [p.k, p.def]));
|
|
90
|
+
// 'lines' 参数:数组原样返回;字符串按行拆开,去掉空行
|
|
91
|
+
const lines = v => Array.isArray(v) ? v : String(v || '').split('\n').map(x => x.trim()).filter(Boolean);
|
|
92
|
+
const still = () => matchMedia('(prefers-reduced-motion: reduce)').matches;
|
|
93
|
+
const clamp = (v, a = 0, b = 1) => Math.max(a, Math.min(b, v));
|
|
94
|
+
const lerp = (a, b, k) => a + (b - a) * k;
|
|
95
|
+
const ease = {
|
|
96
|
+
in: t => t * t * t,
|
|
97
|
+
out: t => 1 - (1 - t) ** 3,
|
|
98
|
+
inOut: t => t < .5 ? 4 * t * t * t : 1 - (-2 * t + 2) ** 3 / 2,
|
|
99
|
+
back: t => { const c = 1.4; return 1 + (c + 1) * (t - 1) ** 3 + c * (t - 1) ** 2; },
|
|
100
|
+
};
|
|
101
|
+
const hash = i => { const x = Math.sin(i * 127.1 + 311.7) * 43758.5453; return x - Math.floor(x); };
|
|
102
|
+
|
|
103
|
+
// 二维 Perlin 噪声,seed 决定排列表
|
|
104
|
+
function perlin(seed = 1) {
|
|
105
|
+
const P = new Uint8Array(512), p = [...Array(256).keys()];
|
|
106
|
+
let s = seed;
|
|
107
|
+
for (let i = 255; i > 0; i--) { s = (s * 16807) % 2147483647; const j = s % (i + 1); [p[i], p[j]] = [p[j], p[i]]; }
|
|
108
|
+
for (let i = 0; i < 512; i++) P[i] = p[i & 255];
|
|
109
|
+
const fade = t => t * t * t * (t * (t * 6 - 15) + 10), grad = (h, x, y) => ((h & 1) ? -x : x) + ((h & 2) ? -y : y);
|
|
110
|
+
return (x, y) => {
|
|
111
|
+
const X = Math.floor(x) & 255, Y = Math.floor(y) & 255; x -= Math.floor(x); y -= Math.floor(y);
|
|
112
|
+
const u = fade(x), v = fade(y), a = P[X] + Y, b = P[X + 1] + Y;
|
|
113
|
+
const l1 = grad(P[a], x, y) + u * (grad(P[b], x - 1, y) - grad(P[a], x, y));
|
|
114
|
+
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));
|
|
115
|
+
return l1 + v * (l2 - l1);
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// 画布按设备像素比放大,画的时候仍用 CSS 像素坐标
|
|
120
|
+
function fit(cv, w, h) {
|
|
121
|
+
const dpr = Math.min(devicePixelRatio || 1, 2);
|
|
122
|
+
cv.width = Math.round(w * dpr); cv.height = Math.round(h * dpr);
|
|
123
|
+
const ctx = cv.getContext('2d'); ctx.setTransform(dpr, 0, 0, dpr, 0, 0); return ctx;
|
|
124
|
+
}
|
|
125
|
+
// 从容器读主题色(--paper / --ink / --dim / --rule / --accent)
|
|
126
|
+
function colors(el) {
|
|
127
|
+
const cs = getComputedStyle(el), v = n => cs.getPropertyValue(n).trim();
|
|
128
|
+
const t = el.closest('[data-theme]');
|
|
129
|
+
return { paper: v('--paper'), ink: v('--ink'), dim: v('--dim'), rule: v('--rule'), accent: v('--accent'), night: !!t && t.dataset.theme === 'night' };
|
|
130
|
+
}
|
|
131
|
+
// 一段时长 ms 的补间,fn(k) 中 k 从 0 到 1;减少动态效果时直接到终点
|
|
132
|
+
const tween = (ms, fn) => new Promise(res => {
|
|
133
|
+
if (still()) { fn(1); return res(); }
|
|
134
|
+
const t0 = performance.now();
|
|
135
|
+
const f = now => { const k = clamp((now - t0) / ms); fn(k); k < 1 ? requestAnimationFrame(f) : res(); };
|
|
136
|
+
requestAnimationFrame(f);
|
|
137
|
+
});
|
|
138
|
+
// 尺寸缩放系数:全屏约 1,缩略图约 0.45,让小画面像“整页缩小”而不是挤满细节
|
|
139
|
+
const scaleOf = W => clamp(W / 1200, .45, 1);
|
|
140
|
+
|
|
141
|
+
// 幽灵光标:没人操作时,沿一条利萨如曲线在容器里慢慢移动,让缩略图自己演示交互
|
|
142
|
+
const ghostAt = (now, W, H, seed = 0) => {
|
|
143
|
+
const t = now / 1000 + seed * 7;
|
|
144
|
+
return { x: W * (.5 + .32 * Math.sin(t * .55) * Math.cos(t * .21)), y: H * (.5 + .3 * Math.sin(t * .83 + 1.3)) };
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
// 效果宿主:(可选)建画布、跟随尺寸、换算局部指针坐标、管动画循环和暂停。
|
|
148
|
+
// setup(h) 返回 { resize(W,H), frame(now), down(x,y), theme(), ghostClick },h 提供 ctx / W / H / P / s / colors()
|
|
149
|
+
// ghostClick: false 表示幽灵光标不替它“点击”(例如片头,点一下会重播)
|
|
150
|
+
function canvasHost(el, opts, setup, { canvas = true } = {}) {
|
|
151
|
+
const cv = canvas ? document.createElement('canvas') : null;
|
|
152
|
+
if (cv) { cv.className = 'mh-canvas'; el.append(cv); }
|
|
153
|
+
let ctx, W = 0, H = 0, raf = 0, running = false, lastReal = -1e9, lastGhostClick = 0;
|
|
154
|
+
const P = { x: -9999, y: -9999, real: false };
|
|
155
|
+
const seed = hash(effects.length + el.childElementCount);
|
|
156
|
+
const h = { cv, get ctx() { return ctx; }, get W() { return W; }, get H() { return H; }, P, s: 1, colors: () => colors(el), expanded: !!opts.expanded };
|
|
157
|
+
const api = setup(h);
|
|
158
|
+
const size = () => {
|
|
159
|
+
const r = el.getBoundingClientRect(); if (!r.width || !r.height) return;
|
|
160
|
+
W = r.width; H = r.height; h.s = scaleOf(W); if (cv) ctx = fit(cv, W, H); api.resize && api.resize(W, H);
|
|
161
|
+
if (!running) api.frame(performance.now());
|
|
162
|
+
};
|
|
163
|
+
const ro = new ResizeObserver(size); ro.observe(el);
|
|
164
|
+
const local = e => { const r = el.getBoundingClientRect(); return [e.clientX - r.left, e.clientY - r.top]; };
|
|
165
|
+
const onMove = e => { [P.x, P.y] = local(e); P.real = true; lastReal = performance.now(); };
|
|
166
|
+
const onLeave = () => { P.real = false; P.x = P.y = -9999; };
|
|
167
|
+
const onDown = e => { if (e.target.closest('a, button')) return; lastReal = performance.now(); const [x, y] = local(e); api.down && api.down(x, y); };
|
|
168
|
+
el.addEventListener('pointermove', onMove); el.addEventListener('pointerleave', onLeave); el.addEventListener('pointerdown', onDown);
|
|
169
|
+
const mo = new MutationObserver(() => { api.theme && api.theme(); if (!running) api.frame(performance.now()); });
|
|
170
|
+
mo.observe(document.documentElement, { attributes: true, attributeFilter: ['data-theme', 'data-palette'] });
|
|
171
|
+
const tick = now => {
|
|
172
|
+
if (!running) return;
|
|
173
|
+
// 缩略图里 2.5 秒没人碰,就让幽灵光标接管;偶尔“点”一下展示点击效果
|
|
174
|
+
if (!h.expanded && now - lastReal > 2500) {
|
|
175
|
+
const g = ghostAt(now, W, H, seed); P.x = g.x; P.y = g.y;
|
|
176
|
+
if (api.down && api.ghostClick !== false && now - lastGhostClick > 4200) { lastGhostClick = now; api.down(g.x, g.y); }
|
|
177
|
+
}
|
|
178
|
+
if (W && H) api.frame(now);
|
|
179
|
+
raf = requestAnimationFrame(tick);
|
|
180
|
+
};
|
|
181
|
+
size();
|
|
182
|
+
return {
|
|
183
|
+
pause() { running = false; cancelAnimationFrame(raf); },
|
|
184
|
+
resume() { if (running) return; if (still()) { api.frame(performance.now()); return; } running = true; raf = requestAnimationFrame(tick); },
|
|
185
|
+
destroy() { this.pause(); ro.disconnect(); mo.disconnect(); el.removeEventListener('pointermove', onMove); el.removeEventListener('pointerleave', onLeave); el.removeEventListener('pointerdown', onDown); cv && cv.remove(); },
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// 让 CSS 能对自定义属性做补间(遮罩半径等)
|
|
190
|
+
if (window.CSS && CSS.registerProperty) {
|
|
191
|
+
for (const name of ['--vr', '--vw']) { try { CSS.registerProperty({ name, syntax: '<length>', inherits: false, initialValue: '0px' }); } catch (e) { /* 已注册 */ } }
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
return { effects, register, defaults, lines, still, clamp, lerp, ease, hash, perlin, fit, colors, tween, scaleOf, ghostAt, canvasHost };
|
|
195
|
+
})();
|
|
196
|
+
</script>
|
|
197
|
+
<script>
|
|
198
|
+
// 小交互:DOM + CSS,没有画布。用 canvasHost 的无画布模式拿到“真实指针或幽灵光标”的位置。
|
|
199
|
+
|
|
200
|
+
// ───── 乱码落定:文字先变成一串随机符号,再从左到右一个个落定 ─────
|
|
201
|
+
MH.register({
|
|
202
|
+
id: 'scramble', name: '乱码落定', cat: '交互', tech: 'requestAnimationFrame · Text Scramble',
|
|
203
|
+
desc: '指针经过时,文字先变成乱码,再从左到右一个个落回原字,适合导航和标题。',
|
|
204
|
+
params: [
|
|
205
|
+
{ k: 'words', label: '文字(一行一条)', type: 'lines', def: 'MOTION HUB\n背景 · 过渡 · 翻页\nHELLO, LJY' },
|
|
206
|
+
{ k: 'pool', label: '乱码字符', type: 'text', def: '01#/<>+=*[]{}%$&' },
|
|
207
|
+
{ k: 'step', label: '每字落定间隔', type: 'range', min: 15, max: 160, step: 5, def: 55, unit: 'ms' },
|
|
208
|
+
],
|
|
209
|
+
mount(el, opts) {
|
|
210
|
+
const WORDS = MH.lines(opts.words);
|
|
211
|
+
el.insertAdjacentHTML('beforeend', `<div class="sc">${WORDS.map(w => `<div class="sc-w" data-s="${w}">${w}</div>`).join('')}</div>`);
|
|
212
|
+
const box = el.lastElementChild, items = [...box.children], pool = opts.pool || '01#/<>+=*[]{}%$&';
|
|
213
|
+
const play = a => {
|
|
214
|
+
if (a._busy || MH.still()) return; a._busy = true;
|
|
215
|
+
const s = a.dataset.s, t0 = performance.now();
|
|
216
|
+
const tick = now => {
|
|
217
|
+
const k = Math.floor((now - t0) / opts.step);
|
|
218
|
+
a.textContent = [...s].map((ch, i) => i < k || ch === ' ' ? ch : pool[Math.random() * pool.length | 0]).join('');
|
|
219
|
+
if (k <= s.length) requestAnimationFrame(tick); else { a.textContent = s; a._busy = false; }
|
|
220
|
+
};
|
|
221
|
+
requestAnimationFrame(tick);
|
|
222
|
+
};
|
|
223
|
+
let last = null;
|
|
224
|
+
return MH.canvasHost(el, opts, h => ({
|
|
225
|
+
frame() {
|
|
226
|
+
const r0 = el.getBoundingClientRect();
|
|
227
|
+
const over = items.find(a => { const r = a.getBoundingClientRect(), x = h.P.x + r0.left, y = h.P.y + r0.top; return x >= r.left && x <= r.right && y >= r.top && y <= r.bottom; });
|
|
228
|
+
if (over && over !== last) play(over);
|
|
229
|
+
items.forEach(a => a.classList.toggle('hot', a === over));
|
|
230
|
+
last = over || null;
|
|
231
|
+
},
|
|
232
|
+
}), { canvas: false });
|
|
233
|
+
},
|
|
234
|
+
});
|
|
235
|
+
</script>
|
|
236
|
+
<script>
|
|
237
|
+
MH.effects.find(e => e.id === 'scramble').mount(document.getElementById('fx'), {
|
|
238
|
+
"words": [
|
|
239
|
+
"MOTION HUB",
|
|
240
|
+
"背景 · 过渡 · 翻页",
|
|
241
|
+
"HELLO, LJY"
|
|
242
|
+
],
|
|
243
|
+
"pool": "01#/<>+=*[]{}%$&",
|
|
244
|
+
"step": 55
|
|
245
|
+
}).resume();
|
|
246
|
+
</script>
|
|
247
|
+
</body>
|
|
248
|
+
</html>
|
|
@@ -0,0 +1,235 @@
|
|
|
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
|
+
- SVG 里一个主圆加 8 条光芒;主圆套一个 mask,mask 里有一个黑色小圆负责“咬”。
|
|
15
|
+
- 月亮状态:主圆 scale(1.5),咬的圆 translate 进来,光芒 rotate(-45deg) scale(.4) 并淡出。
|
|
16
|
+
- 全部用 CSS transition(0.5s,cubic-bezier(.3,.8,.2,1)),切换时只切一个 class。
|
|
17
|
+
|
|
18
|
+
【可以调的参数】
|
|
19
|
+
- 过渡 0.5s
|
|
20
|
+
- 咬的圆半径 7.5(24×24 坐标系)
|
|
21
|
+
|
|
22
|
+
【注意】
|
|
23
|
+
- 同一页有多个图标时,mask 的 id 要唯一。
|
|
24
|
+
- 按钮要有 aria-label,比如“切换到深色模式”。
|
|
25
|
+
- 颜色都来自 CSS 变量(paper / ink / dim / rule / accent),换成项目自己的颜色即可。
|
|
26
|
+
- 要处理 prefers-reduced-motion:系统要求减少动态效果时只显示静止画面。
|
|
27
|
+
- 组件卸载时要停掉动画循环、移除事件监听(mount 返回的 destroy)。
|
|
28
|
+
|
|
29
|
+
【opts 参数】(mount 的第二个参数,不传就用默认值;文件末尾的 mount 调用里是你在 Motion Hub 上调好的值)
|
|
30
|
+
- interval:自动演示间隔,默认 1800(600–5000ms)
|
|
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
|
+
.sm{display:inline-grid;place-items:center;width:36px;height:36px;padding:0;border:1px solid currentColor;background:none;cursor:pointer;color:var(--ink)}
|
|
57
|
+
.sm svg{width:18px;height:18px;overflow:visible}
|
|
58
|
+
.sm .core{transform-origin:12px 12px;transition:transform .5s cubic-bezier(.3,.8,.2,1)}
|
|
59
|
+
.sm .bite{transition:transform .5s cubic-bezier(.3,.8,.2,1)}
|
|
60
|
+
.sm .rays{transform-origin:12px 12px;transition:transform .5s cubic-bezier(.3,.8,.2,1),opacity .3s}
|
|
61
|
+
.sm.moon .core{transform:scale(1.5)}
|
|
62
|
+
.sm.moon .bite{transform:translate(-10.5px,4.5px)}
|
|
63
|
+
.sm.moon .rays{transform:rotate(-45deg) scale(.4);opacity:0}
|
|
64
|
+
.sm:hover{color:var(--accent)}
|
|
65
|
+
|
|
66
|
+
.ui-center{position:absolute;inset:0;display:grid;place-items:center;align-content:center;gap:14px}
|
|
67
|
+
|
|
68
|
+
/* 太阳月亮开关(舞台版,大号) */
|
|
69
|
+
.ui-center .sm{width:22cqh;height:22cqh;border-width:2px;color:var(--accent)}
|
|
70
|
+
.ui-center .sm svg{width:11cqh;height:11cqh}
|
|
71
|
+
.sm-tip{font:500 12px/1 var(--mono);letter-spacing:.2em;color:var(--dim)}
|
|
72
|
+
html,body{height:100%;margin:0;background:var(--paper)}
|
|
73
|
+
#fx{position:relative;height:100vh;overflow:hidden;container-type:size}
|
|
74
|
+
</style>
|
|
75
|
+
</head>
|
|
76
|
+
<body>
|
|
77
|
+
<div id="fx"></div>
|
|
78
|
+
<script>
|
|
79
|
+
// Motion Hub 核心:注册表 + 公共工具。所有效果文件都是普通 <script>(不是 ES 模块),
|
|
80
|
+
// 这样直接双击 index.html(file://)也能运行。每个效果调用 MH.register({...}) 把自己登记进来。
|
|
81
|
+
//
|
|
82
|
+
// 效果对象的约定:
|
|
83
|
+
// id 唯一标识(小写短横线)
|
|
84
|
+
// name 中文名
|
|
85
|
+
// cat 分类:背景 / 过渡 / 翻页 / 片头 / 交互
|
|
86
|
+
// tech 用到的技术(展示用)
|
|
87
|
+
// desc 一句话说明
|
|
88
|
+
// mount(el, opts) → { pause(), resume(), destroy() }
|
|
89
|
+
// el 是一个已经有尺寸的容器;opts.expanded 为 true 表示全屏体验(关掉幽灵光标、允许滚轮等)
|
|
90
|
+
// params 可调参数(可选),实验台据此生成参数面板,每项 { k, label, type, def, ... }:
|
|
91
|
+
// type 'range':数值滑块,带 min / max / step,可选 unit;'text':单行文字;'lines':多行文字,一行一项
|
|
92
|
+
// mount 拿到的 opts 已经按 def 补齐(register 里包了一层),效果代码直接读 opts.k 即可
|
|
93
|
+
window.MH = (() => {
|
|
94
|
+
const effects = [];
|
|
95
|
+
// 记下效果来自哪个文件(复制代码时只带这个文件)
|
|
96
|
+
const register = e => {
|
|
97
|
+
const src = document.currentScript && document.currentScript.src; e.file = e.file || (src ? src.split('/').pop() : '');
|
|
98
|
+
if (e.params) { const m = e.mount; e.mount = (el, opts = {}) => m.call(e, el, { ...defaults(e), ...opts }); }
|
|
99
|
+
effects.push(e);
|
|
100
|
+
};
|
|
101
|
+
const defaults = e => Object.fromEntries((e.params || []).map(p => [p.k, p.def]));
|
|
102
|
+
// 'lines' 参数:数组原样返回;字符串按行拆开,去掉空行
|
|
103
|
+
const lines = v => Array.isArray(v) ? v : String(v || '').split('\n').map(x => x.trim()).filter(Boolean);
|
|
104
|
+
const still = () => matchMedia('(prefers-reduced-motion: reduce)').matches;
|
|
105
|
+
const clamp = (v, a = 0, b = 1) => Math.max(a, Math.min(b, v));
|
|
106
|
+
const lerp = (a, b, k) => a + (b - a) * k;
|
|
107
|
+
const ease = {
|
|
108
|
+
in: t => t * t * t,
|
|
109
|
+
out: t => 1 - (1 - t) ** 3,
|
|
110
|
+
inOut: t => t < .5 ? 4 * t * t * t : 1 - (-2 * t + 2) ** 3 / 2,
|
|
111
|
+
back: t => { const c = 1.4; return 1 + (c + 1) * (t - 1) ** 3 + c * (t - 1) ** 2; },
|
|
112
|
+
};
|
|
113
|
+
const hash = i => { const x = Math.sin(i * 127.1 + 311.7) * 43758.5453; return x - Math.floor(x); };
|
|
114
|
+
|
|
115
|
+
// 二维 Perlin 噪声,seed 决定排列表
|
|
116
|
+
function perlin(seed = 1) {
|
|
117
|
+
const P = new Uint8Array(512), p = [...Array(256).keys()];
|
|
118
|
+
let s = seed;
|
|
119
|
+
for (let i = 255; i > 0; i--) { s = (s * 16807) % 2147483647; const j = s % (i + 1); [p[i], p[j]] = [p[j], p[i]]; }
|
|
120
|
+
for (let i = 0; i < 512; i++) P[i] = p[i & 255];
|
|
121
|
+
const fade = t => t * t * t * (t * (t * 6 - 15) + 10), grad = (h, x, y) => ((h & 1) ? -x : x) + ((h & 2) ? -y : y);
|
|
122
|
+
return (x, y) => {
|
|
123
|
+
const X = Math.floor(x) & 255, Y = Math.floor(y) & 255; x -= Math.floor(x); y -= Math.floor(y);
|
|
124
|
+
const u = fade(x), v = fade(y), a = P[X] + Y, b = P[X + 1] + Y;
|
|
125
|
+
const l1 = grad(P[a], x, y) + u * (grad(P[b], x - 1, y) - grad(P[a], x, y));
|
|
126
|
+
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));
|
|
127
|
+
return l1 + v * (l2 - l1);
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// 画布按设备像素比放大,画的时候仍用 CSS 像素坐标
|
|
132
|
+
function fit(cv, w, h) {
|
|
133
|
+
const dpr = Math.min(devicePixelRatio || 1, 2);
|
|
134
|
+
cv.width = Math.round(w * dpr); cv.height = Math.round(h * dpr);
|
|
135
|
+
const ctx = cv.getContext('2d'); ctx.setTransform(dpr, 0, 0, dpr, 0, 0); return ctx;
|
|
136
|
+
}
|
|
137
|
+
// 从容器读主题色(--paper / --ink / --dim / --rule / --accent)
|
|
138
|
+
function colors(el) {
|
|
139
|
+
const cs = getComputedStyle(el), v = n => cs.getPropertyValue(n).trim();
|
|
140
|
+
const t = el.closest('[data-theme]');
|
|
141
|
+
return { paper: v('--paper'), ink: v('--ink'), dim: v('--dim'), rule: v('--rule'), accent: v('--accent'), night: !!t && t.dataset.theme === 'night' };
|
|
142
|
+
}
|
|
143
|
+
// 一段时长 ms 的补间,fn(k) 中 k 从 0 到 1;减少动态效果时直接到终点
|
|
144
|
+
const tween = (ms, fn) => new Promise(res => {
|
|
145
|
+
if (still()) { fn(1); return res(); }
|
|
146
|
+
const t0 = performance.now();
|
|
147
|
+
const f = now => { const k = clamp((now - t0) / ms); fn(k); k < 1 ? requestAnimationFrame(f) : res(); };
|
|
148
|
+
requestAnimationFrame(f);
|
|
149
|
+
});
|
|
150
|
+
// 尺寸缩放系数:全屏约 1,缩略图约 0.45,让小画面像“整页缩小”而不是挤满细节
|
|
151
|
+
const scaleOf = W => clamp(W / 1200, .45, 1);
|
|
152
|
+
|
|
153
|
+
// 幽灵光标:没人操作时,沿一条利萨如曲线在容器里慢慢移动,让缩略图自己演示交互
|
|
154
|
+
const ghostAt = (now, W, H, seed = 0) => {
|
|
155
|
+
const t = now / 1000 + seed * 7;
|
|
156
|
+
return { x: W * (.5 + .32 * Math.sin(t * .55) * Math.cos(t * .21)), y: H * (.5 + .3 * Math.sin(t * .83 + 1.3)) };
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
// 效果宿主:(可选)建画布、跟随尺寸、换算局部指针坐标、管动画循环和暂停。
|
|
160
|
+
// setup(h) 返回 { resize(W,H), frame(now), down(x,y), theme(), ghostClick },h 提供 ctx / W / H / P / s / colors()
|
|
161
|
+
// ghostClick: false 表示幽灵光标不替它“点击”(例如片头,点一下会重播)
|
|
162
|
+
function canvasHost(el, opts, setup, { canvas = true } = {}) {
|
|
163
|
+
const cv = canvas ? document.createElement('canvas') : null;
|
|
164
|
+
if (cv) { cv.className = 'mh-canvas'; el.append(cv); }
|
|
165
|
+
let ctx, W = 0, H = 0, raf = 0, running = false, lastReal = -1e9, lastGhostClick = 0;
|
|
166
|
+
const P = { x: -9999, y: -9999, real: false };
|
|
167
|
+
const seed = hash(effects.length + el.childElementCount);
|
|
168
|
+
const h = { cv, get ctx() { return ctx; }, get W() { return W; }, get H() { return H; }, P, s: 1, colors: () => colors(el), expanded: !!opts.expanded };
|
|
169
|
+
const api = setup(h);
|
|
170
|
+
const size = () => {
|
|
171
|
+
const r = el.getBoundingClientRect(); if (!r.width || !r.height) return;
|
|
172
|
+
W = r.width; H = r.height; h.s = scaleOf(W); if (cv) ctx = fit(cv, W, H); api.resize && api.resize(W, H);
|
|
173
|
+
if (!running) api.frame(performance.now());
|
|
174
|
+
};
|
|
175
|
+
const ro = new ResizeObserver(size); ro.observe(el);
|
|
176
|
+
const local = e => { const r = el.getBoundingClientRect(); return [e.clientX - r.left, e.clientY - r.top]; };
|
|
177
|
+
const onMove = e => { [P.x, P.y] = local(e); P.real = true; lastReal = performance.now(); };
|
|
178
|
+
const onLeave = () => { P.real = false; P.x = P.y = -9999; };
|
|
179
|
+
const onDown = e => { if (e.target.closest('a, button')) return; lastReal = performance.now(); const [x, y] = local(e); api.down && api.down(x, y); };
|
|
180
|
+
el.addEventListener('pointermove', onMove); el.addEventListener('pointerleave', onLeave); el.addEventListener('pointerdown', onDown);
|
|
181
|
+
const mo = new MutationObserver(() => { api.theme && api.theme(); if (!running) api.frame(performance.now()); });
|
|
182
|
+
mo.observe(document.documentElement, { attributes: true, attributeFilter: ['data-theme', 'data-palette'] });
|
|
183
|
+
const tick = now => {
|
|
184
|
+
if (!running) return;
|
|
185
|
+
// 缩略图里 2.5 秒没人碰,就让幽灵光标接管;偶尔“点”一下展示点击效果
|
|
186
|
+
if (!h.expanded && now - lastReal > 2500) {
|
|
187
|
+
const g = ghostAt(now, W, H, seed); P.x = g.x; P.y = g.y;
|
|
188
|
+
if (api.down && api.ghostClick !== false && now - lastGhostClick > 4200) { lastGhostClick = now; api.down(g.x, g.y); }
|
|
189
|
+
}
|
|
190
|
+
if (W && H) api.frame(now);
|
|
191
|
+
raf = requestAnimationFrame(tick);
|
|
192
|
+
};
|
|
193
|
+
size();
|
|
194
|
+
return {
|
|
195
|
+
pause() { running = false; cancelAnimationFrame(raf); },
|
|
196
|
+
resume() { if (running) return; if (still()) { api.frame(performance.now()); return; } running = true; raf = requestAnimationFrame(tick); },
|
|
197
|
+
destroy() { this.pause(); ro.disconnect(); mo.disconnect(); el.removeEventListener('pointermove', onMove); el.removeEventListener('pointerleave', onLeave); el.removeEventListener('pointerdown', onDown); cv && cv.remove(); },
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// 让 CSS 能对自定义属性做补间(遮罩半径等)
|
|
202
|
+
if (window.CSS && CSS.registerProperty) {
|
|
203
|
+
for (const name of ['--vr', '--vw']) { try { CSS.registerProperty({ name, syntax: '<length>', inherits: false, initialValue: '0px' }); } catch (e) { /* 已注册 */ } }
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
return { effects, register, defaults, lines, still, clamp, lerp, ease, hash, perlin, fit, colors, tween, scaleOf, ghostAt, canvasHost };
|
|
207
|
+
})();
|
|
208
|
+
</script>
|
|
209
|
+
<script>
|
|
210
|
+
// ───── 太阳 ⇄ 月亮:一个圆 + 一个“咬掉一口”的遮罩 + 八条光芒;遮罩移进来、光芒收起就是月亮 ─────
|
|
211
|
+
MH.register({
|
|
212
|
+
id: 'sun-moon', name: '太阳月亮开关', cat: '交互', tech: 'SVG Mask · CSS Transform',
|
|
213
|
+
desc: '深浅模式开关的图标:浅色时是月亮(点了变深色),深色时是太阳;切换时光芒收起、圆被咬掉一口变成月牙。',
|
|
214
|
+
params: [
|
|
215
|
+
{ k: 'interval', label: '自动演示间隔', type: 'range', min: 600, max: 5000, step: 100, def: 1800, unit: 'ms' },
|
|
216
|
+
],
|
|
217
|
+
mount(el, opts) {
|
|
218
|
+
const rays = [0, 45, 90, 135, 180, 225, 270, 315].map(a => `<line x1="12" y1="2.5" x2="12" y2="4.5" transform="rotate(${a} 12 12)"/>`).join('');
|
|
219
|
+
const id = 'sm' + Math.random().toString(36).slice(2, 7);
|
|
220
|
+
el.insertAdjacentHTML('beforeend', `<div class="ui-center"><button class="sm moon" type="button" aria-label="切换"><svg viewBox="0 0 24 24"><mask id="${id}"><rect x="-4" y="-4" width="32" height="32" fill="#fff"/><circle class="bite" cx="28" cy="2" r="7.5" fill="#000"/></mask><circle class="core" cx="12" cy="12" r="5" fill="currentColor" mask="url(#${id})"/><g class="rays" stroke="currentColor" stroke-width="1.8" stroke-linecap="round">${rays}</g></svg></button><span class="sm-tip">点一下</span></div>`);
|
|
221
|
+
const box = el.lastElementChild, btn = box.querySelector('.sm');
|
|
222
|
+
let running = false, timer = 0, lastReal = -1e9;
|
|
223
|
+
btn.addEventListener('click', () => { lastReal = performance.now(); btn.classList.toggle('moon'); });
|
|
224
|
+
const auto = () => { if (!running) return; if (performance.now() - lastReal > 3000) btn.classList.toggle('moon'); timer = setTimeout(auto, opts.interval); };
|
|
225
|
+
return { pause() { running = false; clearTimeout(timer); }, resume() { if (running || MH.still()) return; running = true; timer = setTimeout(auto, 1200); }, destroy() { this.pause(); box.remove(); } };
|
|
226
|
+
},
|
|
227
|
+
});
|
|
228
|
+
</script>
|
|
229
|
+
<script>
|
|
230
|
+
MH.effects.find(e => e.id === 'sun-moon').mount(document.getElementById('fx'), {
|
|
231
|
+
"interval": 1800
|
|
232
|
+
}).resume();
|
|
233
|
+
</script>
|
|
234
|
+
</body>
|
|
235
|
+
</html>
|