canvas+webgl实现灵感线条飘动动画效果代码

代码语言:html

所属分类:动画

代码描述:canvas+webgl实现灵感线条飘动动画效果代码

代码标签: canvas webgl 灵感 线条 飘动 动画

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  

  
  
</head>

<body translate="no">
  <script type="x-shader/x-fragment">#version 300 es
/*********
* made by Matthias Hurrle (@atzedent)
*/   
precision highp float;
out vec4 O;
uniform float time;
uniform vec2 resolution;
#define FC gl_FragCoord.xy
#define R resolution
#define T time
#define S smoothstep
#define MN min(R.x,R.y)
#define SE(v,s) S(s+1./MN,s-1./MN,v)
float pattern(vec2 uv) {
	float d=.0;
	for (float i=.0; i<3.; i++) {
		uv.x+=sin(T*(1.+i)+uv.y*1.5)*.2;
		d+=.005/abs(uv.x);
	}
	return d;	
}
vec3 scene(vec2 uv) {
	vec3 col=vec3(0);
	uv=vec2(atan(uv.x,uv.y)*2./6.28318,-log(length(uv))+T);
	for (float i=.0; i<3.; i++) {
		int k=int(mod(i,3.));
		col[k]+=pattern(uv+i*6./MN);
	}
	return col;
}
void main() {
	vec2 uv=(FC-.5*R)/MN;
	vec3 col=vec3(0);
	float s=12., e=9e-4;
	col+=e/(sin(uv.x*s)*cos(uv.y*s));
	uv.y+=R.x>R.y?.5:.5*(R.y/R.x);
	col+=scene(uv);
	O=vec4(col,1);
}</script>
  
      <script  >
window.onload = init
function init() {
  let renderer, canvas
  const dpr = Math.max(1, devicePixelRatio)
  const resize = () => {
    const { innerWidth: width, innerHeight: height } = window
    canvas.width = width * dpr
    canvas.height = height * dpr
    if (renderer) {
      renderer.updateScale(dpr)
    }
  }
  const source = document.querySelector("script[type='x-shader/x-fragment']").textContent
  canvas = document.createElement("canvas")
  document.title = "✨ Happy Birthday Mustafa"
  document.body.innerHTML = ""
  document.body.appendChild(canvas)
  document.body.style = "margin:0;touch-action:none;overflow:hidden"
  canvas.style.width = "100%"
  canvas.style.height = "auto"
  canvas.style.userSelect = "none"
  renderer = new Renderer(canvas, dpr)
  renderer.setup()
  renderer.init()
  resize()
  if (renderer.test(source) === null) {
    renderer.updateShader(source)
  }
  window.onresize = resize
  const loop = (now) => {
    renderer.render(now)
    requestAnimationFrame(loop)
  }
  loop(0)
}
class Renderer {
  #vertexSrc = "#version 300 es\nprecision highp float;\nin vec4 position;\nvoid main(){gl_Position=position;}"
  #fragmtSrc = "#version 300 es\nprecision highp float;\nout vec4 O;\nuniform float time;\nuniform vec2 resolution;\nvoid main() {\n\tvec2 uv=gl_FragCoord.xy/resolution;\n\tO=vec4(uv,sin(time)*.5+.5,1);\n}"
  #vertices = [-1, 1, -1, -1, 1, 1, 1, -1]
  constructor(canvas, scale) {
    this.canvas = canvas
    this.scale = scale
    this.gl = canvas.getContext("webgl2")
    this.gl.viewport(0, 0, canvas.width * scale, canvas.height * scale)
    this.shaderSource = this.#fragmtSrc
    this.mouseCoords = [0, 0]
    this.pointerCoords = [0, 0]
    this.nbrOfPointers = 0
  }
  get defaultSource() { return this.#fragmtSrc }
  updateShader(source) {
    this.reset()
    this.shaderSource = source
    this.setup()
    this.init()
  }
  updateMouse(coords) {
    this.mouseCoords = coords
  }
  updatePointerCoords(coords) {
    this.pointerCoords = coords
  }
  updatePointerCount(nbr) {
    this.nbrOfPointers =.........完整代码请登录后点击上方下载按钮下载查看

网友评论0