Toggle navigation
在线编辑器
在线代码
文本比较
jQuery下载
前端库
在线手册
登录/注册
下载代码
html
css
js
分享到微信朋友圈
X
html
css
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; }
JavaScript
"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(fragmentShader); const program = this.gl.createProgram(); this.gl.attachShader(program, vertexShader); this.gl.attachShader(program, fragmentShader); this.gl.linkProgram(program); this.gl.useProgram(program); this.aPosition = this.gl.getAttribLocation(program, "aPosition"); this.gl.enableVertexAttribArray(this.aPosition); this.positionBuffer = this.gl.createBuffer(); this.elem.width = canvas.width; this.elem.height = canvas.height; const uResolution = this.gl.getUniformLocation(program, "uResolution"); this.gl.enableVertexAttribArray(uResolution); this.gl.uniform2f(uResolution, canvas.width, canvas.height); this.gl.viewport( 0, 0, this.gl.drawingBufferWidth, this.gl.drawingBufferHeight ); return this.gl; }, drawBuffer(data, num) { this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.positionBuffer); this.gl.vertexAttribPointer(this.aPosition, 2, this.gl.FLOAT, false, 0, 0); this.gl.bufferData( this.gl.ARRAY_BUFFER, data, this.gl.DYNAMIC_DRAW ); this.gl.drawArrays(this.gl.GL_POINTS, 0, num); } }; ///////////////////////////////////////////////////////////////// const ctx = canvas.init(); const gl = webgl.init(canvas, { alpha: false, stencil: false, antialias: false, depth: false, }); perlin.init(); const nParticles = 1000; const velocities = new Float32Array(nParticles * 2); const particles = new Float32Array(nParticles * 2); let frame = 0; for (let i = 0; i < nParticles; i++) { const p = i * 2; particles[p+0] = Math.random() * canvas.width; particles[p+1] = Math.random() * canvas.height; } ///////////////////////////////////////////////////////////////// const run = () => { requestAnimationFrame(run); frame++; gl.clear(gl.COLOR_BUFFER_BIT); ctx.globalCompositeOperation = "source-over"; ctx.fillStyle = "rgba(0, 0, 0, 0.025)"; ctx.fillRect(0, 0, canvas.width, canvas.height); ctx.globalCompositeOperation = "lighter"; for (let i = 0; i < nParticles; i++) { const p = i * 2; let n = 80 * perlin.noise2d(particles[p+0] * 0.001, particles[p+1] * 0.001); velocities[p+0] += 0.1 * Math.cos(n); velocities[p+1] += 0.1 * Math.sin(n); particles[p+0] += (velocities[p+0] *= 0.99); particles[p+1] += (velocities[p+1] *= 0.99); particles[p+0] = (canvas.width + particles[p+0]) % canvas.width; particles[p+1] = (canvas.height + particles[p+1]) % canvas.height; } webgl.drawBuffer(particles, nParticles); if (frame > 30) ctx.drawImage(webgl.elem, 0, 0); }; requestAnimationFrame(run); ///////////////////////////////////////////////////////////////// ["click", "touchdown"].forEach(event => { document.addEventListener(event, e => perlin.reset(), false); }); }
粒子
时间
文字
hover
canvas
3d
游戏
音乐
火焰
水波
轮播图
鼠标跟随
动画
css
加载动画
导航
菜单
按钮
滑块
tab
弹出层
统计图
svg
×
Close
在线代码下载提示
开通在线代码永久免费下载,需支付20jQ币
开通后,在线代码模块中所有代码可终身免费下!
您已开通在线代码永久免费下载,关闭提示框后,点下载代码可直接下载!
您已经开通过在线代码永久免费下载
对不起,您的jQ币不足!可通过发布资源 或
直接充值获取jQ币
取消
开通下载
<!doctype html> <html> <head> <meta charset="utf-8"> <title>canvas粒子-jq22.com</title> <script src="https://www.jq22.com/jquery/jquery-1.10.2.js"></script> <style>
</style> </head> <body>
<script>
</script>
</body> </html>
2012-2021 jQuery插件库版权所有
jquery插件
|
jq22工具库
|
网页技术
|
广告合作
|
在线反馈
|
版权声明
沪ICP备13043785号-1
浙公网安备 33041102000314号