Toggle navigation
在线编辑器
在线代码
文本比较
jQuery下载
前端库
在线手册
登录/注册
下载代码
html
css
js
分享到微信朋友圈
X
html
别害羞, 把鼠标挪过来!
css
body, html{ margin: 0; padding: 0; border: 0; font-family: 'Nunito', sans-serif; } canvas{ margin: 0; padding: 0; display: block; /* ˉ\_(ツ)_/ˉ */ touch-action: none; } h1{ position: absolute; top: 50%; left: 0; margin: auto; width: 100%; text-align: center; }
JavaScript
var canvas = document.querySelector('canvas'); canvas.height = window.innerHeight; canvas.width = window.innerWidth; c = canvas.getContext('2d'); window.addEventListener('resize', function(){ canvas.height = window.innerHeight; canvas.width = window.innerWidth; initCanvas(); }) var mouse = { x: undefined, y: undefined } window.addEventListener('mousemove', function (event) { mouse.x = event.x; mouse.y = event.y; drawCircles(); } ) window.addEventListener("touchmove", function (event) { let touch = event.touches[0]; mouse.x = touch.clientX; mouse.y = touch.clientY; drawCircles(); } ) function Circle(x, y, radius, vx, vy, rgb, opacity, birth, life){ this.x = x; this.y = y; this.radius = radius; this.minRadius = radius; this.vx = vx; this.vy = vy; this.birth = birth; this.life = life; this.opacity = opacity; this.draw = function() { c.beginPath(); c.arc(this.x, this.y, this.radius, Math.PI * 2, false); c.fillStyle = 'rgba(' + rgb +','+ this.opacity +')'; c.fill(); } this.update = function(){ if (this.x + this.radius > innerWidth || this.x - this.radius < 0) { this.vx = -this.vx; } if (this.y + this.radius > innerHeight || this.y - this.radius < 0) { this.vy = -this.vy; } this.x += this.vx; this.y += this.vy; this.opacity = 1- (((frame - this.birth) * 1) / this.life); if (frame > this.birth + this.life){ for (let i = 0; i < circleArray.length; i++){ if (this.birth == circleArray[i].birth && this.life == circleArray[i].life){ circleArray.splice(i, 1); break; } } } else{ this.draw(); } } } var circleArray = []; function initCanvas() { circleArray = []; } var colorArray = [ '355,85,80', '9,80,100', '343,81,45' ] function drawCircles(){ for (let i = 0; i < 6; i++) { let radius = Math.floor(Math.random() * 4) + 2; let vx = (Math.random() * 2) - 1; let vy = (Math.random() * 2) - 1; let spawnFrame = frame; let rgb = colorArray[Math.floor(Math.random() * colorArray.length)]; let life = 100; circleArray.push(new Circle(mouse.x, mouse.y, radius, vx, vy, rgb, 1, spawnFrame, life)); } } var frame = 0; function animate() { requestAnimationFrame(animate); frame += 1; c.clearRect(0, 0, innerWidth, innerHeight); for (let i = 0; i < circleArray.length; i++ ){ circleArray[i].update(); } } initCanvas(); animate(); // This is just for demo purposes : for (let i = 1; i < 110; i++) { (function (index) { setTimeout(function () { mouse.x = 100 + i * 10; mouse.y = 100; drawCircles(); }, i * 10); })(i); }
粒子
时间
文字
hover
canvas
3d
游戏
音乐
火焰
水波
轮播图
鼠标跟随
动画
css
加载动画
导航
菜单
按钮
滑块
tab
弹出层
统计图
svg
×
Close
在线代码下载提示
开通在线代码永久免费下载,需支付20jQ币
开通后,在线代码模块中所有代码可终身免费下!
您已开通在线代码永久免费下载,关闭提示框后,点下载代码可直接下载!
您已经开通过在线代码永久免费下载
对不起,您的jQ币不足!可通过发布资源 或
直接充值获取jQ币
取消
开通下载
<!doctype html> <html> <head> <meta charset="utf-8"> <title>canvas鼠标和触摸事件-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号