今天无意中访问到了开源社区 (apiopen.top)的主界面,发现鼠标跟随的特效不错(残留轨迹),弄下来玩玩

上代码

整合后只需要两部分,导入JS依赖后,在html 添加 id 为 mouseCanvasCanvas标签就行

1、HTML

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>测试</title>
<!-- 引入依赖js -->
<script src="./mouseCanvas.js"></script>
</head>
<body>
<!-- 只需要一个Id为 mouseCanvas 的canvas标签 -->
<canvas id="mouseCanvas"></canvas>
</body>
</html>

2、js

var SCREEN_WIDTH=window.innerWidth;var SCREEN_HEIGHT=window.innerHeight;var RADIUS=70;var QUANTITY=25;var RADIUS_SCALE=1;var RADIUS_SCALE_MIN=1;var RADIUS_SCALE_MAX=1.5;var canvas;var context;var particles;var mouseX=SCREEN_WIDTH*0.5;var mouseY=SCREEN_HEIGHT*0.5;var mouseIsDown=false;function init(){canvas=document.getElementById('mouseCanvas');if(canvas&&canvas.getContext){context=canvas.getContext('2d');window.addEventListener('mousemove',documentMouseMoveHandler,false);window.addEventListener('mousedown',documentMouseDownHandler,false);window.addEventListener('mouseup',documentMouseUpHandler,false);document.addEventListener('touchstart',documentTouchStartHandler,false);document.addEventListener('touchmove',documentTouchMoveHandler,false);window.addEventListener('resize',windowResizeHandler,false);createParticles();windowResizeHandler();setInterval(loop,1000/60)}}function createParticles(){particles=[];for(var i=0;i<QUANTITY;i++){var particle={size:1,position:{x:mouseX,y:mouseY},offset:{x:0,y:0},shift:{x:mouseX,y:mouseY},speed:0.01+Math.random()*0.04,targetSize:1,fillColor:'#'+(Math.random()*0x904040+0xaaaaaa|0).toString(16),orbit:RADIUS*.5+(RADIUS*.5*Math.random())};particles.push(particle)}}function documentMouseMoveHandler(event){mouseX=event.clientX-(window.innerWidth-SCREEN_WIDTH)*.5;mouseY=event.clientY-(window.innerHeight-SCREEN_HEIGHT)*.5}function documentMouseDownHandler(event){mouseIsDown=true}function documentMouseUpHandler(event){mouseIsDown=false}function documentTouchStartHandler(event){if(event.touches.length==1){event.preventDefault();mouseX=event.touches[0].pageX-(window.innerWidth-SCREEN_WIDTH)*.5;mouseY=event.touches[0].pageY-(window.innerHeight-SCREEN_HEIGHT)*.5}}function documentTouchMoveHandler(event){if(event.touches.length==1){event.preventDefault();mouseX=event.touches[0].pageX-(window.innerWidth-SCREEN_WIDTH)*.5;mouseY=event.touches[0].pageY-(window.innerHeight-SCREEN_HEIGHT)*.5}}function windowResizeHandler(){SCREEN_WIDTH=window.innerWidth;SCREEN_HEIGHT=window.innerHeight;canvas.width=SCREEN_WIDTH;canvas.height=SCREEN_HEIGHT}function loop(){if(mouseIsDown){RADIUS_SCALE+=(RADIUS_SCALE_MAX-RADIUS_SCALE)*(0.02)}else{RADIUS_SCALE-=(RADIUS_SCALE-RADIUS_SCALE_MIN)*(0.02)}RADIUS_SCALE=Math.min(RADIUS_SCALE,RADIUS_SCALE_MAX);context.fillStyle='rgba(0,0,0,0.05)';context.fillRect(0,0,context.canvas.width,context.canvas.height);for(i=0,len=particles.length;i<len;i++){var particle=particles[i];var lp={x:particle.position.x,y:particle.position.y};particle.offset.x+=particle.speed;particle.offset.y+=particle.speed;particle.shift.x+=(mouseX-particle.shift.x)*(particle.speed);particle.shift.y+=(mouseY-particle.shift.y)*(particle.speed);particle.position.x=particle.shift.x+Math.cos(i+particle.offset.x)*(particle.orbit*RADIUS_SCALE);particle.position.y=particle.shift.y+Math.sin(i+particle.offset.y)*(particle.orbit*RADIUS_SCALE);particle.position.x=Math.max(Math.min(particle.position.x,SCREEN_WIDTH),0);particle.position.y=Math.max(Math.min(particle.position.y,SCREEN_HEIGHT),0);particle.size+=(particle.targetSize-particle.size)*0.01;if(Math.round(particle.size)==Math.round(particle.targetSize)){particle.targetSize=1+Math.random()*2}context.beginPath();context.fillStyle=particle.fillColor;context.strokeStyle=particle.fillColor;context.lineWidth=particle.size;context.moveTo(lp.x,lp.y);context.lineTo(particle.position.x,particle.position.y);context.stroke();context.arc(particle.position.x,particle.position.y,particle.size/2,0,Math.PI*2,true);context.fill()}}window.onload=function(){document.getElementById('mouseCanvas').style="width:100%;height:100%;position: fixed;top: 0;left: 0;z-index: -999;";document.getElementsByTagName('html')[0].style='margin:0;width:100%;height:100%;';document.getElementsByTagName('body')[0].style='margin:0;width:100%;height:100%;';init()};

效果:

最新文章

  1. Ubuntu 14.04下搜狗输入法崩溃重启
  2. PHP控制div块大小和颜色的例子
  3. Java递归算法——变位字
  4. JavaScript instanceof和typeof的区别
  5. 柯南君:看大数据时代下的IT架构(7)消息队列之RabbitMQ--案例(routing 起航)
  6. 第一个processing程序(2016-01-15)
  7. 基于visual Studio2013解决面试题之1105字符串压缩
  8. Elasticsearch过滤器——filter
  9. etcd集群部署
  10. Python自动化测试、性能测试成长路线图
  11. Android Studio中创建Kotlin For Android项目
  12. 华为5G在印度被禁
  13. Python复习笔记(四)高阶函数/返回函数/匿名函数/偏函数/装饰器
  14. LeetCode题解之 Continuous Subarray Sum
  15. effective c++ 笔记 (26-29)
  16. iOS配置SSO授权
  17. HDU2553 N皇后问题 2016-07-24 13:56 283人阅读 评论(0) 收藏
  18. hdu 5032 不易发觉的树状数组
  19. POJ 3613 Cow Relays(floyd+快速幂)
  20. Disruptor Java版和.NET版的区别

热门文章

  1. mujoco d4rl 安装问题
  2. 使用MVC的实现登录注册功能
  3. VSCode设置鼠标滚轮滑动设置字体大小
  4. 基于 Redis 生成分布式订单号
  5. 【UML】统一建模语言
  6. vue2 解决跨域
  7. 一、什么是Kubernetes
  8. 孙荣辛|大数据穿针引线进阶必看——Google经典大数据知识
  9. 关于.Net和Java的看法-一个小实习生经历
  10. springboot前端向后端请求返回html语句