鼠标跟随填充网格背景效果
代码语言:html
所属分类:背景
代码描述:鼠标跟随填充网格背景效果
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <style> body { overflow: hidden; } canvas { width: 100vw; height: 100vh; cursor: pointer; } </style> </head> <body translate="no"> <canvas id="c"></canvas> <script type="text/javascript" src="http://repo.bfw.wiki/bfwrepo/js/victor.min.js"></script> <script type="text/javascript" src="http://repo.bfw.wiki/bfwrepo/js/chroma.min.js"></script> <script type="text/javascript" src="http://repo.bfw.wiki/bfwrepo/js/simplex-noise.min.js"></script> <script type="text/javascript" src="http://repo.bfw.wiki/bfwrepo/js/tweakpane.min.js"></script> <script > console.clear(); class Utils { static randomRange(min, max) { return Math.random() * (max - min) + min; } static mapRange(value, inputMin, inputMax, outputMin, outputMax, clamp) { if (Math.abs(inputMin - inputMax) < Number.EPSILON) { return outputMin; } else { var outVal = (value - inputMin) / (inputMax - inputMin) * (outputMax - outputMin) + outputMin; if (clamp) { if (outputMax < outputMin) { if (outVal < outputMax) outVal = outputMax;else if (outVal > outputMin) outVal = outputMin; } else { if (outVal > outputMax) outVal = outputMax;else if (outVal < outputMin) outVal = outputMin; } } return outVal; } }} Utils.simplex = new SimplexNoise('seed'); class App { constructor() { this.config = { bgColor: chroma({ h: 230, s: 0.5, l: 0.92 }).hex(), baseSize: 20, attractionRadius: 200, explosionParticlesNum: 16 }; this.canvas = document.getElementById('c'); this.ctx = this.canvas.getContext('2d'); this.shadowCanvas = document.createElement('canvas'); this.shadowCtx = this.shadowCanvas.getContext('2d'); this.timestamp = 0; this.fpsHistory = []; this.setUpVars(); this.mouse = { x: this.wCenterX, y: this.wCenterY }; this.setUpListeners(); this.setUpGui(); this.update(); this.handleClick(); } setUpGui() { const pane = new Tweakpane(); const folder = pane.addFolder({ expanded: false, title: 'Settings' }); folder.addInput(this.config, 'baseSize', { min: 10, max: Math.floor(this.wMin / 4), step: 1 }). on('change', () => { this.setUpVars(); }); } setUpVars() { this.canvas.width = this.shadowCanvas.width = this.wWidth = window.innerWidth; this.canvas.height = this.shadowCanvas.height = this.wHeight = window.innerHeight; this.wCenterX = this.wWidth / 2; this.wCenterY = this.wHeight / 2; this.wHypot = Math.hypot(this.wWidth, this.wHeight); this.wMin = Math.min(this.wWidth, this.wHeight); this.config.attractionRadius = this.wMin * 0.2; const xMaxCount = Math.round(this.wWidth / this.config.baseSize); const yMaxCount = Math.round(this.wHeight / this.config.baseSize); this.cellWidth = this.wWidth / xMaxCount; this.cellHeight = this.wHeight / yMaxCount; this.cellMap = []; this.grid = this.genGrid(xMaxCount, yMaxCount); } cellTaken(x, y) { const isInMap = this.cellMap.filter(cell => cell.x === x && cell.y === y).length; // allow to overshoot by 10% const outXBounds = x * this.cellWidth + this.cellWidth > this.wWidth + this.cellWidth * 0.1; const outYBounds = y * this.cellHeight + this.cellHeight > this.wHeight + this.cellHeight * 0.1; return isInMap || outXBounds || outYBounds; } genGrid(xCount, yCount) { const grid = []; for (let x = 0; x < xCount; x++) { for (let y = 0; y < yCount; y++) { if (!this.cellTaken(x, y)) { const span = 1 + Math.floor(Math.random() * 6); grid.push( this.genCell(x, y, span, span)); } } } return grid; } genCell(x, y, xSpan, ySpan) { const cellsMap = []; let spaceIsAvailable = true; let cell; for (let xid = 0; xid < xSpan; xid++) { for (let yid = 0; yid < ySpan; yid++) { if (this.cellTaken(x + xid, y + yid)) { spaceIsAvailable = false; } else if (spaceIsAvailable) { cellsMap.push({ x: x + xid, y: y + yid }); } } } if (spaceIsAvailable) { cell = { x: Math.ceil(x * this.cellWidth), y: Math.ceil(y * this.cellHeight), width: Math.ceil(xSpan * this.cellWidth), height: Math.ceil(ySpan * this.cellHeight), color: 'hsl(' + (100 + (x + y) * 1.1) + ', 20%, 60%)', angle: Math.PI * 2 * Math.random(), prevAngle: 0, xSpan, ySpan }; this.cellMap.push(...cellsMap); } else { // if space is unavailable defa.........完整代码请登录后点击上方下载按钮下载查看
网友评论0