js实现小蛇爬行canvas加载动画效果代码
代码语言:html
所属分类:动画
代码描述:js实现小蛇爬行canvas加载动画效果代码
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<!DOCTYPE html> <html lang="en" > <head> <meta charset="UTF-8"> <style> svg { width: 85vw; opacity: 0; position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); } body { height: 100vh; overflow: hidden; } canvas { position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); } p { position: fixed; bottom: 10px; left: 0; width: 100%; text-align: center; font-family: "Antonio", sans-serif; letter-spacing: 0.01em; } </style> </head> <body > <canvas></canvas> <svg viewBox="0 0 543.7 232.6"> <path d="M51.424,116.98317c0-170.16522,222.20155,0,222.20155,0s218.70567,170.16521,218.70567,0-218.70567-4.933-218.70567-4.933S51.424,287.14838,51.424,116.98317Z" fill="none" stroke="#3eab37" stroke-width="7"/> </svg> <p>Move your mouse or finger for more fun</p> <script > const canvas = document.querySelector('canvas'); const svg = document.querySelector('svg'); const ctx = canvas.getContext('2d'); let width = svg.clientWidth; let height = svg.clientHeight; const path = svg.querySelector('path'); const totalLength = path.getTotalLength(); let mouseX = 3; let coordinates = []; canvas.width = width; canvas.height = height; const gradients = [ [ [0, [190, 217, 2]], [33, [107, 140, 1]], [66, [80, 123, 5]], [100, [57, 84, 0]]]]; const dots = []; class Dot { constructor(x, y, color, delay, index) { this._x = x; this._y = y; this.x = x * width; this.y = y * height; this.r = width * 0.005 + Math.sin(delay) * width * 0.08; this.color = color; this.delay = delay; this.index = index; } resize() { this.x = this._x * width; this.y = this._y * height; } update(time) { const p = coordinates[Math.floor(time + this.delay * totalLength) % coordinates.length]; this._x = p.x / 543.7; this._y = p.y / 232.6; this.x = this._x * width; this.y = this._y * height; this.r = width * 0.005 + Math.sin(this.delay) * width * 0.08; } draw() { if (this.index % mouseX === 0) { ctx.fillStyle = this.color; ctx.beginPath(); ctx.arc(this.x, this.y, this.r, 0, 2 * Math.PI); ctx.fill(); } }} /* ============ */ /* === INIT === */ /* ============ */ function init() { let sum_length = 0; for (let i = 0; i < totalLength * 0.9; i += 2) { const point = path.getPointAtLength(i); const x = point.x / 543.7; const y = point.y / 232.6; const RGB_color = getColor(0, totalLength, i / totalLength); const color = `rgb(${RGB_color[0]}, ${RGB_color[1]}, ${RGB_color[2]})`; const dot = new Dot(x, y, color, 1 - i / totalLength, i / 2); dots.push(dot); sum_length += 2; } for (let i = 0; i < totalLength; i += 1) { coordinates.push(path.getPointAtLength(i)); } } /* Code copied from https://stackoverflow.com/a/30144587 */ function pickHex(color1, color2, weight) { var p = weight; var w = p * 2 - 1; var w1 = (w / 1 + 1) / 2; var w2 = 1 - w1; var rgb = [Math.round(color1[0] *.........完整代码请登录后点击上方下载按钮下载查看
网友评论0