<!doctype html>
|
<html lang="zh-CN">
|
<head>
|
<meta charset="utf-8">
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
<title>Music Visualizer</title>
|
<style>
|
html,
|
body {
|
width: 100%;
|
height: 100%;
|
margin: 0;
|
overflow: hidden;
|
background: transparent;
|
user-select: none;
|
}
|
|
#stage {
|
width: 100vw;
|
height: 100vh;
|
position: relative;
|
box-sizing: border-box;
|
background: transparent;
|
--base-y: 58%;
|
--glow: rgba(255, 47, 179, 0.42);
|
--cool: #29d8ff;
|
--hot: #ff2fb3;
|
--accent: #f8fbff;
|
}
|
|
#stage::before {
|
content: "";
|
position: absolute;
|
left: 0;
|
right: 0;
|
top: var(--base-y);
|
height: 1px;
|
background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.72), transparent);
|
box-shadow:
|
0 0 8px rgba(255, 255, 255, 0.7),
|
0 0 22px var(--glow);
|
opacity: 0.86;
|
}
|
|
.bars {
|
position: absolute;
|
inset: 0 10px;
|
display: flex;
|
justify-content: space-between;
|
align-items: flex-start;
|
gap: 2px;
|
}
|
|
.bar {
|
--level: 0.16;
|
--bar-height: 7%;
|
--bar-top: 51%;
|
--bar-opacity: 0.43;
|
--reflection-height: 4vh;
|
--reflection-opacity: 0.06;
|
position: relative;
|
flex: 1 1 auto;
|
min-width: 1px;
|
max-width: 4px;
|
height: var(--bar-height);
|
margin-top: var(--bar-top);
|
border-radius: 999px;
|
transform-origin: bottom center;
|
background:
|
linear-gradient(180deg, var(--accent), var(--hot) 36%, var(--cool));
|
box-shadow:
|
0 0 5px rgba(255, 255, 255, 0.72),
|
0 0 12px var(--glow);
|
opacity: var(--bar-opacity);
|
transition:
|
height 64ms linear,
|
margin-top 64ms linear,
|
opacity 64ms linear;
|
}
|
|
.bar::after {
|
content: "";
|
position: absolute;
|
width: inherit;
|
min-width: inherit;
|
max-width: inherit;
|
height: var(--reflection-height);
|
transform: translateY(100%) scaleY(0.72);
|
transform-origin: top center;
|
border-radius: inherit;
|
background: linear-gradient(180deg, rgba(255, 47, 179, 0.26), rgba(41, 216, 255, 0.02));
|
filter: blur(1px);
|
opacity: var(--reflection-opacity);
|
}
|
</style>
|
</head>
|
<body>
|
<main id="stage" aria-hidden="true">
|
<div id="bars" class="bars"></div>
|
</main>
|
<script>
|
const stage = document.getElementById("stage");
|
const barRoot = document.getElementById("bars");
|
const settings = {
|
barCount: 192,
|
primaryColor: "#ff2fb3",
|
secondaryColor: "#29d8ff",
|
accentColor: "#f8fbff",
|
sensitivity: 1.42
|
};
|
let hostLevels = null;
|
let lastHostSampleAt = 0;
|
let fallbackPhase = 0;
|
let bars = [];
|
|
function applyConfig(config) {
|
Object.assign(settings, config || {});
|
stage.style.setProperty("--hot", settings.primaryColor);
|
stage.style.setProperty("--cool", settings.secondaryColor);
|
stage.style.setProperty("--accent", settings.accentColor);
|
stage.style.setProperty("--glow", `${hexToRgb(settings.primaryColor, "255, 47, 179")}, 0.42)`.replace(/^/, "rgba("));
|
const count = Math.max(96, Math.min(320, Number(settings.barCount) || 192));
|
if (bars.length === count) {
|
return;
|
}
|
|
barRoot.textContent = "";
|
bars = Array.from({ length: count }, () => {
|
const node = document.createElement("i");
|
node.className = "bar";
|
barRoot.appendChild(node);
|
return node;
|
});
|
}
|
|
function sample(index) {
|
if (hostLevels && performance.now() - lastHostSampleAt < 420) {
|
const bin = Math.min(hostLevels.length - 1, Math.floor(index / Math.max(1, bars.length) * hostLevels.length));
|
const value = Number(hostLevels[bin]) || 0;
|
const neighbor = (Number(hostLevels[Math.max(0, bin - 1)]) || 0) * 0.16
|
+ (Number(hostLevels[Math.min(hostLevels.length - 1, bin + 1)]) || 0) * 0.16;
|
return Math.min(1, Math.pow(value, 0.72) * settings.sensitivity + neighbor);
|
}
|
|
fallbackPhase += 0.0025;
|
const main = Math.sin(index * 0.18 + fallbackPhase * 54) * 0.5 + 0.5;
|
const ripple = Math.sin(index * 0.047 - fallbackPhase * 22) * 0.5 + 0.5;
|
const burst = Math.pow(Math.sin(index * 0.022 + fallbackPhase * 13) * 0.5 + 0.5, 7);
|
return 0.08 + main * 0.18 + ripple * 0.12 + burst * 0.58;
|
}
|
|
function draw() {
|
bars.forEach((bar, index) => {
|
const centerBias = Math.sin((index / Math.max(1, bars.length - 1)) * Math.PI);
|
const value = Math.min(1, sample(index) * (0.72 + centerBias * 0.36));
|
const height = value * 44;
|
const top = Math.max(0, 58 - height);
|
bar.style.setProperty("--level", value.toFixed(3));
|
bar.style.setProperty("--bar-height", `${height.toFixed(2)}%`);
|
bar.style.setProperty("--bar-top", `${top.toFixed(2)}%`);
|
bar.style.setProperty("--bar-opacity", (0.32 + value * 0.68).toFixed(3));
|
bar.style.setProperty("--reflection-height", `${(value * 26).toFixed(2)}vh`);
|
bar.style.setProperty("--reflection-opacity", (value * 0.36).toFixed(3));
|
});
|
requestAnimationFrame(draw);
|
}
|
|
function hexToRgb(hex, fallback) {
|
const match = String(hex || "").trim().match(/^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i);
|
if (!match) {
|
return fallback;
|
}
|
|
return `${parseInt(match[1], 16)}, ${parseInt(match[2], 16)}, ${parseInt(match[3], 16)}`;
|
}
|
|
window.addEventListener("moli:widget-loaded", async () => {
|
if (window.moliDesktop?.getConfig) {
|
applyConfig(await window.moliDesktop.getConfig());
|
}
|
});
|
window.addEventListener("moli:audio-spectrum", event => {
|
const levels = event.detail?.levels;
|
if (Array.isArray(levels) && levels.length > 0) {
|
hostLevels = levels;
|
lastHostSampleAt = performance.now();
|
}
|
});
|
window.addEventListener("moli:config-changed", event => applyConfig(event.detail));
|
applyConfig();
|
draw();
|
</script>
|
</body>
|
</html>
|