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动画效果演示
随机推荐
- IE、chrome、火狐中如何调试javascript脚本
1. IE中点击"F12",在弹出页面中调试javascript脚本 2. chrome中点击"F12",在弹出页面中调试javascript脚本 3. 火 ...
- 电量显示Binding Converter MVVM
用一个ProcessBar显示电量,低于20%时候,ForeGround为红色,否则为绿色, 页面使用了MVVM绑定到了ViewModel, ProcessBar XAML为 <Progress ...
- 在Matlab中编译c/c++代码需要安装mex
>> mex -setup Welcome to mex -setup. This utility will help you set up a default compiler. For ...
- 解决: Fail to create empty document
做 Programming Windows with MFC 2nd 的例子 MyWord 的时候. 发现启动的时候总是报错: Fail to create empty document. 搜索了一下 ...
- Oracle Flashback Technologies - 闪回查询
Oracle Flashback Technologies - 闪回查询 查看表中,某行数据的修改记录 #创建一个表,并插入和修改数据 SQL> create table y3(id )); T ...
- 使用Genymotion来运行Android Studio开发的程序
访问网址:https://www.genymotion.com,注册账户,之后下载,下载时需要注意不要选择(withou virtualbox那个) 之后运行安装, 运行Genymotion运行启动G ...
- C++Primer 第九章
//1.vector:可变大小数组.支持快速随机访问,在尾部之外的位置插入或删除元素可能很慢.注意点:不要在vector中存放bool类型,vector<bool>并不是一个容器,其实现方 ...
- C++Primer 第二章
//1.程序尽量避免依赖于实现环境的行为.比如:如果将int的尺寸看成一个确定不变的已知值,那么这样的程序就称为不可移植的. typedef int int32; //使用类似的typedef,可以有 ...
- FlashBuilder 4.7 win64 和 mac版 下载地址
win64 http://trials3.adobe.com/AdobeProducts/FLBR/4_7/win64/FlashBuilder_4_7_LS10_win64.exe mac http ...
- git的基本使用
1.在本地新建一个文件夹来存放代码 2.用命令行进入这个文件夹 3.git init --来创建一个代码仓库 3. 配置用户信息:用户名和 邮箱(联系作者本人沟通, 责任到人) git config ...