基于WebView2实现的Win11桌面框架,利用插件自定义
Moli
2026-05-03 c3a5f58e60f85400edfb5f5379145390316394c5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<!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>