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动画效果演示
随机推荐
- javascript学习之位置获取
一.获取浏览器的大小和位置 具体可以参见博客:http://www.cnblogs.com/bobodeboke/p/4653920.html 二.获取元素的大小和位置 方法一.利用offsetXXX ...
- 解决Regsvr32: DllRegisterServer entry point was not found
原因: 虽然项目里面包含了DEF文件, 但是尼玛没有配置项目属性让链接器处理它啊! 解决方案: 项目属性->链接器->输入->模块定义文件->{输入文件名}
- HTML是什么
HTML(Hyper Text Mark-up Language )即超文本标记语言,是 WWW 的描述语言,由 Tim Berners-lee提出.设计 HTML 语言的目的是为了能把存放在一台电脑 ...
- 错过C++
曾相识的C++--2008年那是还在学校,接触到了这门语言,可遗憾的是当时,自己没有能静下心好好学习这门语言.所以相识了了半年的c++就这样不见. 如今又相逢,但已经感觉到很陌生,陌生的我们互补相认.
- 在windows下配置pthread
http://blog.csdn.net/qianchenglenger/article/details/16907821 简单介绍windows平台下的pthread线程库
- Sikuli增强包
一.前提准备1) VCForPython27.msi2) Cython安装包3) pyjnius安装包4) sikuli_cpython安装包5) SLF4J文件包二.安装过程1) VCF ...
- CCF真题之字符串匹配
201409-3 问题描述 给出一个字符串和多行文字,在这些文字中找到字符串出现的那些行.你的程序还需支持大小写敏感选项:当选项打开时,表示同一个字母的大写和小写看作不同的字符:当选项关闭时,表示同一 ...
- windows系统调用 进程终止
#include "windows.h" #include "iostream" #include "stdio.h" using name ...
- 简单常用的sql,统计语句,陆续整理添加吧
1. 分段统计分数 if object_id('[score]') is not null drop table [score] go create table [score]([学号] i ...
- 关于 屏幕阅读器 和 sr-only
.sr-only = screen reader only 用于读屏器识别使用.