手写particles
 var canvas = document.getElementById('canvas');
    var ctx = canvas.getContext('2d');
    var Grewer = {
        init: function() {
            this.getWindowSize();
            canvas.width = this.w;
            canvas.height = this.h;
            this.num = 50;
            this.range = 100;
            this.arr = [];
            this.add();
        },
        getWindowSize: function() {
            //获取窗口宽度
            if (window.innerWidth) { //兼容火狐,谷歌,safari等浏览器
                this.w = window.innerWidth;
            } else if ((document.body) && (document.body.clientWidth)) { //兼容IE浏览器
                this.w = document.body.clientWidth;
            }
            //获取窗口高度
            if (window.innerHeight) {
                this.h = window.innerHeight;
            } else if ((document.body) && (document.body.clientHeight)) {
                this.h = document.body.clientHeight;
            }
        },
        update: function(obj) {
            obj.x += obj.vx;
            obj.y += obj.vy;
            if (obj.x < 0 || obj.x > this.w) {
                obj.vx *= -1;
            }
            if (obj.y < 0 || obj.y > this.h) {
                obj.vy *= -1;
            }
        },
        add: function() {
            var i = this.num;
            while (i--) {
                var particles = {
                    x: (Math.random() * this.w) | 0,
                    y: (Math.random() * this.h) | 0,
                    vx: (Math.random() - .5) * 1,
                    vy: (Math.random() - .5) * 1,
                }
                this.arr.push(particles);
            }
        },
        checkDist: function(a, b, dist) {
            var x = a.x - b.x,
                y = a.y - b.y;
            return x * x + y * y <= dist * dist;
        },
        print: function(obj) {
            ctx.beginPath();
            ctx.arc(obj.x, obj.y, 2, 0, 2 * Math.PI);
            ctx.fillStyle = '#ddd';
            ctx.fill();
        }
    }
    var G = Object.create(Grewer);
    G.init();
    var Ganim = function() {
        window.requestAnimationFrame(Ganim);
        ctx.fillStyle = '#fff';
        ctx.fillRect(0, 0, G.w, G.h);
        var length = G.num;
        for (var i = 0; i < length; i++) {
            var o1 = G.arr[i]
            G.update(o1);
            G.print(o1);
            for (var j = i + 1; j < length; ++j) {
                var o2 = G.arr[j];
                if (G.checkDist(o1, o2, G.range)) {
                    ctx.strokeStyle = '#ddd';
                    ctx.beginPath();
                    ctx.moveTo(o1.x, o1.y);
                    ctx.lineTo(o2.x, o2.y);
                    ctx.stroke();
                }
            }
        }
    }
    Ganim();
旧版本
demo:https://grewer.github.io/JsDemo/particles/
github:https://github.com/Grewer/JsDemo/tree/master/particles
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
var Grewer = {
    init: function() {
        this.getWindowSize();
        canvas.width = this.w;
        canvas.height = this.h;
        this.num = 70;
        this.range = 80;
        this.arr = [];
        this.add();
    },
    getWindowSize: function() {
        //获取窗口宽度
        if (window.innerWidth) { //兼容火狐,谷歌,safari等浏览器
            this.w = window.innerWidth;
        } else if ((document.body) && (document.body.clientWidth)) { //兼容IE浏览器
            this.w = document.body.clientWidth;
        }
        //获取窗口高度
        if (window.innerHeight) {
            this.h = window.innerHeight;
        } else if ((document.body) && (document.body.clientHeight)) {
            this.h = document.body.clientHeight;
        }
    },
    update: function(obj) {
        obj.x += obj.vx;
        obj.y += obj.vy;
        if (obj.x < 0 || obj.x > this.w) {
            obj.vx *= -1;
        }
        if (obj.y < 0 || obj.y > this.h) {
            obj.vy *= -1;
        }
    },
    add: function() {
        var i = this.num;
        while (i--) {
            var particles = {
                x: (Math.random() * this.w) | 0,
                y: (Math.random() * this.h) | 0,
                vx: (Math.random() - .5) * 1,
                vy: (Math.random() - .5) * 1,
                r: ((Math.random() * 2) | 0) + 1
            }
            this.arr.push(particles);
        }
    },
    checkDist: function(a, b, dist) {
        var x = a.x - b.x,
            y = a.y - b.y;
        return x * x + y * y <= dist * dist;
    },
    print: function(obj) {
        ctx.beginPath();
        ctx.arc(obj.x, obj.y, obj.r, 0, 2 * Math.PI);
        ctx.fillStyle = '#cccccc';
        ctx.fill();
    }
}
var G = Object.create(Grewer);
G.init();
var Ganim = function() {
    window.requestAnimationFrame(Ganim);
    ctx.fillStyle = '#fff';
    ctx.fillRect(0, 0, G.w, G.h);
    var length = G.arr.length;
    for (var i = 0; i < length; i++) {
        var o1 = G.arr[i]
        G.update(o1);
        G.print(o1);
        for (var j = i + 1; j < length; ++j) {
            var o2 = G.arr[j];
            if (G.checkDist(o1, o2, G.range)) {
                ctx.strokeStyle = '#cccccc';
                ctx.beginPath();
                ctx.moveTo(o1.x, o1.y);
                ctx.lineTo(o2.x, o2.y);
                ctx.stroke();
            }
        }
    }
}
G.arr.push({
    x: 1,
    y: 1,
    vx: 0,
    vy: 0,
    r: 1
})
document.addEventListener('mousemove', function(e) {
    G.arr[G.num].x = e.clientX;
    G.arr[G.num].y = e.clientY;
}, false)
Ganim();
手写particles的更多相关文章
- 【Win 10 应用开发】手写识别
		记得前面(忘了是哪天写的,反正是前些天,请用力点击这里观看)老周讲了一个14393新增的控件,可以很轻松地结合InkCanvas来完成涂鸦.其实,InkCanvas除了涂鸦外,另一个大用途是墨迹识别, ... 
- JS / Egret 单笔手写识别、手势识别
		UnistrokeRecognizer 单笔手写识别.手势识别 UnistrokeRecognizer : https://github.com/RichLiu1023/UnistrokeRecogn ... 
- 如何用卷积神经网络CNN识别手写数字集?
		前几天用CNN识别手写数字集,后来看到kaggle上有一个比赛是识别手写数字集的,已经进行了一年多了,目前有1179个有效提交,最高的是100%,我做了一下,用keras做的,一开始用最简单的MLP, ... 
- 【转】机器学习教程 十四-利用tensorflow做手写数字识别
		模式识别领域应用机器学习的场景非常多,手写识别就是其中一种,最简单的数字识别是一个多类分类问题,我们借这个多类分类问题来介绍一下google最新开源的tensorflow框架,后面深度学习的内容都会基 ... 
- caffe_手写数字识别Lenet模型理解
		这两天看了Lenet的模型理解,很简单的手写数字CNN网络,90年代美国用它来识别钞票,准确率还是很高的,所以它也是一个很经典的模型.而且学习这个模型也有助于我们理解更大的网络比如Imagenet等等 ... 
- 使用神经网络来识别手写数字【译】(三)- 用Python代码实现
		实现我们分类数字的网络 好,让我们使用随机梯度下降和 MNIST训练数据来写一个程序来学习怎样识别手写数字. 我们用Python (2.7) 来实现.只有 74 行代码!我们需要的第一个东西是 MNI ... 
- 手写原生ajax
		关于手写原生ajax重要不重要,各位道友自己揣摩吧, 本着学习才能进步,分享大家共同受益,自己也在自己博客里写一下 function createXMLHTTPRequest() { //1.创建XM ... 
- springmvc 动态代理  JDK实现与模拟JDK纯手写实现。
		首先明白 动态代理和静态代理的区别: 静态代理:①持有被代理类的引用 ② 代理类一开始就被加载到内存中了(非常重要) 动态代理:JDK中的动态代理中的代理类是动态生成的.并且生成的动态代理类为$Pr ... 
- 为sproto手写了一个python parser
		这是sproto系列文章的第三篇,可以参考前面的<为sproto添加python绑定>.<为python-sproto添加map支持>. sproto是云风设计的序列化协议,用 ... 
随机推荐
- 第四章 MySQL高级查询(二)
			第四章 MySQL高级查询(二) 一.EXISTS子查询 在执行create 或drop语句之前,可以使用exists语句判断该数据库对像是否存在,返回值是true或false.除此之外,exists ... 
- 使用MVVM减少控制器代码实战(减少56%)
			减少比例= (360(原来的行数)-159(瘦身后的行数))/360 = 56% 父类 MVC 和MVVM 前后基本不动 父类主要完成如下三个功能: 1)功能:MJRefrsh +上拉下拉没有更多数据 ... 
- Prometheus 架构 - 每天5分钟玩转 Docker 容器技术(83)
			Prometheus 是一个非常优秀的监控工具.准确的说,应该是监控方案.Prometheus 提供了监控数据搜集.存储.处理.可视化和告警一套完整的解决方案. 让我们先来看看 Prometheus ... 
- win10 uwp 弹起键盘不隐藏界面元素
			本文主要讲,在我们使用手机输入的时候,会因为手机的虚拟键盘隐藏了一些界面的元素.我们有一个简单的方法让虚拟键盘不隐藏界面元素. 我们需要的界面元素是在显示了虚拟键盘后的空间能全部显示,如果不能的话,还 ... 
- LINUX 笔记-watch命令
			命令格式:watch[参数][命令] 可以将命令的输出结果输出到标准输出设备,多用于周期性执行命令/定时执行命令 命令参数: -n或--interval watch缺省每2秒运行一下程序,可以用-n ... 
- 使用EF对已存在的数据库进行模块化数据迁移
			注:本文面向的是已经对EF的迁移功能有所了解,知道如何在控制台下进行相关命令输入的读者 问题 最近公司项目架构使用ABP进行整改,顺带想用EF的自动迁移代替了以前的手工脚本. 为什么要替代? 请看下图 ... 
- LeetCode 112. Path Sum (二叉树路径之和)
			Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ... 
- Ionic3 一些命令
			ionic start --help E:\Projects\ionic>ionic start --help ionic start - Create a new project This c ... 
- Linux学习(十四)磁盘格式化、磁盘挂载、手动增加swap空间
			一.磁盘格式化 分好去的磁盘需要格式化之后才可以使用.磁盘分区一般用mke2fs命令或者mkfs.filesystemtype.这个filesystemtype分为ext4,ext3,xfs等等.xf ... 
- 用js判断是否为手机浏览,如果是手机浏览就跳转到手机站
			<script type="text/javascript"> function browserRedirect() { var sUserAgent= navigat ... 
