javascript - 我怎样才能让这个 Codepen 项目成为我网站的背景?

标签 javascript html background

很抱歉问了这个愚蠢的问题,但我想知道如何使这个 javascript 成为我网站的背景。对于 JavaScript 来说相对较新,我不太确定如何将其实现为背景。

代码笔链接:

'''https://codepen.io/strangerintheq/pen/JjdZKEa'''

最佳答案

免责声明

首先 - 不要试图理解其中发生的一切。作为初学者,您应该真正努力向上,并在某个时候发明自己的奇特项目。

实现

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;
}

将其添加到外部样式表或头部部分。
该脚本的工作原理是在页面上创建 Canvas 。此 CSS 将其移动到页面上每个元素的后面 (z-index: -1),使其不可移动 (position:fixed),并从页面的最上边缘开始窗口(顶部:0)。

关于javascript - 我怎样才能让这个 Codepen 项目成为我网站的背景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60766791/

相关文章:

javascript - 尝试使用 react-leaflet-choropleth : Cannot read property 'map' of undefined 映射等值线时出错

html - 如何修复我的媒体查询不响应移动设备显示(跨所有平台)?

html - 如何将我的前端开发人员构建的 HTML 页面添加到 Rails 元素中?

css - 如何去除使用线性渐变属性时出现的条纹

javascript - javascript验证中名称字段的正则表达式

javascript - 在 TinyMCE 4 中,editor.onBeforeSetContent.add() 已弃用,用什么代替?

iOS后台获取清除之前的通知

html - Logo 背景缩放 不使 Logo 缩放

javascript - 在文档加载和函数调用时选择不同的行

javascript - 使用 WebRTC 将数据与视频同步