webgl实现粒子汇聚成文字动画效果代码
代码语言:html
所属分类:粒子
代码描述:webgl实现粒子汇聚成文字动画效果代码
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<!DOCTYPE html> <html lang="en" > <head> <meta charset="UTF-8"> <style> body { margin: 0; overflow: hidden; } a { position: fixed; display: inline-block; margin-left: calc(50vw - 70px); font-size:20px; z-index:1; color: white; } </style> </head> <body > <script > let canvas = document.createElement('canvas'); canvas.style.background = '#000'; document.body.appendChild(canvas); let gl = canvas.getContext("webgl", { preserveDrawingBuffer: true }); let clearPass = program({ type: gl.TRIANGLES, vertexSize: 2, vertices: [-1, 3, -1, -1, 3, -1], // full screen triangle shaders: [` attribute vec2 pt; void main(void) { gl_Position=vec4(pt, 0.0, 1.0); }`, ` void main(void) { gl_FragColor=vec4(0.0, 0.0, 0.0, 0.05); }`] }); let particles = program({ type: gl.POINTS, vertexSize: 4, vertices: createVertices(), uniforms: { time: '1f', resolution: '2f' }, shaders: [` uniform float time; uniform vec2 resolution; attribute vec4 pt; varying float i; void main() { float aspect = resolution.y/resolution.x; float t = smoothstep(0.0, 1.0, fract(time*0.3)); t += mod(floor(time*0.3), 2.0); t *= 3.1415; i = fract(pt.x*pt.y*pt.z*pt.w); vec2 c = (pt.xy + pt.zw)/2.0; float a = atan(pt.y - c.y, pt.x - c.x); float r = length(vec2(pt.x - c.x, pt.y - c.y)); r -= sin(t*3.)*0.005; c += vec2(cos(a+t), sin(a+t)) * r; gl_Position = vec4(c.x*aspect, c.y, 0., 1.); gl_PointSize = 10.0; }`, ` uniform float time; varying float i; float ch(vec2 uv) { float d = length(uv - 0.5); return smoothstep(0.2, 0.00, d); } void main() { float d = length(gl_PointCoord.xy-0.5); d = smoothstep(0.5, 0.00, d); gl_FragColor = vec4( ch(gl_PointCoord.xy-0.25*sin(time+i)), ch(gl_PointCoord.xy), ch(gl_PointCoord.xy+0.25*sin(time+i)), 0.01 ); }`] }); gl.enable(gl.BLEND); requestAnimationFrame(function draw(t) { gl.blendFunc(gl.ONE, gl.ONE); // additive particles.bind(); if (canvas.width != innerWidth || canvas.height !== innerHeight) { particles.uniforms.resolution([innerWidth, innerHeight]); g.........完整代码请登录后点击上方下载按钮下载查看
网友评论0