JS鼠标吸粉特效
HTML
<canvas id=canvas></canvas>
CSS
* {
margin: 0;
padding: 0;
}
html {
overflow: hidden;
}
canvas {
cursor: none;
}
JS
window.onload = function() {
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var pi = Math.PI;
var centerX, centerY;
var part_num = 2000;
var mousedown = false;
var X, Y;
/*===========================================================================*/
/*===========================================================================*/
var P = [];
var part = function(x, y, vx, vy, r, red, green, blue, alpha, col) {
this.x = x;
this.y = y;
this.vx = vx;
this.vy = vy;
this.r = r;
this.red = red;
this.green = green;
this.blue = blue;
this.alpha = alpha;
this.col = col;
};
function rand(min, max) {
return Math.random() * (max - min) + min;
}
function dist(dx, dy) {
return Math.sqrt(dx * dx + dy * dy);
}
function size() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
centerX = canvas.width / 2;
centerY = canvas.height / 2;
}
size();
X = centerX;
Y = centerY;
function init() {
var x, y, vx, vy, r, red, green, blue, alpha, col;
for (var i = 0; i < part_num; i++) {
x = rand(0, canvas.width);
y = rand(0, canvas.height);
vx = rand(-1, 1);
vy = rand(-1, 1);
r = rand(1, 3);
red = Math.round(rand(150, 200));
green = Math.round(rand(100, 255));
blue = Math.round(rand(180, 255));
alpha = 1;
col = "rgba(" + red + "," + green + "," + blue + "," + alpha + ")";
P.push(new part(x, y, vx, vy, r, red, green, blue, alpha, col));
}
}
function bg() {
ctx.fillStyle = "rgba(25,25,30,1)";
ctx.fillRect(0, 0, canvas.width, canvas.height);
//ctx.clearRect(0, 0, canvas.width, canvas.height);
}
function bounce(b) {
if (b.x < b.r) {
b.x = b.r;
b.vx *= -1;
}
if (b.x > canvas.width - b.r) {
b.x = canvas.width - b.r;
b.vx *= -1;
}
if (b.y - b.r < 0) {
b.y = b.r;
b.vy *= -1;
}
if (b.y > canvas.height - b.r) {
b.y = canvas.height - b.r;
b.vy *= -1;
}
}
function attract(p) {
var dx = (p.x - X),
dy = (p.y - Y),
dist = Math.sqrt(dx * dx + dy * dy),
angle = Math.atan2(dy, dx);
if (dist > 10 && dist < 300) {
if (!mousedown) {
p.vx -= (20 / (p.r * dist)) * Math.cos(angle);
p.vy -= (20 / (p.r * dist)) * Math.sin(angle);
} else if (mousedown) {
p.vx += (250 / (p.r * dist)) * Math.cos(angle);
p.vy += (250 / (p.r * dist)) * Math.sin(angle);
}
}
}
function draw() {
var p;
for (var i = 0; i < P.length; i++) {
p = P[i];
if (mouseover) attract(p);
bounce(p);
p.x += p.vx;
p.y += p.vy;
p.vx *= .975;
p.vy *= .975;
ctx.fillStyle = p.col;
ctx.fillRect(p.x, p.y, p.r, p.r);
//ctx.beginPath();
//ctx.fillStyle = p.col;
//ctx.arc(p.x, p.y, p.r, 0, 2 * pi);
//ctx.fill();
}
ctx.strokeStyle = (!mousedown) ? "rgba(255,255,255,1)" : "rgba(255,0,0,1)";
ctx.beginPath();
ctx.moveTo(X, Y - 10);
ctx.lineTo(X, Y + 10);
ctx.moveTo(X - 10, Y);
ctx.lineTo(X + 10, Y);
ctx.stroke();
}
function loop() {
bg();
draw();
window.requestAnimationFrame(loop);
}
window.onresize = size;
window.onmousemove = function(e) {
X = e.clientX;
Y = e.clientY;
}
window.onmousedown = function() {
mousedown = true;
}
window.onmouseup = function() {
mousedown = false;
}
var mouseover = false;
window.onmouseover = function() {
mouseover = true;
}
window.onmouseout = function() {
mouseover = false;
}
init();
loop();
}
JS鼠标吸粉特效的更多相关文章
- js鼠标点击特效,有关参数设置
效果图,用的faststone--录像--togif,黄色圆圈实际是不显示的 博客后台管理设置 本地新建一个demo.html文件,可以自行测试,要引入jquery文件哦 来个“红橙黄绿蓝靛紫”的点击 ...
- js,jquery,css,html5特效
包含js,jquery,css,html5特效,源代码 本文地址:http://www.cnblogs.com/roucheng/p/texiao.html 2017新年快乐特效 jQuery最新最全 ...
- HTML5+CSS3鼠标悬停图片特效
点击预览效果 下载该特效: HTML5+CSS3鼠标悬停图片特效.zip 特效说明: 一款HTML5+CSS3鼠标悬停图片事件网页特效,集合了最流行鼠标悬停图片文字.图片动画移动 ...
- 18款js和jquery文字特效代码分享
18款js和jquery文字特效代码分享 jQCloud标签云插件_热门城市文字标签云代码 js 3d标签云特效关键词文字球状标签云代码 原生JS鼠标悬停文字球状放大显示效果代码 原生js文字动画圆形 ...
- cnblogs鼠标点击特效
喜大普奔! 伸手党福利 ! 创建mouse.js文件, 上传到博客, 直接引用即可, 内容如下: (function(window, document, undefined) { var hearts ...
- JS网站图集相册特效
JS网站图集相册特效是一款可以直接使用鼠标进行前后导航,也可以通过缩略图来切换图片. 在线演示本地下载
- js鼠标滚轮滚动图片切换效果
效果体验网址:http://keleyi.com/keleyi/phtml/image/12.htm HTML文件代码: <!DOCTYPE html PUBLIC "-//W3C// ...
- js 鼠标事件的抓取代码
js 鼠标事件的抓取代码,分享给大家. 1.通过ele.setCapture();设置鼠标事件的抓取. 2,应用可以通过单.双击文字来获取时间. <html> <head> & ...
- js鼠标及对象坐标控制属性详细解析
对js鼠标及对象坐标控制属性进行了详细的分析介绍. offsetTop获取对象相对于版面或由 offsetParent 属性指定的父坐标的计算顶端位置. offsetLeft获取对象相对于版面或由 ...
随机推荐
- Linux命令学习-cp命令
Linux中,cp命令的全称是copy,主要作用是复制文件或文件夹,类似于Windows下的复制功能. 假设当前处于wintest用户的主目录,路径为 /home/wintest ,存在文件夹test ...
- 1. Python 魔法方法
Python 魔法方法 基础: 如果你想... 所以,你写... Python调用... 初始化一个实例 x = MyClass() x.__init__() 作为一个字符串的"官方&quo ...
- .Net Core 学习路由和请求参数传递
一.配置默认路由方式 {Controller=Home}/{action=Index}/{id?} 默认请求地址:http://localhost:xxx/home/index /id? 是可选项例如 ...
- Linux日志系统分析:rsyslog、syslog和klog
参考博客: https://blog.csdn.net/lidonghat/article/details/55004280 https://blog.csdn.net/u012247418/arti ...
- EF简介及CRUD简单DEMO
一.实体框架(Entity FrameWork)简介 • 简称EF • 与Asp.Net MVC关系与ADO.NET关系 • ADO.NET Entity FrameWork是微软以ADO.NET为基 ...
- 小白开学Asp.Net Core《二》
小白开学Asp.Net Core<二> ——数据仓储层(Repositroy) 一.历史现象 在后端开发中,数据库操作是最频繁的,每一个开发人员都会接触,甚至不少开发人员每天的工作就是与数 ...
- Linux基础之定时任务
30.1)什么是定时任务 定时任务命令是cond,crond就是计划任务,类似于我们平时生活中的闹钟,定点执行. 30.2)为什么要用crond 计划任务主要是做一些周期性的任务,比如凌晨3点定时备份 ...
- 走近OPENCV // opencv 2.4.9+vs2013配置
一直很懒去配opencv,这几周忍不了终于抽空来配了一下环境... 用的是旧版opencv2.4系列,最新到3.0了,之后再看看教程不知道有什么特别大的区别. (FF14国服没有4.0 // 8.19 ...
- 微信小程序登陆流程图时序图
微信小程序登录 小程序可以通过微信官方提供的登录能力方便地获取微信提供的用户身份标识,快速建立小程序内的用户体系. 微信小程序登录流程时序图 说明 调用 wx.login() 获取 临时登录凭证cod ...
- Python基础总结之第十天开始【认识一下python的另一个数据对象-----字典】(新手可相互督促)
看了大家的评论,还是有意外的收货.感谢每个小伙伴的评论与补充. 众人拾柴火焰高~ 今天的笔记是记录python中的数据对象----字典! 前面有讲到list列表和tuple元组的笔记,他们都是一样可以 ...