我怎样才能使这个copepen项目成为我网站的背景?

问题描述 投票:0回答:1

很抱歉这个愚蠢的问题,但我想知道如何才能将此JavaScript用作我网站的背景。对JavaScript而言相对较新,并且不确定如何将其作为背景来实现。

链接到密码笔:

'''https://codepen.io/strangerintheq/pen/JjdZKEa'''
javascript html background
1个回答
0
投票

免责声明

首先-甚至不要考虑尝试了解其中发生的一切。作为初学者,您应该真正提高自己的水平,并在某个时候发明自己的幻想项目。

实施

HTML

<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)开始。

© www.soinside.com 2019 - 2024. All rights reserved.