canvas 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();
canvas particles的更多相关文章
- Particles.js基于Canvas画布创建粒子原子颗粒效果
文章目录 使用方法 自定义参数 相关链接 Particles.js是一款基于HTML5 Canvas画布的轻量级粒子动画插件,可以设置粒子的形状.旋转.分布.颜色等属性,还可以动态添加粒子,效果非常炫 ...
- 惊艳!9个不可思议的 HTML5 Canvas 应用试验
HTML5 <canvas> 元素给网页中的视觉展示带来了革命性的变化.Canvas 能够实现各种让人惊叹的视觉效果和高效的动画,在这以前是需要 Flash 支持或者 JavaScript ...
- JavaScript 加载动画Canvas 设计
var c = document.getElementById('c'), ctx = c.getContext('2d'), cw = c.width = 400, ch = c.height = ...
- canvas 的一些效果
<html> <head> <style> *{ margin: 0; padding: 0; } body{ background:green; } #div{ ...
- 弄个知乎的粒子动态背景_实践particles.js
好久没登录知乎,发现他们的登录页面粒子动态效果蛮炫的,查一下代码用了Particles.js基于Canvas画布创建粒子颗粒效果. 上图 上图: 感觉有比格,就照着弄了一个,玩玩. githu ...
- 用canvas 实现个图片三角化(LOW POLY)效果
之前无意中看到Ovilia 用threejs做了个LOW POLY,也就是图片平面三角化的效果,觉得很惊艳,然后就自己花了点时间尝试了一下. 我是没怎么用过threejs,所以就直接用canvas的2 ...
- 打造高大上的Canvas粒子(一)
HTML5 Canvas <canvas>标签定义图形,比如图表和其他图像,必须用脚本(javascript)绘制图形. 举例:绘制矩形 <script> var c = do ...
- Canvas实现文字粒子化,并且绕轴旋转(完善)
1. 之前有放过一个初始版本,但是因为在旋转的时候,有比较大的瑕疵,造成每个点运动到端点后,出现类似撞击的感觉. 2. 所以本文对旋转作了些调整,运用类似水平方向的圆周运动 a. HTML代码,定义c ...
- h5 canvas
概述 Canvas API(画布)用于在网页实时生成图像,并且可以操作图像内容,基本上它是一个可以用JavaScript操作的位图(bitmap). 使用前,首先需要新建一个canvas网页元素. & ...
随机推荐
- conflunce安装配置
下载 下载Confluence-v5.4.4.zip包,其中包含 atlassian-confluence-5.4.4-x64.bin #程序二进制文件 confluence5.x-crack.z ...
- bzoj 4991 [Usaco2017 Feb]Why Did the Cow Cross the Road III(cdq分治,树状数组)
题目描述 Farmer John is continuing to ponder the issue of cows crossing the road through his farm, intro ...
- 【NOIP2017练习】函数变换(DP,dfs)
题意: 思路: 极限步数大概不会超过30 ; ..max,..]of longint; eul:..max]of longint; cas,v,n,k,i,ans,j:longint; functio ...
- 【IntelliJ】IntelliJ IDEA常用设置及快捷键以及自定义Live templates
IntelliJ IDEA是一款非常优秀的JAVA编辑器,初学都可会对其中的一些做法感到很别扭,刚开始用的时候我也感到很不习惯,在参考了网上一些文章后在这里把我的一些经验写出来,希望初学者能快速适应它 ...
- 【小记事】电脑命令行开WiFi
1.设置WiFi名称和密码 在命令行输入: netsh wlan set hostednetwork mode=allow WiFi名称 key=密码 2.开启WiFi 在命令行输入: netsh w ...
- Spring Boot为我们准备了最佳的数据库连接池方案,只需要在属性文件(例如application.properties)中配置需要的连接池参数即可。
Spring Boot为我们准备了最佳的数据库连接池方案,只需要在属性文件(例如application.properties)中配置需要的连接池参数即可.
- spark开发环境配置
以后spark,mapreduce,mpi可能三者集于同一平台,各自的侧重点有所不用,相当于云计算与高性能计算的集合,互补,把spark的基础看了看,现在把开发环境看看,主要是看源码,最近Apache ...
- CentOS 7 防火墙开启了哪些服务和端口?
过滤封包功能的 netfilter 已经内建在 CentOS 7 的内核,但是配置 netfilter 的界面程式 firewalld 却未必有安装,不论是否已经安装,都可以执行下面的安装指令: yu ...
- AutoCAD如何方便截图放到Word文档,改成白底黑字
将模型视图切换到布局2即可 比如下图所示的效果 先回到模型视图把所有线条颜色都改成白色,然后添加适当的标注(比如要受力分析,则在CAD中绘制箭头也很方便的),文字说明.然后切换到布局2就OK ...
- Codeforces Round #313 B. Gerald is into Art(简单题)
B. Gerald is into Art time limit per test 2 seconds memory limit per test 256 megabytes input standa ...