从一个大神那看到的,拷贝过来跟大家分享下!

html

<canvas></canvas>

*{margin:0;padding:0;}body{background:#222;}
canvas{
position:absolute;
border:5px solid rgba(255,255,255,0.1);
box-shadow:inset 0 0 100px #4162a9;
}

css

var   canvas = document.querySelector('canvas'),
ctx = canvas.getContext('2d'),
particles = [],
patriclesNum = 500,
w = 500,
h = 500,
colors = ['#f35d4f','#f36849','#c0d988','#6ddaf1','#f1e85b']; canvas.width = 500;
canvas.height = 500;
canvas.style.left = (window.innerWidth - 500)/2+'px'; if(window.innerHeight>500)
canvas.style.top = (window.innerHeight - 500)/2+'px'; function Factory(){
this.x = Math.round( Math.random() * w);
this.y = Math.round( Math.random() * h);
this.rad = Math.round( Math.random() * 1) + 1;
this.rgba = colors[ Math.round( Math.random() * 3) ];
this.vx = Math.round( Math.random() * 3) - 1.5;
this.vy = Math.round( Math.random() * 3) - 1.5;
} function draw(){
ctx.clearRect(0, 0, w, h);
ctx.globalCompositeOperation = 'lighter';
for(var i = 0;i < patriclesNum; i++){
var temp = particles[i];
var factor = 1; for(var j = 0; j<patriclesNum; j++){ var temp2 = particles[j];
ctx.linewidth = 0.5; if(temp.rgba == temp2.rgba && findDistance(temp, temp2)<50){
ctx.strokeStyle = temp.rgba;
ctx.beginPath();
ctx.moveTo(temp.x, temp.y);
ctx.lineTo(temp2.x, temp2.y);
ctx.stroke();
factor++;
}
} ctx.fillStyle = temp.rgba;
ctx.strokeStyle = temp.rgba; ctx.beginPath();
ctx.arc(temp.x, temp.y, temp.rad*factor, 0, Math.PI*2, true);
ctx.fill();
ctx.closePath(); ctx.beginPath();
ctx.arc(temp.x, temp.y, (temp.rad+5)*factor, 0, Math.PI*2, true);
ctx.stroke();
ctx.closePath(); temp.x += temp.vx;
temp.y += temp.vy; if(temp.x > w)temp.x = 0;
if(temp.x <)temp.x = w;
if(temp.y > h)temp.y = 0;
if(temp.y <)temp.y = h;
}
} function findDistance(p1,p2){
return Math.sqrt( Math.pow(p2.x - p1.x, 2) + Math.pow(p2.y - p1.y, 2) );
} window.requestAnimFrame = (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
function( callback ){
window.setTimeout(callback, 1000 / 60);
};
})(); (function init(){
for(var i = 0; i < patriclesNum; i++){
particles.push(new Factory);
}
})(); (function loop(){
draw();
requestAnimFrame(loop);
})();

js

html5 canvas 运行起来绝对让你震撼!的更多相关文章

  1. 16个富有创意的HTML5 Canvas动画特效集合

    HTML5技术正在不断的发展和更新,越来越多的开发者也正在加入HTML5阵营,甚至在移动开发上HTML5的地位也是越来越重要了.HTML5中的大部分动画都是通过Canvas实现,因为Canvas就像一 ...

  2. 16个非常有趣的HTML5 Canvas动画特效集合

    HTML5技术正在不断的发展和更新,越来越多的开发者也正在加入HTML5阵营,甚至在移动开发上HTML5的地位也是越来越重要了.HTML5中的大部分动画都是通过Canvas实现,因为Canvas就像一 ...

  3. HTML5 程序设计 - 使用HTML5 Canvas API

    请你跟着本篇示例代码实现每个示例,30分钟后,你会高喊:“HTML5 Canvas?!在哥面前,那都不是事儿!” 呵呵.不要被滚动条吓到,很多都是代码和图片.我没有分开写,不过上面给大家提供了目录,方 ...

  4. 赠书:HTML5 Canvas 2d 编程必读的两本经典

    赠书:HTML5 Canvas 2d 编程必读的两本经典 这两年多一直在和HTML5 Canvas 打交道,也带领团队开发了世界首款基于HTML5 Canvas 的演示文档工具---AxeSlide( ...

  5. 如何开发一个简单的HTML5 Canvas 小游戏

    原文:How to make a simple HTML5 Canvas game 想要快速上手HTML5 Canvas小游戏开发?下面通过一个例子来进行手把手教学.(如果你怀疑我的资历, A Wiz ...

  6. 用html5 canvas和JS写个数独游戏

    为啥要写这个游戏? 因为我儿子二年级数字下册最后一章讲到了数独.他想玩儿. 因为我也想玩有提示功能的数独. 因为我也正想决定要把HTML5和JS搞搞熟.熟悉一个编程平台,最好的办法,就是了解其原理与思 ...

  7. 使用html5 canvas绘制圆形或弧线

    注意:本文属于<html5 Canvas绘制图形入门详解>系列文章中的一部分.如果你是html5初学者,仅仅阅读本文,可能无法较深入的理解canvas,甚至无法顺畅地通读本文.请点击上述链 ...

  8. 开源HTML5 Canvas游戏Runtime发布

    Cantk-Runtime是通用的HTML5 Canvas 2D游戏引擎运行库,让HTML5游戏的性能飞起来.Cantk-Runtime以PhoneGap插件的方式提供,从此结束PhoneGap低性能 ...

  9. 使用 HTML5 canvas 绘制精美的图形

    HTML5 是一个新兴标准,它正在以越来越快的速度替代久经考验的 HTML4.HTML5 是一个 W3C “工作草案” — 意味着它仍然处于开发阶段 — 它包含丰富的元素和属性,它们都支持现行的 HT ...

随机推荐

  1. <memory> is not a BOMStorage file

    解决 Autoresizing 和AutoLayout 冲突 设置 self.autoresizingMask = UIViewAutoresizingNone;

  2. 《think in python》学习-5

    think in python -5 think in python -5 条件和递归 求模操作符% 用于整数,可以计算出第一个操作数除以第二个操作数的余数 7%3 #结果是2 求模操作符%有很多用途 ...

  3. SharePoint2010 Form验证配置流程

    1.修改管理中心的Web.config文件,位置:C:\inetpub\wwwroot\wss\VirtualDirectories\42903 2.修改应用程序的Web.config文件,位置:C: ...

  4. win32 消息说明

    WM_NULL = $0000; WM_CREATE = $0001; 应用程序创建一个窗口 WM_DESTROY = $0002; 一个窗口被销毁 WM_MOVE = $0003; 移动一个窗口 W ...

  5. c++中自增(++)和自减(--)操作符

    自增(++)和自减(--)操作符为对象加1 或减1 操作提供了方便简短的实现方式.它们有前置和后置两种使用形式.到目前为止,我们已经使用过前自增操作,该操作使其操作数加1,操作结果是修改后的值.同理, ...

  6. Oracle自动执行任务(存储过程)

    Oracle自动执行任务(存储过程) SQL> variable job number;SQL> begin2 dbms_job.submit(:job,'存储过程名;',sysdate, ...

  7. [转]取代cookie的网站追踪技术:”帆布指纹识别”初探

    [前言] 一般情况下,网站或者广告联盟都会非常想要一种技术方式可以在网络上精确定位到每一个个体,这样可以通过收集这些个体的数据,通过分析后更加精准的去推送广告(精准化营销)或其他有针对性的一些活动.C ...

  8. VBA基础概念

    一:VBA对象 'VBA对象 'VBA中的对象其实就是我们操作的具有方法.属性的excel中支持的对象 'Excel中的几个常用对象表示方法 '1.工作簿 ' Workbooks 代表工作簿集合,所有 ...

  9. C++虚函数在内存中的实现

    首先来一张图,一目了然: 然后把相应的代码贴上来: class A { int a; public: virtual void f(); virtual void g(int); virtual vo ...

  10. 通过 IP 访问谷歌

    最近貌似谷歌都不能访问,对于我等经常使用谷歌的人说不是件好事,毕竟谷歌比百度专业,好在有解决办法: 1. 找到文件:C:\Windows\System32\drivers\etc\hosts 2. 把 ...