canvas 动画
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 动画的更多相关文章
- 2015.4.23 贪吃蛇、canvas动画,各种上传工具,url信息匹配以及最全前端面试题等
1.面向对象贪吃蛇 2.css中:hover 改变图片 页面加载完 第一次鼠标移入会闪一下 这是为啥? 解决方法:你把两张图合成一张图或者是先把图片加载到页面上,然后再hover出来. 解析:图片 ...
- HTML动画分类 HTML5动画 SVG库 SVG工具 Canvas动画工具
1.js配合传统css属性控制,可以使用setTimeout或者高级的requestAnimationFrame 2.css3 3.svg 4.canvas(当然,这个还是要配合js) 也许这么 ...
- 7 个顶级的 HTML5 Canvas 动画赏析
HTML5确实是一项改革浏览器乃至整个软件行业的新技术,它可以帮助我们Web开发者很方便地在网页上实现动画特效,而无需臃肿的Flash作为支撑.本文分享7个顶级的HTML5 Canvas 动画,都有非 ...
- 8个经典炫酷的HTML5 Canvas动画欣赏
HTML5非常强大,尤其是Canvas技术的应用,让HTML5几乎可以完成所有Flash能完成的效果.本文精选了8个经典炫酷的HTML5 Canvas动画欣赏,每一个都提供全部的源代码,希望对你有所帮 ...
- 7个惊艳的HTML5 Canvas动画效果及源码
HTML5非常强大,尤其是现在大部分浏览器都支持HTML5和CSS3,用HTML5制作的动画也多了起来.另外,Canvas上绘制图形非常简单,本文就分享了一些强大的HTML5 Cnavas动画,一起来 ...
- 《FLASH CC 2015 CANVAS 中文教程》——1、导出canvas动画,文件结构浅析
注::如果你对 FLASH 这个软件操作不够熟悉,建议你可以先看看FLASH动画之类的书. :FLASH CC 在文中直接简称为CC. :以下所以文章中所说的快捷键 如果你按了不起作用,请检查是否有其 ...
- Html5 Canvas动画旋转的小方块;
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...
- 【原创】测试不同浏览器播放canvas动画的平滑程度
Canvas无疑是HTML5开放式网络平台最激动人心的技术之一.目前,除了IE8以外,各类浏览器的新版本都支持HTML5 Canvas. 程序员需要通过Javascript调用Canvas API.基 ...
- HTML5 Canvas动画效果演示
HTML5 Canvas动画效果演示 主要思想: 首先要准备一张有连续帧的图片,然后利用HTML5 Canvas的draw方法在不同的时间 间隔绘制不同的帧,这样看起来就像动画在播放. 关键技术点: ...
- HTML5 Canvas动画效果演示 - 流浪的鱼 - 博客频道 - CSDN.NET
HTML5 Canvas动画效果演示 - 流浪的鱼 - 博客频道 - CSDN.NET HTML5 Canvas动画效果演示
随机推荐
- bootstrap实战经验
凡是基本的布局需要float实现的,都可以考虑利用网格布局. 1,.jumbotron可以形成一个青灰色的背景,并自动调节对应边距 2,.panel的应用十分广泛,可以自动设置合适的padding.甚 ...
- Git和SSH协议
SSH(安全外壳协议)为Secure Shell的缩写,由IETF的网络工作小组(Network Working Group)所制定:SSH为建立在应用层和传输层基础上的安全协议.SSH是目前较可靠, ...
- tooltip
/* 背景色 ; 字体颜色 ; 云,显示在上面 */ .tooltip-inner{ background-color: #FF0000; ForeColor:#0f0; IsBalloon:true ...
- WPF 基础面试题及答案(一)
一 · WPF由哪两部分组成? wpf 由两个主要部分 组成:引擎和编程框架. 1 引擎.wpf引擎是基于窗体的应用程序 图形 视频 音频和文档提供了一个单一的运行时库.重要的是WPF基于矢量的呈现引 ...
- SuSE Apache2 VirtualHost Build
1,linux version:openSuSE 12.1 2,add ServerName to DNS(johv.ts.com ,use the same IP) 3,mkdir /srv/www ...
- ajax异步提交文件
首先 下载jquery和jquery.form.js http://malsup.com/jquery/form/ <script type="text/javascript&qu ...
- Java基础之创建窗口——颜色和光标(TryWindow4)
控制台程序. java.awt包中把SystemColor类定义为Color类的子类.SystemColor类封装了本机操作系统用于显示各种组件的标准颜色.如果要比较SystemColor值和Colo ...
- Java FX中TreeView节点选中和双击事件监听
TreeItem<String> treeRoot = new TreeItem<String>("Root"); treeRoot.setExpanded ...
- spring纯java注解式开发(一)
习惯了用XML文件来配置spring,现在开始尝试使用纯java代码来配置spring. 其实,spring的纯java配置,简单来说就是将bean标签的内容通过注解转换成bean对象的过程,没什么神 ...
- How to create a project with existing folder of files in Visual Studio?
1. Select Visual Studio tool bar-> New -> Project from existing code-> continue with config ...