1.随机产生形状,做360度运转,带有一个开始开始按钮一个停止按钮

 var canvas=$('.mycanvas');
canvas.attr("width",500);//$(window).get(0).innerWidth
canvas.attr("height",500);//$(window).get(0).innerHeight
var context=canvas.get(0).getContext('2d');
 var startbtn=$('#started'),stopbtn=$("#stoped"),palyAnimation = true;
startbtn.hide();
startbtn.click(function(){
$(this).hide();
stopbtn.show();
palyAnimation = true;
animate();
})
stopbtn.click(function(){
$(this).hide();
startbtn.show();
palyAnimation = false;
})
var Shape = function(x,y,width,height){
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.radius = Math.random()*30;
this.angle = 0;
}
var shapes = new Array();
for(var i= 0; i< 10; i++){
var x = Math.random()*250,y = Math.random()*250,width = height =Math.random()*50;
shapes.push(new Shape(x,y,width,height));
} /*shapes.push(new Shape(50,50,10,10));
shapes.push(new Shape(100,100,10,10));
shapes.push(new Shape(150,150,10,10));*/
function animate(){
context.clearRect(0,0,500,500);
var shapeslength = shapes.length;
for(var i=0;i< shapeslength;i++){
var tmpShape = shapes[i];
x = tmpShape.x+(tmpShape.radius*Math.cos(tmpShape.angle*(Math.PI/180)));
y = tmpShape.y+(tmpShape.radius*Math.sin(tmpShape.angle*(Math.PI/180))); tmpShape.angle += 5;
if(tmpShape.angle > 360){
tmpShape.angle = 0;
};
context.fillStyle ="rgb("+Math.floor(Math.random()*255)+","+Math.floor(Math.random()*255)+","+Math.floor(Math.random()*255)+")";//随机颜色
context.fillRect(x,y,tmpShape.width,tmpShape.height);
};
if(palyAnimation){ setTimeout(animate,33)};
}
animate();

2.随机生成的形状,左右运动,遇边界反弹

var startbtn=$('#started'),stopbtn=$("#stoped"),palyAnimation = true;
startbtn.hide();
startbtn.click(function(){
$(this).hide();
stopbtn.show();
palyAnimation = true;
animate();
})
stopbtn.click(function(){
$(this).hide();
startbtn.show();
palyAnimation = false;
})
var Shape = function(x,y,width,height){
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.radius = Math.random()*30;
this.angle = 0;
this.reverseX = false;
this.reverseY = false;
}
var shapes = new Array();
for(var i= 0; i< 10; i++){
var x = Math.random()*250,y = Math.random()*250,width = height =Math.random()*50;
shapes.push(new Shape(x,y,width,height));
} function animate(){
context.clearRect(0,0,500,500);
var shapeslength = shapes.length;
for(var i=0;i< shapeslength;i++){
var tmpShape = shapes[i];
//x = tmpShape.x+(tmpShape.radius*Math.cos(tmpShape.angle*(Math.PI/180)));
//y = tmpShape.y+(tmpShape.radius*Math.sin(tmpShape.angle*(Math.PI/180)));
if(tmpShape.x < 0){
tmpShape.reverseX =false;
}else if(tmpShape.x + tmpShape.width > 500){
tmpShape.reverseX = true;
}
if(tmpShape.y < 0){
tmpShape.reverseY =false;
}else if(tmpShape.y + tmpShape.height > 500){
tmpShape.reverseY = true;
}
if(!tmpShape.reverseX){
tmpShape.x +=2;
} else{
tmpShape.x -=2;
}
if(!tmpShape.reverseY){
tmpShape.y +=2;
} else{
tmpShape.y -=2;
} context.fillRect(tmpShape.x,tmpShape.y,tmpShape.width,tmpShape.height);
};
if(palyAnimation){ setTimeout(animate,33)};
}
animate();

canvas 动画的更多相关文章

  1. 2015.4.23 贪吃蛇、canvas动画,各种上传工具,url信息匹配以及最全前端面试题等

    1.面向对象贪吃蛇   2.css中:hover 改变图片 页面加载完 第一次鼠标移入会闪一下 这是为啥? 解决方法:你把两张图合成一张图或者是先把图片加载到页面上,然后再hover出来. 解析:图片 ...

  2. HTML动画分类 HTML5动画 SVG库 SVG工具 Canvas动画工具

     1.js配合传统css属性控制,可以使用setTimeout或者高级的requestAnimationFrame 2.css3 3.svg 4.canvas(当然,这个还是要配合js)   也许这么 ...

  3. 7 个顶级的 HTML5 Canvas 动画赏析

    HTML5确实是一项改革浏览器乃至整个软件行业的新技术,它可以帮助我们Web开发者很方便地在网页上实现动画特效,而无需臃肿的Flash作为支撑.本文分享7个顶级的HTML5 Canvas 动画,都有非 ...

  4. 8个经典炫酷的HTML5 Canvas动画欣赏

    HTML5非常强大,尤其是Canvas技术的应用,让HTML5几乎可以完成所有Flash能完成的效果.本文精选了8个经典炫酷的HTML5 Canvas动画欣赏,每一个都提供全部的源代码,希望对你有所帮 ...

  5. 7个惊艳的HTML5 Canvas动画效果及源码

    HTML5非常强大,尤其是现在大部分浏览器都支持HTML5和CSS3,用HTML5制作的动画也多了起来.另外,Canvas上绘制图形非常简单,本文就分享了一些强大的HTML5 Cnavas动画,一起来 ...

  6. 《FLASH CC 2015 CANVAS 中文教程》——1、导出canvas动画,文件结构浅析

    注::如果你对 FLASH 这个软件操作不够熟悉,建议你可以先看看FLASH动画之类的书. :FLASH CC 在文中直接简称为CC. :以下所以文章中所说的快捷键 如果你按了不起作用,请检查是否有其 ...

  7. Html5 Canvas动画旋转的小方块;

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...

  8. 【原创】测试不同浏览器播放canvas动画的平滑程度

    Canvas无疑是HTML5开放式网络平台最激动人心的技术之一.目前,除了IE8以外,各类浏览器的新版本都支持HTML5 Canvas. 程序员需要通过Javascript调用Canvas API.基 ...

  9. HTML5 Canvas动画效果演示

    HTML5 Canvas动画效果演示 主要思想: 首先要准备一张有连续帧的图片,然后利用HTML5 Canvas的draw方法在不同的时间 间隔绘制不同的帧,这样看起来就像动画在播放. 关键技术点: ...

  10. HTML5 Canvas动画效果演示 - 流浪的鱼 - 博客频道 - CSDN.NET

    HTML5 Canvas动画效果演示 - 流浪的鱼 - 博客频道 - CSDN.NET HTML5 Canvas动画效果演示

随机推荐

  1. Python模块(pickle)

    pickle 序列化和反序列化 序列化作用 序列化使用 cPickle使用例 Python提供一个标准的模块,称为pickle.使用它你可以在一个文件中储存任何Python对象,之后你又可以把它完整无 ...

  2. linux普通用户权限设置为超级用户权限方法、sudo不用登陆密码

    以用户zato为例 普通用户权限设置为超级用户权限 进入有超级用户权限的账号 添加文件可写(w)权限 sudo chmod u+x /etc/sudoers 编辑/etc/sudoers文件 添加语句 ...

  3. jQuery.proxy()函数

    jQuery.proxy(),接受一个函数,然后返回一个新函数,并且这个新函数始终保持了特定的上下文(context)语境.   context 代表上下文 name是上下文的某个属性 jQuery. ...

  4. WebService之Axis2(5):会话(Session)管理

    WebService给人最直观的感觉就是由一个个方法组成,并在客户端通过SOAP协议调用这些方法.这些方法可能有返回值,也可能没有返回值.虽然这样可以完成一些工具,但这些被调用的方法是孤立的,当一个方 ...

  5. Emmet 的使用

    Emmet 的介绍 Emmet 的前身叫做:Zen Coding,也许熟知旧名的人不在少数.Emmet 一般前端工程师用得比较多,具体它是做什么的,我们通过下面两张 Gif 演示图来说明: Intel ...

  6. iOS 提交代码出现提示弹出框显示 “A commit message is required to perform this operation.Enter a commit message and try again.“

    需要你写一下你确认提交的信息,就是你这次提交上去修改了什么功能,简单描述一下

  7. C++Primer 第十四章

    //1.当运算符作用于类类型运算对象时,可以通过运算符重载重新定义该运算符的含义.明智的使用运算符重载能令程序更加易于编写和阅读. //2.重载的运算符是具有特殊名字的函数,它们由关键字operato ...

  8. Swift游戏实战-跑酷熊猫 00 游戏预览

    这个系列我们将要一起来做一个这样的游戏

  9. Aptana Studio3开发Python和Ruby(最佳工具)

    即从: http://d1iwq2e2xrohf.cloudfront.net/tools/studio/standalone/3.3.1.201212171919/win/Aptana_Studio ...

  10. ofbiz进击 第六节。 --OFBiz配置之[widget.properties] 配置属性的分析

    配置内容分析如下 # -- 定义上下文使用者 -- security.context =default # -- 定义密码限制长度最小值 -- password.length.min =5 # -- ...