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,263 @@
|
|
|
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
|
+
- 每个点记住静止位置;鼠标半径内按 (1 - d/R)² 施加径向推力。
|
|
15
|
+
- 弹簧把点拉回静止位置(v += (home - p) × 0.06),再乘阻尼 0.82,形成回弹。
|
|
16
|
+
- 点偏离静止位置越远半径越大;超过阈值的点归入强调色。
|
|
17
|
+
- 冲击波是一圈以 700px/s 外扩的高斯环,经过时给点一个向外的冲量。
|
|
18
|
+
- 所有点按颜色合并进两个 Path2D,一次 fill。
|
|
19
|
+
|
|
20
|
+
【可以调的参数】
|
|
21
|
+
- 点间距 GAP 26px
|
|
22
|
+
- 推开半径 R 150px
|
|
23
|
+
- 推力 3.2
|
|
24
|
+
- 弹簧 0.06、阻尼 0.82
|
|
25
|
+
- 冲击波速度 700px/s、环宽 40px、寿命 1.8s
|
|
26
|
+
- “呼吸”:半径随位置和时间做 ±0.25 的正弦起伏
|
|
27
|
+
|
|
28
|
+
【注意】
|
|
29
|
+
- 点数 = 屏幕面积 / GAP²,2K 屏约 3000 个;合并 Path2D 是性能关键。
|
|
30
|
+
- 如果页面上有按钮,点按钮时不要触发冲击波(判断 event.target.closest("a, button"))。否则配合 View Transitions 切换主题时,快照会把冲击波冻住,看起来像卡了一下。
|
|
31
|
+
- 颜色都来自 CSS 变量(paper / ink / dim / rule / accent),换成项目自己的颜色即可。
|
|
32
|
+
- 要处理 prefers-reduced-motion:系统要求减少动态效果时只显示静止画面。
|
|
33
|
+
- 组件卸载时要停掉动画循环、移除事件监听(mount 返回的 destroy)。
|
|
34
|
+
|
|
35
|
+
【opts 参数】(mount 的第二个参数,不传就用默认值;文件末尾的 mount 调用里是你在 Motion Hub 上调好的值)
|
|
36
|
+
- gap:点间距,默认 26(12–60px)
|
|
37
|
+
- radius:推开半径,默认 150(40–320px)
|
|
38
|
+
- spring:弹簧刚度,默认 0.06(0.01–0.2)
|
|
39
|
+
- damping:阻尼,默认 0.82(0.6–0.95)
|
|
40
|
+
|
|
41
|
+
【代码结构】
|
|
42
|
+
- <style>:配色变量和这个效果用到的样式
|
|
43
|
+
- 第一段 <script>:MH 核心(注册表、噪声、缓动;canvasHost 负责画布尺寸、指针坐标、动画循环和暂停)
|
|
44
|
+
- 第二段 <script>:效果本身,核心是 MH.register 里的 mount(el, opts),返回 { pause, resume, destroy }
|
|
45
|
+
- 最后一段 <script>:把效果挂到 #fx 上并启动
|
|
46
|
+
-->
|
|
47
|
+
<html lang="zh-CN">
|
|
48
|
+
<head>
|
|
49
|
+
<meta charset="utf-8">
|
|
50
|
+
<meta name="viewport" content="width=device-width,initial-scale=1">
|
|
51
|
+
<title>磁性点阵</title>
|
|
52
|
+
<style>
|
|
53
|
+
/* Motion Hub 效果样式:配色变量(Acid:浅色暖灰 + 紫罗兰,深色近黑 + 酸橙绿)+ 各效果用到的样式 + 整页过渡遮罩。按“@css 名称”标记分段,复制代码时只带用到的段。 */
|
|
54
|
+
:root{
|
|
55
|
+
--paper:#F1F0EC;--paper2:#E4E2DC;--ink:#141316;--body:#3A383F;--dim:#85828C;--rule:#D6D3CC;--accent:#6B4EFF;--on-accent:#fff;--hi:#C6F21E;
|
|
56
|
+
--serif:"Songti SC","STSong","Noto Serif SC",serif;
|
|
57
|
+
--sans:"PingFang SC","Helvetica Neue","Noto Sans SC",sans-serif;
|
|
58
|
+
--latin:"Iowan Old Style",Palatino,Georgia,serif;
|
|
59
|
+
--mono:"SF Mono",Menlo,monospace;
|
|
60
|
+
}
|
|
61
|
+
[data-theme="night"]{--paper:#111113;--paper2:#1B1B1F;--ink:#F2F1F5;--body:#C4C2CC;--dim:#7E7B88;--rule:#2A2A30;--accent:#C6F21E;--on-accent:#111113;--hi:#8E75FF}
|
|
62
|
+
[data-theme="day"]{--paper:#F1F0EC;--paper2:#E4E2DC;--ink:#141316;--body:#3A383F;--dim:#85828C;--rule:#D6D3CC;--accent:#6B4EFF;--on-accent:#fff;--hi:#C6F21E}
|
|
63
|
+
|
|
64
|
+
/* ───── 效果共用 ───── */
|
|
65
|
+
.mh-canvas{position:absolute;inset:0;width:100%;height:100%;display:block}
|
|
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
|
+
// 背景动效:画在 canvas 上,尺寸跟随容器,h.s 是缩放系数(全屏约 1,缩略图约 0.45)。
|
|
205
|
+
|
|
206
|
+
// ───── 磁性点阵:每个点有静止位置,被鼠标推开、被弹簧拉回;推得越远越大、越接近强调色;点击发出冲击波 ─────
|
|
207
|
+
function dotField(quiet) {
|
|
208
|
+
return (el, opts) => MH.canvasHost(el, opts, h => {
|
|
209
|
+
let C = h.colors(), dots = [], GAP, R;
|
|
210
|
+
const waves = [];
|
|
211
|
+
return {
|
|
212
|
+
resize(W, H) {
|
|
213
|
+
GAP = Math.max(8, opts.gap * h.s); R = opts.radius * h.s; dots = [];
|
|
214
|
+
const ox = (W % GAP) / 2, oy = (H % GAP) / 2;
|
|
215
|
+
for (let y = oy; y < H; y += GAP) for (let x = ox; x < W; x += GAP) dots.push({ hx: x, hy: y, x, y, vx: 0, vy: 0 });
|
|
216
|
+
},
|
|
217
|
+
theme() { C = h.colors(); },
|
|
218
|
+
down: quiet ? null : (x, y) => { waves.push({ x, y, t0: performance.now() }); if (waves.length > 3) waves.shift(); },
|
|
219
|
+
frame(now) {
|
|
220
|
+
const { ctx, W, H, P, s } = h, t = now / 1000, live = waves.filter(w => now - w.t0 < 1800);
|
|
221
|
+
const push = quiet ? 1.6 : 3.2, base = new Path2D(), hot = new Path2D();
|
|
222
|
+
for (const d of dots) {
|
|
223
|
+
const dx = d.x - P.x, dy = d.y - P.y, dist = Math.hypot(dx, dy);
|
|
224
|
+
if (dist < R && dist > .1) { const f = (1 - dist / R) ** 2 * push * s; d.vx += dx / dist * f; d.vy += dy / dist * f; }
|
|
225
|
+
for (const w of live) {
|
|
226
|
+
const age = (now - w.t0) / 1000, wx = d.hx - w.x, wy = d.hy - w.y, wd = Math.hypot(wx, wy), front = age * 700 * s;
|
|
227
|
+
const g = Math.exp(-(((wd - front) / (40 * s)) ** 2)) * (1 - age / 1.8) * 1.3 * s;
|
|
228
|
+
if (g > .01 && wd > 1) { d.vx += wx / wd * g; d.vy += wy / wd * g; }
|
|
229
|
+
}
|
|
230
|
+
d.vx += (d.hx - d.x) * opts.spring; d.vy += (d.hy - d.y) * opts.spring; d.vx *= opts.damping; d.vy *= opts.damping; d.x += d.vx; d.y += d.vy;
|
|
231
|
+
const off = Math.hypot(d.x - d.hx, d.y - d.hy) / s;
|
|
232
|
+
const breathe = quiet ? 0 : Math.sin(d.hx * .012 / s + d.hy * .008 / s - t * 1.2) * .25;
|
|
233
|
+
const r = Math.min(1.1 + breathe + off * (quiet ? .06 : .09), quiet ? 2.4 : 3.6) * Math.max(.7, s);
|
|
234
|
+
const p = off > (quiet ? 6 : 7) ? hot : base;
|
|
235
|
+
p.moveTo(d.x + r, d.y); p.arc(d.x, d.y, r, 0, Math.PI * 2);
|
|
236
|
+
}
|
|
237
|
+
ctx.clearRect(0, 0, W, H);
|
|
238
|
+
ctx.fillStyle = C.dim; ctx.globalAlpha = quiet ? (C.night ? .26 : .3) : (C.night ? .38 : .42); ctx.fill(base);
|
|
239
|
+
ctx.fillStyle = C.accent; ctx.globalAlpha = quiet ? .6 : .85; ctx.fill(hot); ctx.globalAlpha = 1;
|
|
240
|
+
},
|
|
241
|
+
};
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
// 可调参数:点间距、推开半径、弹簧刚度(拉回的力)、阻尼(每帧保留的速度,越大越“弹”)
|
|
245
|
+
const dotParams = radius => [
|
|
246
|
+
{ k: 'gap', label: '点间距', type: 'range', min: 12, max: 60, step: 1, def: 26, unit: 'px' },
|
|
247
|
+
{ k: 'radius', label: '推开半径', type: 'range', min: 40, max: 320, step: 5, def: radius, unit: 'px' },
|
|
248
|
+
{ k: 'spring', label: '弹簧刚度', type: 'range', min: 0.01, max: 0.2, step: 0.005, def: 0.06 },
|
|
249
|
+
{ k: 'damping', label: '阻尼', type: 'range', min: 0.6, max: 0.95, step: 0.01, def: 0.82 },
|
|
250
|
+
];
|
|
251
|
+
MH.register({ id: 'dots', name: '磁性点阵', cat: '背景', tech: 'Canvas 2D · Spring-Damper Physics', desc: '整屏点阵被鼠标推开再弹回,推开的点变大、变成强调色;点击发出一圈冲击波。', params: dotParams(150), mount: dotField(false) });
|
|
252
|
+
MH.register({ id: 'dots-quiet', name: '安静点阵', cat: '背景', tech: 'Canvas 2D · Spring-Damper Physics', params: dotParams(110), desc: '同一套点阵的阅读版:点更淡、推开的范围和力度更小,没有冲击波,适合长文页面。', mount: dotField(true) });
|
|
253
|
+
</script>
|
|
254
|
+
<script>
|
|
255
|
+
MH.effects.find(e => e.id === 'dots').mount(document.getElementById('fx'), {
|
|
256
|
+
"gap": 26,
|
|
257
|
+
"radius": 150,
|
|
258
|
+
"spring": 0.06,
|
|
259
|
+
"damping": 0.82
|
|
260
|
+
}).resume();
|
|
261
|
+
</script>
|
|
262
|
+
</body>
|
|
263
|
+
</html>
|
|
@@ -0,0 +1,253 @@
|
|
|
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
|
+
- 每个粒子按所在位置的噪声值换算成角度前进(角度 = noise(x, y, t) × 2.4π),噪声随时间缓慢变化。
|
|
15
|
+
- 画布每帧用 globalCompositeOperation = "destination-out" 擦掉 7%,形成拖尾,画布本身保持透明,可以叠在任何背景上。
|
|
16
|
+
- 鼠标附近叠加切向速度形成漩涡,并给粒子打上“热度”,逐帧衰减。
|
|
17
|
+
- 粒子寿命到期或出界时随机重生。
|
|
18
|
+
|
|
19
|
+
【可以调的参数】
|
|
20
|
+
- 粒子数 = 面积 / 1100
|
|
21
|
+
- 噪声缩放 380
|
|
22
|
+
- 速度 1.1px/帧
|
|
23
|
+
- 拖尾擦除比例 0.07
|
|
24
|
+
- 漩涡半径 √32000 ≈ 179px
|
|
25
|
+
|
|
26
|
+
【注意】
|
|
27
|
+
- destination-out 擦除在 8 位精度下会残留极淡的痕迹,擦除比例别小于 0.05。
|
|
28
|
+
- 切换深浅色时要 clearRect 一次,否则旧颜色的拖尾会留着。
|
|
29
|
+
- 颜色都来自 CSS 变量(paper / ink / dim / rule / accent),换成项目自己的颜色即可。
|
|
30
|
+
- 要处理 prefers-reduced-motion:系统要求减少动态效果时只显示静止画面。
|
|
31
|
+
- 组件卸载时要停掉动画循环、移除事件监听(mount 返回的 destroy)。
|
|
32
|
+
|
|
33
|
+
【opts 参数】(mount 的第二个参数,不传就用默认值;文件末尾的 mount 调用里是你在 Motion Hub 上调好的值)
|
|
34
|
+
- density:粒子密度,默认 1(0.2–3×)
|
|
35
|
+
- speed:流速,默认 1.1(0.2–4)
|
|
36
|
+
- fade:拖尾消退,默认 0.07(0.02–0.3)
|
|
37
|
+
- swirl:漩涡半径,默认 180(40–400px)
|
|
38
|
+
|
|
39
|
+
【代码结构】
|
|
40
|
+
- <style>:配色变量和这个效果用到的样式
|
|
41
|
+
- 第一段 <script>:MH 核心(注册表、噪声、缓动;canvasHost 负责画布尺寸、指针坐标、动画循环和暂停)
|
|
42
|
+
- 第二段 <script>:效果本身,核心是 MH.register 里的 mount(el, opts),返回 { pause, resume, destroy }
|
|
43
|
+
- 最后一段 <script>:把效果挂到 #fx 上并启动
|
|
44
|
+
-->
|
|
45
|
+
<html lang="zh-CN">
|
|
46
|
+
<head>
|
|
47
|
+
<meta charset="utf-8">
|
|
48
|
+
<meta name="viewport" content="width=device-width,initial-scale=1">
|
|
49
|
+
<title>风场</title>
|
|
50
|
+
<style>
|
|
51
|
+
/* Motion Hub 效果样式:配色变量(Acid:浅色暖灰 + 紫罗兰,深色近黑 + 酸橙绿)+ 各效果用到的样式 + 整页过渡遮罩。按“@css 名称”标记分段,复制代码时只带用到的段。 */
|
|
52
|
+
:root{
|
|
53
|
+
--paper:#F1F0EC;--paper2:#E4E2DC;--ink:#141316;--body:#3A383F;--dim:#85828C;--rule:#D6D3CC;--accent:#6B4EFF;--on-accent:#fff;--hi:#C6F21E;
|
|
54
|
+
--serif:"Songti SC","STSong","Noto Serif SC",serif;
|
|
55
|
+
--sans:"PingFang SC","Helvetica Neue","Noto Sans SC",sans-serif;
|
|
56
|
+
--latin:"Iowan Old Style",Palatino,Georgia,serif;
|
|
57
|
+
--mono:"SF Mono",Menlo,monospace;
|
|
58
|
+
}
|
|
59
|
+
[data-theme="night"]{--paper:#111113;--paper2:#1B1B1F;--ink:#F2F1F5;--body:#C4C2CC;--dim:#7E7B88;--rule:#2A2A30;--accent:#C6F21E;--on-accent:#111113;--hi:#8E75FF}
|
|
60
|
+
[data-theme="day"]{--paper:#F1F0EC;--paper2:#E4E2DC;--ink:#141316;--body:#3A383F;--dim:#85828C;--rule:#D6D3CC;--accent:#6B4EFF;--on-accent:#fff;--hi:#C6F21E}
|
|
61
|
+
|
|
62
|
+
/* ───── 效果共用 ───── */
|
|
63
|
+
.mh-canvas{position:absolute;inset:0;width:100%;height:100%;display:block}
|
|
64
|
+
html,body{height:100%;margin:0;background:var(--paper)}
|
|
65
|
+
#fx{position:relative;height:100vh;overflow:hidden;container-type:size}
|
|
66
|
+
</style>
|
|
67
|
+
</head>
|
|
68
|
+
<body>
|
|
69
|
+
<div id="fx"></div>
|
|
70
|
+
<script>
|
|
71
|
+
// Motion Hub 核心:注册表 + 公共工具。所有效果文件都是普通 <script>(不是 ES 模块),
|
|
72
|
+
// 这样直接双击 index.html(file://)也能运行。每个效果调用 MH.register({...}) 把自己登记进来。
|
|
73
|
+
//
|
|
74
|
+
// 效果对象的约定:
|
|
75
|
+
// id 唯一标识(小写短横线)
|
|
76
|
+
// name 中文名
|
|
77
|
+
// cat 分类:背景 / 过渡 / 翻页 / 片头 / 交互
|
|
78
|
+
// tech 用到的技术(展示用)
|
|
79
|
+
// desc 一句话说明
|
|
80
|
+
// mount(el, opts) → { pause(), resume(), destroy() }
|
|
81
|
+
// el 是一个已经有尺寸的容器;opts.expanded 为 true 表示全屏体验(关掉幽灵光标、允许滚轮等)
|
|
82
|
+
// params 可调参数(可选),实验台据此生成参数面板,每项 { k, label, type, def, ... }:
|
|
83
|
+
// type 'range':数值滑块,带 min / max / step,可选 unit;'text':单行文字;'lines':多行文字,一行一项
|
|
84
|
+
// mount 拿到的 opts 已经按 def 补齐(register 里包了一层),效果代码直接读 opts.k 即可
|
|
85
|
+
window.MH = (() => {
|
|
86
|
+
const effects = [];
|
|
87
|
+
// 记下效果来自哪个文件(复制代码时只带这个文件)
|
|
88
|
+
const register = e => {
|
|
89
|
+
const src = document.currentScript && document.currentScript.src; e.file = e.file || (src ? src.split('/').pop() : '');
|
|
90
|
+
if (e.params) { const m = e.mount; e.mount = (el, opts = {}) => m.call(e, el, { ...defaults(e), ...opts }); }
|
|
91
|
+
effects.push(e);
|
|
92
|
+
};
|
|
93
|
+
const defaults = e => Object.fromEntries((e.params || []).map(p => [p.k, p.def]));
|
|
94
|
+
// 'lines' 参数:数组原样返回;字符串按行拆开,去掉空行
|
|
95
|
+
const lines = v => Array.isArray(v) ? v : String(v || '').split('\n').map(x => x.trim()).filter(Boolean);
|
|
96
|
+
const still = () => matchMedia('(prefers-reduced-motion: reduce)').matches;
|
|
97
|
+
const clamp = (v, a = 0, b = 1) => Math.max(a, Math.min(b, v));
|
|
98
|
+
const lerp = (a, b, k) => a + (b - a) * k;
|
|
99
|
+
const ease = {
|
|
100
|
+
in: t => t * t * t,
|
|
101
|
+
out: t => 1 - (1 - t) ** 3,
|
|
102
|
+
inOut: t => t < .5 ? 4 * t * t * t : 1 - (-2 * t + 2) ** 3 / 2,
|
|
103
|
+
back: t => { const c = 1.4; return 1 + (c + 1) * (t - 1) ** 3 + c * (t - 1) ** 2; },
|
|
104
|
+
};
|
|
105
|
+
const hash = i => { const x = Math.sin(i * 127.1 + 311.7) * 43758.5453; return x - Math.floor(x); };
|
|
106
|
+
|
|
107
|
+
// 二维 Perlin 噪声,seed 决定排列表
|
|
108
|
+
function perlin(seed = 1) {
|
|
109
|
+
const P = new Uint8Array(512), p = [...Array(256).keys()];
|
|
110
|
+
let s = seed;
|
|
111
|
+
for (let i = 255; i > 0; i--) { s = (s * 16807) % 2147483647; const j = s % (i + 1); [p[i], p[j]] = [p[j], p[i]]; }
|
|
112
|
+
for (let i = 0; i < 512; i++) P[i] = p[i & 255];
|
|
113
|
+
const fade = t => t * t * t * (t * (t * 6 - 15) + 10), grad = (h, x, y) => ((h & 1) ? -x : x) + ((h & 2) ? -y : y);
|
|
114
|
+
return (x, y) => {
|
|
115
|
+
const X = Math.floor(x) & 255, Y = Math.floor(y) & 255; x -= Math.floor(x); y -= Math.floor(y);
|
|
116
|
+
const u = fade(x), v = fade(y), a = P[X] + Y, b = P[X + 1] + Y;
|
|
117
|
+
const l1 = grad(P[a], x, y) + u * (grad(P[b], x - 1, y) - grad(P[a], x, y));
|
|
118
|
+
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));
|
|
119
|
+
return l1 + v * (l2 - l1);
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// 画布按设备像素比放大,画的时候仍用 CSS 像素坐标
|
|
124
|
+
function fit(cv, w, h) {
|
|
125
|
+
const dpr = Math.min(devicePixelRatio || 1, 2);
|
|
126
|
+
cv.width = Math.round(w * dpr); cv.height = Math.round(h * dpr);
|
|
127
|
+
const ctx = cv.getContext('2d'); ctx.setTransform(dpr, 0, 0, dpr, 0, 0); return ctx;
|
|
128
|
+
}
|
|
129
|
+
// 从容器读主题色(--paper / --ink / --dim / --rule / --accent)
|
|
130
|
+
function colors(el) {
|
|
131
|
+
const cs = getComputedStyle(el), v = n => cs.getPropertyValue(n).trim();
|
|
132
|
+
const t = el.closest('[data-theme]');
|
|
133
|
+
return { paper: v('--paper'), ink: v('--ink'), dim: v('--dim'), rule: v('--rule'), accent: v('--accent'), night: !!t && t.dataset.theme === 'night' };
|
|
134
|
+
}
|
|
135
|
+
// 一段时长 ms 的补间,fn(k) 中 k 从 0 到 1;减少动态效果时直接到终点
|
|
136
|
+
const tween = (ms, fn) => new Promise(res => {
|
|
137
|
+
if (still()) { fn(1); return res(); }
|
|
138
|
+
const t0 = performance.now();
|
|
139
|
+
const f = now => { const k = clamp((now - t0) / ms); fn(k); k < 1 ? requestAnimationFrame(f) : res(); };
|
|
140
|
+
requestAnimationFrame(f);
|
|
141
|
+
});
|
|
142
|
+
// 尺寸缩放系数:全屏约 1,缩略图约 0.45,让小画面像“整页缩小”而不是挤满细节
|
|
143
|
+
const scaleOf = W => clamp(W / 1200, .45, 1);
|
|
144
|
+
|
|
145
|
+
// 幽灵光标:没人操作时,沿一条利萨如曲线在容器里慢慢移动,让缩略图自己演示交互
|
|
146
|
+
const ghostAt = (now, W, H, seed = 0) => {
|
|
147
|
+
const t = now / 1000 + seed * 7;
|
|
148
|
+
return { x: W * (.5 + .32 * Math.sin(t * .55) * Math.cos(t * .21)), y: H * (.5 + .3 * Math.sin(t * .83 + 1.3)) };
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
// 效果宿主:(可选)建画布、跟随尺寸、换算局部指针坐标、管动画循环和暂停。
|
|
152
|
+
// setup(h) 返回 { resize(W,H), frame(now), down(x,y), theme(), ghostClick },h 提供 ctx / W / H / P / s / colors()
|
|
153
|
+
// ghostClick: false 表示幽灵光标不替它“点击”(例如片头,点一下会重播)
|
|
154
|
+
function canvasHost(el, opts, setup, { canvas = true } = {}) {
|
|
155
|
+
const cv = canvas ? document.createElement('canvas') : null;
|
|
156
|
+
if (cv) { cv.className = 'mh-canvas'; el.append(cv); }
|
|
157
|
+
let ctx, W = 0, H = 0, raf = 0, running = false, lastReal = -1e9, lastGhostClick = 0;
|
|
158
|
+
const P = { x: -9999, y: -9999, real: false };
|
|
159
|
+
const seed = hash(effects.length + el.childElementCount);
|
|
160
|
+
const h = { cv, get ctx() { return ctx; }, get W() { return W; }, get H() { return H; }, P, s: 1, colors: () => colors(el), expanded: !!opts.expanded };
|
|
161
|
+
const api = setup(h);
|
|
162
|
+
const size = () => {
|
|
163
|
+
const r = el.getBoundingClientRect(); if (!r.width || !r.height) return;
|
|
164
|
+
W = r.width; H = r.height; h.s = scaleOf(W); if (cv) ctx = fit(cv, W, H); api.resize && api.resize(W, H);
|
|
165
|
+
if (!running) api.frame(performance.now());
|
|
166
|
+
};
|
|
167
|
+
const ro = new ResizeObserver(size); ro.observe(el);
|
|
168
|
+
const local = e => { const r = el.getBoundingClientRect(); return [e.clientX - r.left, e.clientY - r.top]; };
|
|
169
|
+
const onMove = e => { [P.x, P.y] = local(e); P.real = true; lastReal = performance.now(); };
|
|
170
|
+
const onLeave = () => { P.real = false; P.x = P.y = -9999; };
|
|
171
|
+
const onDown = e => { if (e.target.closest('a, button')) return; lastReal = performance.now(); const [x, y] = local(e); api.down && api.down(x, y); };
|
|
172
|
+
el.addEventListener('pointermove', onMove); el.addEventListener('pointerleave', onLeave); el.addEventListener('pointerdown', onDown);
|
|
173
|
+
const mo = new MutationObserver(() => { api.theme && api.theme(); if (!running) api.frame(performance.now()); });
|
|
174
|
+
mo.observe(document.documentElement, { attributes: true, attributeFilter: ['data-theme', 'data-palette'] });
|
|
175
|
+
const tick = now => {
|
|
176
|
+
if (!running) return;
|
|
177
|
+
// 缩略图里 2.5 秒没人碰,就让幽灵光标接管;偶尔“点”一下展示点击效果
|
|
178
|
+
if (!h.expanded && now - lastReal > 2500) {
|
|
179
|
+
const g = ghostAt(now, W, H, seed); P.x = g.x; P.y = g.y;
|
|
180
|
+
if (api.down && api.ghostClick !== false && now - lastGhostClick > 4200) { lastGhostClick = now; api.down(g.x, g.y); }
|
|
181
|
+
}
|
|
182
|
+
if (W && H) api.frame(now);
|
|
183
|
+
raf = requestAnimationFrame(tick);
|
|
184
|
+
};
|
|
185
|
+
size();
|
|
186
|
+
return {
|
|
187
|
+
pause() { running = false; cancelAnimationFrame(raf); },
|
|
188
|
+
resume() { if (running) return; if (still()) { api.frame(performance.now()); return; } running = true; raf = requestAnimationFrame(tick); },
|
|
189
|
+
destroy() { this.pause(); ro.disconnect(); mo.disconnect(); el.removeEventListener('pointermove', onMove); el.removeEventListener('pointerleave', onLeave); el.removeEventListener('pointerdown', onDown); cv && cv.remove(); },
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
// 让 CSS 能对自定义属性做补间(遮罩半径等)
|
|
194
|
+
if (window.CSS && CSS.registerProperty) {
|
|
195
|
+
for (const name of ['--vr', '--vw']) { try { CSS.registerProperty({ name, syntax: '<length>', inherits: false, initialValue: '0px' }); } catch (e) { /* 已注册 */ } }
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
return { effects, register, defaults, lines, still, clamp, lerp, ease, hash, perlin, fit, colors, tween, scaleOf, ghostAt, canvasHost };
|
|
199
|
+
})();
|
|
200
|
+
</script>
|
|
201
|
+
<script>
|
|
202
|
+
// 背景动效:画在 canvas 上,尺寸跟随容器,h.s 是缩放系数(全屏约 1,缩略图约 0.45)。
|
|
203
|
+
|
|
204
|
+
// ───── 风场:粒子顺着噪声角度流动,画布每帧轻微擦掉一点形成拖尾;鼠标附近卷成漩涡 ─────
|
|
205
|
+
MH.register({
|
|
206
|
+
id: 'flow', name: '风场', cat: '背景', tech: 'Canvas 2D · Particles · Noise Flow Field',
|
|
207
|
+
desc: '几千个粒子顺着看不见的风流动,留下淡淡的拖尾;鼠标附近被卷成漩涡,经过的粒子变成强调色。',
|
|
208
|
+
params: [
|
|
209
|
+
{ k: 'density', label: '粒子密度', type: 'range', min: 0.2, max: 3, step: 0.1, def: 1, unit: '×' },
|
|
210
|
+
{ k: 'speed', label: '流速', type: 'range', min: 0.2, max: 4, step: 0.1, def: 1.1 },
|
|
211
|
+
{ k: 'fade', label: '拖尾消退', type: 'range', min: 0.02, max: 0.3, step: 0.01, def: 0.07 },
|
|
212
|
+
{ k: 'swirl', label: '漩涡半径', type: 'range', min: 40, max: 400, step: 10, def: 180, unit: 'px' },
|
|
213
|
+
],
|
|
214
|
+
mount: (el, opts) => MH.canvasHost(el, opts, h => {
|
|
215
|
+
const noise = MH.perlin(11);
|
|
216
|
+
let C = h.colors(), ps = [];
|
|
217
|
+
const spawn = p => { p.x = Math.random() * h.W; p.y = Math.random() * h.H; p.life = 80 + Math.random() * 160; p.hot = 0; return p; };
|
|
218
|
+
return {
|
|
219
|
+
resize(W, H) { ps = Array.from({ length: Math.round(W * H / (1100 * h.s * h.s) * opts.density) }, () => spawn({})); },
|
|
220
|
+
theme() { C = h.colors(); h.ctx && h.ctx.clearRect(0, 0, h.W, h.H); },
|
|
221
|
+
frame(now) {
|
|
222
|
+
const { ctx, W, H, P, s } = h, t = now / 1000, SC = 380 * s, R2 = (opts.swirl * s) ** 2;
|
|
223
|
+
ctx.globalCompositeOperation = 'destination-out'; ctx.fillStyle = `rgba(0,0,0,${opts.fade})`; ctx.fillRect(0, 0, W, H);
|
|
224
|
+
ctx.globalCompositeOperation = 'source-over';
|
|
225
|
+
const cold = new Path2D(), warm = new Path2D();
|
|
226
|
+
for (const p of ps) {
|
|
227
|
+
const a = noise(p.x / SC + t * .03, p.y / SC) * Math.PI * 2.4;
|
|
228
|
+
let vx = Math.cos(a) * opts.speed * s, vy = Math.sin(a) * opts.speed * s;
|
|
229
|
+
const dx = p.x - P.x, dy = p.y - P.y, d2 = dx * dx + dy * dy;
|
|
230
|
+
if (d2 < R2) { const d = Math.sqrt(d2) + 1, f = (1 - d2 / R2) * 5 * s; vx += -dy / d * f + dx / d * f * .25; vy += dx / d * f + dy / d * f * .25; p.hot = 1; }
|
|
231
|
+
p.hot *= .97;
|
|
232
|
+
const path = p.hot > .25 ? warm : cold;
|
|
233
|
+
path.moveTo(p.x, p.y); p.x += vx; p.y += vy; path.lineTo(p.x, p.y);
|
|
234
|
+
if (--p.life < 0 || p.x < -10 || p.x > W + 10 || p.y < -10 || p.y > H + 10) spawn(p);
|
|
235
|
+
}
|
|
236
|
+
ctx.lineWidth = 1;
|
|
237
|
+
ctx.strokeStyle = C.dim; ctx.globalAlpha = C.night ? .45 : .5; ctx.stroke(cold);
|
|
238
|
+
ctx.strokeStyle = C.accent; ctx.globalAlpha = .9; ctx.stroke(warm); ctx.globalAlpha = 1;
|
|
239
|
+
},
|
|
240
|
+
};
|
|
241
|
+
}),
|
|
242
|
+
});
|
|
243
|
+
</script>
|
|
244
|
+
<script>
|
|
245
|
+
MH.effects.find(e => e.id === 'flow').mount(document.getElementById('fx'), {
|
|
246
|
+
"density": 1,
|
|
247
|
+
"speed": 1.1,
|
|
248
|
+
"fade": 0.07,
|
|
249
|
+
"swirl": 180
|
|
250
|
+
}).resume();
|
|
251
|
+
</script>
|
|
252
|
+
</body>
|
|
253
|
+
</html>
|