粒子流流动动画效果
代码语言:html
所属分类:动画
代码描述:粒子流流动动画效果
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<!DOCTYPE html> <html lang="en" > <head> <meta charset="UTF-8"> <style> @import url("https://fonts.googleapis.com/css2?family=Rajdhani:wght@500&display=swap"); * { margin: 0; padding: 0; } html, body { font-family: "Rajdhani", sans-serif; overflow: hidden; } body { position: relative; background: #000; } main { z-index: 2; color: #fff; position: fixed; top: 50%; left: 1.6rem; transform: translateY(-50%); } h1 { font-size: 2.4rem; } h1 > span { font-size: 1.6rem; } p.checkbox { color: #fff; font-size: 1.6rem; } input[type="checkbox"] { height: 1rem; width: 1rem; } p { padding: 0.8rem 0 0 0; } </style> </head> <body translate="no" > <main> <h1>Digital Stream</h1> <p>Please moving the mouse or scroll.</p> <p class="checkbox"><input id="checkbox" type="checkbox"><label for="checkbox"> Crack?</label></p> </main> <script src='https://cdnjs.cloudflare.com/ajax/libs/simplex-noise/2.4.0/simplex-noise.min.js'></script> <script> /* * File Name / digitalStream.js * Created Date / Sep 07, 2020 * Aurhor / Toshiya Marukubo * Twitter / https://twitter.com/toshiyamarukubo */ /* Common Tool. */ class Tool { // random number. static randomNumber(min, max) { return Math.floor(Math.random() * (max - min + 1) + min); } // random color rgb. static randomColorRGB() { return ( "rgb(" + this.randomNumber(0, 255) + ", " + this.randomNumber(0, 255) + ", " + this.randomNumber(0, 255) + ")"); } // random color hsl. static randomColorHSL(hue, saturation, lightness) { return ( "hsl(" + hue + ", " + saturation + "%, " + lightness + "%)"); } // gradient color. static gradientColor(ctx, cr, cg, cb, ca, x, y, r) { const col = cr + "," + cg + "," + cb; const g = ctx.createRadialGradient(x, y, 0, x, y, r); g.addColorStop(0, "rgba(" + col + ", " + ca * 1 + ")"); g.addColorStop(0.5, "rgba(" + col + ", " + ca * 0.5 + ")"); g.addColorStop(1, "rgba(" + col + ", " + ca * 0 + ")"); return g; }} /* When want to use angle. */ class Angle { constructor(angle) { this.a = angle; this.rad = this.a * Math.PI / 180; } incDec(num) { this.a += num; this.rad = this.a * Math.PI / 180; return this.rad; }} /* When want to use controller. */ class Controller { constructor(id) { this.id = document.getElementById(id); } getVal() { return this.id.value; }} let canvas; const simplex = new SimplexNoise(); class Canvas { constructor(bool) { // create canvas. this.canvas = document.createElement("canvas"); // if on screen. if (bool === true) { this.canvas.style.position = 'fixed'; this.canvas.style.display = 'block'; this.canvas.style.top = 0; this.canvas.style.left = 0; document.getElementsByTagName("body")[0].appendChild(this.canvas); } this.ctx = this.canvas.getContext("2d"); this.width = this.canvas.width = window.innerWidth; this.height = this.canvas.height = window.innerHeight; // mouse infomation. this.mouseX = null; this.mouseY = null; // controller this.checkbox = document.getElementById('checkbox'); // Shape this.shapes = []; this.width > this.height ? this.size = 20 : this.size = 20; this.xNum = this.width / this.size + 1; this.yNum = this.height / this.size + 1; this.noiseDist = 100; this.glitch; this.flg = this.checkbox.checked; } // init, render, resize init() { for (let x = 0; x < this.xNum; x++) { if (this.shapes.length > 2000) break; for (let y = 0; y < this.yNum; y++) { let s = new Shape(this.ctx, x * this.size, y * this.size, this.size); this.shapes.push(s); } } this.glitch = new Glitch(this.ctx); } render() { //this.ctx.clearRect(0, 0, this.width, this.height); this.ctx.globalCompositeOperation = "darken"; this.ctx.globalAlpha = 0.03; this.ctx.fillStyle = "rgb(0,0,0)"; this.ctx.fillRect(0, 0, this.width, this.height); this.ctx.globalCompositeOperation = "source-over"; this.ctx.globalAlpha = 1; for (let i = 0; i < this.shapes.length; i++) { this.shapes[i].render(); } if (this.flg === true && Math.random() < 0.3) this.glitch.render(); } resize() { this.width = this.canvas.width = window.innerWidth; this.height = this.canvas.height = window.innerHeight; this.shapes = []; this.width > this.height ? this.size = 20 : this.size = 20; this.xNum = this.width / this.size + 1; this.yNum = this.height / this.size + 1; this.init(); }} /* Shape class. */ class Shape { constructor(ctx, x, y, r) { this.ctx = ctx; this.init(x, y, r); } init(x, y, r) { this.x = x; this.y = y; this.r = r; this.t = 1000; this.a = new Angle(0); if (canvas.width > canvas.height) { this.v = { x: 1, y: 0 }; } else { this.v = { x: 0, y: -1 }; } this.l = Tool.randomNumber(10, 100); } draw() { const ctx = this.ctx; const color = simplex.noise3D(this.x / 1000, this.y / 1000, this.t) * Math.sin(this.a.rad * 1.........完整代码请登录后点击上方下载按钮下载查看
网友评论0