1 回答

TA貢獻1829條經驗 獲得超7個贊
免責聲明
首先——不要試圖去理解其中發生的一切。作為一個初學者,你應該真正努力向上,并在某個時候發明你自己的奇特項目。
執行
超文本標記語言
<head>
<!-- Other stuff -->
<script defer src="https://raw.githack.com/strangerintheq/ShaderToy/master/ShaderToy.js"></script>
<script defer src="shader.js"></script>
<!-- Other scripts of yours -->
</head>
您基本上是在添加兩個腳本。第一個來自網站,第二個您必須自己復制(即您可以在 Codepen JS 部分看到的那個)。
JS
// filename: "shader.js"
// https://thelig.ht/chladni/
let uf = {xy: '2f'};
addEventListener('mousemove', e => uf.xy([e.x/innerWidth,e.y/innerHeight]))
new ShaderToy(`void main(void) {
const float PI = 3.14159265;
vec2 p = (2.0 * gl_FragCoord.xy - resolution.xy) / resolution.y;
vec4 s1 = vec4(1.0, 1.0, 1.0, 2.0);
vec4 s2 = vec4(-4.0, 4.0, 4.0, 4.6);
float tx = sin(time)*0.1;
float ty = cos(time)*0.1;
float a = mix(s1.x, s2.x, xy.x+tx);
float b = mix(s1.y, s2.y, xy.x+tx);
float n = mix(s1.z, s2.z, xy.y+ty);
float m = mix(s1.w, s2.w, xy.y+ty);
float max_amp = abs(a) + abs(b);
float amp = a * sin(PI*n*p.x) * sin(PI*m*p.y) + b * sin(PI*m*p.x) * sin(PI*n*p.y);
float col = 1.0 - smoothstep(abs(amp), 0.0, 0.1);
gl_FragColor = vec4(vec3(col), 1.0);
}`, {uniforms:uf}).fullscreen().loop();
我沒有費心去檢查那里發生了什么。如果您有興趣,也許您喜歡查找一些有關著色器的教程。
CSS
canvas {
z-index: -1;
position: fixed;
top: 0;
}
將其添加到外部樣式表或頭部部分。
該腳本的工作原理是在頁面上創建畫布。此 CSS 將其移動到頁面上每個元素的后面 ( z-index: -1),使其不可移動 ( position: fixed) 并從窗口的最上邊緣開始 ( top: 0)。
- 1 回答
- 0 關注
- 123 瀏覽
添加回答
舉報