Toggle navigation
在线编辑器
在线代码
文本比较
jQuery下载
前端库
在线手册
登录/注册
下载代码
html
css
js
分享到微信朋友圈
X
html
css
html, body { overflow: hidden; background: black; }
JavaScript
var PI = Math.PI,cos = Math.cos,acos = Math.acos,sin = Math.sin,asin = Math.asin,abs = Math.abs,sqrt = Math.sqrt,pow = Math.pow,floor = Math.floor,round = Math.round,random = Math.random,atan2 = Math.atan2; var HALF_PI = 0.5 * PI; var TAU = 2 * PI; var QT3_TAU = TAU - HALF_PI; var TO_RAD = PI / 180; var rand = function rand(n) {return n * random();}; var randRange = function randRange(n) {return n - rand(2 * n);}; var fadeIn = function fadeIn(t, m) {return t / m;}; var fadeOut = function fadeOut(t, m) {return (m - t) / m;}; var fadeInOut = function fadeInOut(t, m) { var hm = 0.5 * m; return abs((t + hm) % m - hm) / hm; }; var dist = function dist(x1, y1, x2, y2) {return sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));}; var angle = function angle(x1, y1, x2, y2) {return atan2(y2 - y1, x2 - x1);}; var lerp = function lerp(n1, n2, speed) {return (1 - speed) * n1 + speed * n2;}; var deflectorCount = 50; var particleCount = 500; var canvas = void 0; var ctx = void 0; var origin = void 0; var mouse = void 0; var hover = void 0; var deflectors = void 0; var particles = void 0; function setup() { canvas = document.createElement('canvas'); ctx = canvas.getContext('2d'); canvas.style = '\n\t\tposition: absolute;\n\t\ttop: 0;\n\t\tleft: 0;\n\t\twidth: 100%;\n\t\theight: 100%;\n\t'; document.body.appendChild(canvas); origin = { x: 0, y: 0 }; mouse = { x: 0, y: 0 }; hover = false; init(); draw(); } function init() { resize(); hover = false; deflectors = []; for (var i = 0; i < deflectorCount; i++) { deflectors.push(getDeflector()); } particles = []; for (var _i = 0; _i < particleCount; _i++) { particles.push(getParticle(_i).create()); } } function resize() { canvas.width = window.innerWidth; canvas.height = window.innerHeight; origin.x = mouse.x = 0.5 * canvas.width; origin.y = mouse.y = 0.5 * canvas.height; } function mouseHandler(e) { hover = e.type === "mousemove"; mouse.x = e.clientX; mouse.y = e.clientY; } function getDeflector() { return { position: { x: rand(window.innerWidth), y: rand(window.innerHeight) }, velocity: { x: randRange(1), y: randRange(1) }, threshold: rand(200) + 100, direction: rand(TAU), move: function move() { if (this.position.x > canvas.width || this.position.x < 0) { this.velocity.x *= -1; } if (this.position.y > canvas.height || this.position.y < 0) { this.velocity.y *= -1; } this.position.x += this.velocity.x; this.position.y += this.velocity.y; } }; } function getParticle(i) { return { create: function create() { this.position.x = this.lastPosition.x = origin.x + randRange(1); this.position.y = this.lastPosition.x = origin.y + randRange(1); this.speed = rand(5) + 1; this.size = rand(3) + 0.5; this.life = 0; this.ttl = rand(100); this.hue = randRange(30); this.direction = angle(0.5 * canvas.width, 0.5 * canvas.height, this.position.x, this.position.y); return this; }, position: { x: 0, y: 0 }, lastPosition: { x: 0, y: 0 }, velocity: { x: 0, y: 0 }, update: function update() { this.life++; this.lastPosition.x = this.position.x; this.lastPosition.y = this.position.y; this.velocity.x = lerp( this.velocity.x, cos(this.direction) * fadeInOut(this.life, this.ttl) * this.speed, 0.15); this.velocity.y = lerp( this.velocity.y, sin(this.direction) * fadeInOut(this.life, this.ttl) * this.speed, 0.15); this.position.x += this.velocity.x; this.position.y += this.velocity.y; this.life > this.ttl && this.create(); }, draw: function draw() { this.update(); ctx.beginPath(); ctx.lineWidth = this.size; ctx.strokeStyle = 'hsla(' + this.hue + ',60%,50%,' + fadeInOut(this.life, this.ttl) * 0.5 + ')'; ctx.moveTo(this.lastPosition.x, this.lastPosition.y); ctx.lineTo(this.position.x, this.position.y); ctx.stroke(); ctx.closePath(); } }; } var deflector = void 0,particle = void 0; function draw() { var i = void 0,j = void 0; origin.x = lerp( origin.x, hover ? mouse.x : 0.5 * canvas.width, 0.05); origin.y = lerp( origin.y, hover ? mouse.y : 0.5 * canvas.height, 0.05); ctx.fillStyle = "rgba(0,0,0,0.05)"; ctx.fillRect(0, 0, canvas.width, canvas.height); for (i = particles.length - 1; i >= 0; i--) { particle = particles[i]; for (j = deflectors.length - 1; j >= 0; j--) { deflector = deflectors[j]; i === 0 && deflector.move(); particle.direction = dist( particle.position.x, particle.position.y, deflector.position.x, deflector.position.y) < deflector.threshold && lerp( particle.direction, angle( deflector.position.x, deflector.position.y, particle.position.x, particle.position.y) + angle( origin.x, origin.y, particle.position.x, particle.position.y), 0.075) || particle.direction; } particle.draw(); } ctx.save(); ctx.globalCompositeOperation = "lighten"; ctx.filter = "blur(6px)"; ctx.drawImage(canvas, 0, 0); ctx.restore(); ctx.save(); ctx.drawImage(canvas, 0, 0); ctx.restore(); window.requestAnimationFrame(draw); } window.addEventListener("load", setup); window.addEventListener("resize", resize); window.addEventListener("mousemove", mouseHandler); window.addEventListener("mouseout", mouseHandler); window.addEventListener("click", init);
粒子
时间
文字
hover
canvas
3d
游戏
音乐
火焰
水波
轮播图
鼠标跟随
动画
css
加载动画
导航
菜单
按钮
滑块
tab
弹出层
统计图
svg
×
Close
在线代码下载提示
开通在线代码永久免费下载,需支付20jQ币
开通后,在线代码模块中所有代码可终身免费下!
您已开通在线代码永久免费下载,关闭提示框后,点下载代码可直接下载!
您已经开通过在线代码永久免费下载
对不起,您的jQ币不足!可通过发布资源 或
直接充值获取jQ币
取消
开通下载
<!doctype html> <html> <head> <meta charset="utf-8"> <title>粒子鼠标跟随-jq22.com</title> <script src="https://www.jq22.com/jquery/jquery-1.10.2.js"></script> <style>
</style> </head> <body>
<script>
</script>
</body> </html>
2012-2021 jQuery插件库版权所有
jquery插件
|
jq22工具库
|
网页技术
|
广告合作
|
在线反馈
|
版权声明
沪ICP备13043785号-1
浙公网安备 33041102000314号