js实现canvas多种动画方式冒泡粒子动画效果代码
代码语言:html
所属分类:粒子
代码描述:js实现canvas多种动画方式冒泡粒子动画效果代码
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<!DOCTYPE html> <html lang="en" > <head> <meta charset="UTF-8"> <style> * { margin: 0; padding: 0; } html, body { width: 100%; height: 100%; } body { font-family: sans-serif; position: relative; background: #000; } h1 { font-size: 3.2rem; color: #666; position: absolute; top: 10%; left: 50%; opacity: 0.3; transform: translate(-50%, -50%); } .outer { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); } .inner { position: relative; } .inner::before, .inner::after { pointer-events: none; z-index: 1; position: absolute; content: ''; display: block; height: 2px; width: 8px; top: 50%; background: #fff; } .inner::before { right: 15.26px; transform: translateY(-50%) rotate(-45deg); } .inner::after { right: 20px; transform: translateY(-50%) rotate(45deg); } input[type="range"] { width: 100%; margin-top: 16px; } select { font-size: 16px; color: #fff; background: rgba(0,0,0,0.5); box-shadow: 0 8px 8px 0 rgba(200,200,200,0.2); -webkit-backdrop-filter: blur(5px); backdrop-filter: blur(5px); border-radius: 8px; border: 1px solid rgba(255,255,255,0.18); -webkit-appearance: none; -moz-appearance: none; appearance: none; padding: 16px 32px; } @media (min-width: 500px) { h1 { font-size: 10rem; } } </style> </head> <body> <h1>Transition</h1> <div class="outer"> <div class="inner"> <select> <option value="Random">Random</option> <option value="EaseIn">EaseIn</option> <option value="EaseInOut">EaseInOut</option> <option value="Elastic">Elastic</option> <option value="Bounce">Bounce</option> <option value="Linear">Linear</option> </select> </div> <input id="range" type="range" min="0" max="10" value="3"/> </div> <script > /** * Utilities static function */ class Utils { /** * Get random number * @param {number} min - min number * @param {number} max - max number */ static getRandomNumber(min, max) { return Math.floor(Math.random() * (max - min + 1) + min); } /** * Get rgb color if not params return random * @param {number} r - red * @param {number} g - green * @param {number} b - blue */ static getRGBColor(r, g, b) { const rr = r || this.getRandomNumber(0, 255); const rg = g || this.getRandomNumber(0, 255); const rb = b || this.getRandomNumber(0, 255); return 'rgb(' + rr + ', ' + rg + ', ' + rb + ')'; } /** * Get hsl color if not params return random * @param {number} hue - from 0 to 360 * @param {number} saturation - from 0 to 100 * @param {number} lightness - from 0 to 100 */ static getHSLColor(hue, saturation, lightness) { const rHue = hue || this.getRandomNumber(0, 360); const rSaturation = saturation || this.getRandomNumber(0, 100); const rLightness = lightness || this.getRandomNumber(0, 100); return 'hsl(' + rHue + ', ' + rSaturation + '%, ' + rLightness + '%)'; } /** * Get gradient color * @param {ctx} ctx - context for canvas * @param {string} type - linear or radial * @param {number} r - red from 0 to 255 * @param {number} g - green from 0 to 255 * @param {number} b - blue from 0 to 255 * @param {number} a - alpha from 0 to 1 * @param {number} x - coordinate x * @param {number} y - coordinate y * @param {number} r - radius * @param {number} x - end coordinate x * @param {number} y - end coordinate y */ static getGradientColor(ctx, type, cr, cg, cb, ca, x, y, r, ex, ey) { let col, g; switch (type) { case 'linear': col = cr + "," + cg + "," + cb; g = ctx.createLinearGradient(x, y, ex, ey); 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; break; case 'radial': col = cr + "," + cg + "," + cb; 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; break; default: console.log('mistaken params'); break;} } /** * Get multiple array * @param {number} yLength - length * @param {number} xLength - length */ static getMultipleArray(yLength, xLength) { const multArr = new Array(yLength); for (let y = 0; y < yLength; y++) { multArr[y] = new Array(xLength); for (let x = 0; x < xLength; x++) { multArr[y][x] = 0; } } return multArr; }} /** * Vector * Referenced / O'Reilly Programming HTML5 Canvas */ class Vector2d { /* * @constructor * @param {number} x - vector x * @param {number} y - vector y */ constructor(x, y) { this.x = x; this.y = y; } /* * Add vector or return new vector * @param {constructor} v - vector or null * @param {number} x - vector x * @param {number} y - vector y */ add(v, x, y) { if (v instanceof Vector2d) { return new Vector2d(this.x + v.x, this.y + v.y); } else { this.x += x; this.y += y; } } /* * Subtract vector or return new vector * @param {constructor} v - vector or null * @param {number} x - vector x * @param {number} y - vector y */ sub(v, x, y) { if (v instanceof Vector2d) { return new Vector2d(this.x - v.x, this.y - v.y); } else { this.x -= x; this.y -= y; } } /* * Multiple vector or return new vector * @param {constructor} v - vector or null * @param {number} num */ mult(v, num) { if (v instanceof Vector2d) { return new Vector2d(this.x * v.x, this.y * v.x); } else { this.x *= num; this.y *= num; } } /* * Change vector from radian * @param {number} radian */ f.........完整代码请登录后点击上方下载按钮下载查看
网友评论0