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_类

    1. 对象的概念 对象包括特性和方法.特性只是作为对象的一部分的变量,方法则是存储在对象内的函数.对象中的方法和其他函数的区别在于方法总是将对象作为自己的第一个参数,这个参数一般称为self. 2. ...

  2. Linux 内核版本规律

    版本组成:主版本号.次版本号.修正版本号 主版本号和次版本号一起标志着重要的功能变更,修正版本号表示较小的功能变更.次版本号表示该版本是否为稳定版本,偶数则为稳定版本,奇数则可能存在一些BUG.

  3. oracle pl sql 解锁表

    select   p.spid,a.serial#, c.object_name,b.session_id,b.oracle_username,b.os_user_name   from   v$pr ...

  4. Java基础之写文件——将多个字符串写入到文件中(WriteProverbs)

    控制台程序,将一系列有用的格言写入到文件中. 本例使用通道把不同长度的字符串写入到文件中,为了方便从文件中恢复字符串,将每个字符串的长度写入到文件中紧靠字符串本身前面的位置,这可以告知在读取字符串之前 ...

  5. oracle组建:ODAC112021Xcopy_x64,在开发机上,不用安装oracle的客户端等开发

    以下解决方案是为了连接远程服务器上的oracle 11g 的解决方案. 下载地址:http://www.oracle.com/technetwork/database/windows/download ...

  6. CSS For Bar Graphs(maybe old)

    Having a working knowledge of XHTML and CSS when developing applications is a big help in knowing wh ...

  7. 当As3遇见Swift(二)

    字符串:String 都是用String来表示,都是值类型,在传递过程中都会进行拷贝. 计算字符数量 As3: str.length Swift: countElements(str) 数组:Arra ...

  8. navicat 的查询功能

    navicat的查询的位置在: 在编辑器界面写代码,代码完成后点左上角的运行. 代码: create(创建)  table(一个表) <xxx>尖括号内的内容必填——我要创建并查询一个名叫 ...

  9. SQL null值 查询null

    select * from emp;

  10. 树形DP(简单题)(Y HDU4705)

    题意:给出一个n个节点的树形图,统计{A,B,C}的数量,其中ABC分别是树上三个不同的节点,并且这三个节点不能被一条路径覆盖 分析:对于下图 进行dfs深搜统计,num[u]统计回溯到当前节点u,并 ...