js实现一个canvas粒子流动动画效果代码

代码语言:html

所属分类:粒子

代码描述:js实现一个canvas粒子流动动画效果代码,点击可改变流动的方向。

代码标签: canvas 粒子 流动 动画

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

<!doctype html>
<html>

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


    <style>
        html, body {
        	overflow: hidden;
        	margin: 0;
        	padding: 0;
        	width: 100%;
        	height: 100%;
        	background:#000;
        	touch-action: none;
        	content-zooming: none;
        }
        canvas {
        	position: absolute;
        	width: 100%;
        	height: 100%;
        	background:#000;
        	cursor: pointer;
        }
    </style>

</head>

<body>

    <div></div>

    <script>
        "use strict";
        {
        	const perlin = {
        		init () {
        			this.p = new Uint8Array(512);
        			this.reset();
        		},
        		reset() {
        			for (let i = 0; i < 512; ++i) {
        				this.p[i] = Math.random() * 256;
        			}
        		},
        		lerp(t, a, b) {
        			return a + t * (b - a);
        		},
        		grad2d(i, x, y) {
        			const v = (i & 1) === 0 ? x : y;
        			return (i & 2) === 0 ? -v : v;
        		},
        		noise2d(x2d, y2d) {
        			const X = Math.floor(x2d) & 255;
        			const Y = Math.floor(y2d) & 255;
        			const x = x2d - Math.floor(x2d);
        			const y = y2d - Math.floor(y2d);
        			const fx = (3 - 2 * x) * x * x;
        			const fy = (3 - 2 * y) * y * y;
        			const p0 = this.p[X] + Y;
        			const p1 = this.p[X + 1] + Y;
        			return this.lerp(
        				fy,
        				this.lerp(
        					fx,
        					this.grad2d(this.p[p0], x, y),
        					this.grad2d(this.p[p1], x - 1, y)
        				),
        				this.lerp(
        					fx,
        					this.grad2d(this.p[p0 + 1], x, y - 1),
        					this.grad2d(this.p[p1 + 1], x - 1, y - 1)
        				)
        			);
        		}
        	};
        /////////////////////////////////////////////////////////////////
        	const canvas = {
        		init() {
        			this.elem = document.createElement("canvas");
        			document.body.appendChild(this.elem);
        			this.width = this.elem.width = this.elem.offsetWidth;
        			this.height = this.elem.height = this.elem.offsetHeight;
        			return this.elem.getContext("2d");
        		}
        	};
        /////////////////////////////////////////////////////////////////
        	const webgl = {
        		init(canvas, options) {
        			this.elem = document.createElement("canvas");
        			this.gl = (
        				this.elem.getContext("webgl", options) ||
        				this.elem.getContext("experimental-webgl", options)
        			);
        			if (!this.gl) return false;
        			const vertexShader = this.gl.createShader(this.gl.VERTEX_SHADER);
        			this.gl.shaderSource(vertexShader, `
        				precision highp float;
        				attribute vec3 aPosition;
        				uniform vec2 uResolution;
        				void main() {
        					gl_PointSize = 1.0;
        					gl_Position = vec4(
        						( aPosition.x / uResolution.x * 2.0) - 1.0, 
        						(-aPosition.y / uResolution.y * 2.0) + 1.0, 
        						0.0,
        						1.0
        					);
        				}`
        			);
        			this.gl.compileShader(vertexShader);
        			const fragmentShader = this.gl.createShader(this.gl.FRAGMENT_SHADER);
        			this.gl.shaderSource(fragmentShader, `
        				precision highp float;
        				void main() {
        					gl_FragColor = vec4(0.2, 0.3, 1.0, 1.0);
        				}`
        			);
        			this.gl.compileShader(frag.........完整代码请登录后点击上方下载按钮下载查看

网友评论0