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 */ fromAngle(radian) { this.x = Math.cos(radian); this.y = Math.sin(radian); } /* * Invert vector */ negate() { this.x = -this.x; this.y = -this.y; } /* * Change vector from radian * @param {number} radian */ rotate(radian) { const x = this.x; const y = this.y; const cosVal = Math.cos(radian); const sinVal = Math.sin(radian); this.x = x * cosVal - y * sinVal; this.y = x * sinVal + y * cosVal; } /* * Return vector length */ length() { return Math.sqrt(this.x * this.x + this.y * this.y); } /* * Return vector length squared */ lengthSquared() { return this.x * this.x + this.y * this.y; } /* * Return making vector same direction and noamlize */ normalize() { const len = Math.sqrt(this.x * this.x + this.y * this.y); if (len) { this.x /= len; this.y /= len; } return len; } /* * Return dot product of vectors * @param {constructor} v - vector */ dotProduct(v) { return this.x * v.x + this.y * v.y; } edge(v) { return this.sub(v); } /* * Return vertical vector */ perpendicular() { const v = new Vector2d(); v.x = this.y; v.y = 0 - this.x; return v; } /* * Return normalized vertical vector */ normal() { const p = this.perpendicular(); return p.normalize(); } /* * Display vector by string */ toString() { return '(' + this.x.toFixed(3) + ',' + this.y.toFixed(3) + ')'; }} /** * Collide * Referenced / O'Reilly Programming HTML5 Canvas */ class CollideWithCircle { /* * @constructor * @param {array} array - target array */ constructor(array) { this.array = array; } /* * Check for collide */ collide() { const v = new Vector2d(0, 0); let dist, obj1, obj2; for (let c = 0; c < this.array.length; c++) { obj1 = this.array[c]; for (let i = c + 1; i < this.array.length; i++) { obj2 = this.array[i]; v.x = obj2.x - obj1.x; v.y = obj2.y - obj1.y; dist = v.length(); if (dist < obj1.radius + obj2.radius) { v.normalize(); v.mult(null, obj1.radius + obj2.radius - dist); v.negate(); obj1.x += v.x; obj1.y += v.y; this.bounce(obj1, obj2); } } } } /* * Change vector * @param {obj} obj1 - target object * @param {obj} obj2 - target object */ bounce(obj1, obj2) { const colnAngle = Math.atan2(obj1.y - obj2.y, obj1.x - obj2.x); const length1 = obj1.v.length(); const length2 = obj2.v.length(); const dirAngle1 = Math.atan2(obj1.v.y, obj1.v.x); const dirAngle2 = Math.atan2(obj2.v.y, obj2.v.x); const newVX1 = length1 * Math.cos(dirAngle1 - colnAngle); const newVX2 = length2 * Math.cos(dirAngle2 - colnAngle); obj1.v.y = length1 * Math.sin(dirAngle1 - colnAngle); obj2.v.y = length2 * Math.sin(dirAngle2 - colnAngle); obj1.v.x = ((obj1.radius - obj2.radius) * newVX1 + 2 * obj2.radius * newVX2) / (obj1.radius + obj2.radius); obj2.v.x = ((obj2.radius - obj1.radius) * newVX2 + 2 * obj1.radius * newVX1) / (obj1.radius + obj2.radius); obj1.v.rotate(colnAngle); obj2.v.rotate(colnAngle); }} /** * Stop watch * Referenced / O'R.........完整代码请登录后点击上方下载按钮下载查看
网友评论0