H5 canvas 小demo之小球的随机运动
2024-10-12 06:43:08
1:结构之html----balls.html
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<link href="style.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="main.js"></script>
</head>
<body>
<div id="container">
<canvas id="canvas"></canvas>
<div id="controller">
<h2>canvas控件</h2>
<a href="javascript:void(0);" id="motion"></a>
<a href="javascript:void(0);" id="white">white</a>
<a href="javascript:void(0);" id="black">black</a>
</div> </div> </body>
</html>
2:样式之css-----style.css
#container{
width:800px;
height:600px;
margin: 10px auto;
position: relative;
}
#canvas{
display: block;
border: 1px solid #808080;
margin:10px auto;
}
h2{
text-align: center;
}
#controller{
border-radius: 20px;
border: 1px solid grey;
width: 200px;
height:100px;
position: absolute;
top:10px;
left:10px;
}
#controller a{
background: #808080;
color:#ffffff;
text-decoration: none;
line-height: 35px;
text-align: center;
position: absolute;
display: block;
width:50px;
height:35px;
border:1px solid #808080;
border-radius: 20px;
}
#motion{
top:55px;
left:10px;
}
#white{
top:55px;
left:70px;
}
#black{
top:55px;
left:130px;
}
3:行为之javascript----main.js
/**
* Created by jackpan on 2015/4/18.
*/
var canvas;
var context;
var width;
var height;
var balls=[];
var isMove=true;
var motion;
var white;
var black;
var themeColor;
window.onload= function () {
canvas=document.getElementById("canvas");
motion=document.getElementById("motion");
white=document.getElementById("white");
black=document.getElementById("black");
motion.innerHTML="运动";
context=canvas.getContext("2d");
canvas.width=800;
canvas.height=600;
width=canvas.width;
height=canvas.height;
context.globalAlpha=0.7;
for(var i=0;i<;i++){
var R=Math.floor(Math.random()*255);
var G=Math.floor(Math.random()*255);
var B=Math.floor(Math.random()*255);
var radius=Math.random()*40+10;
var ball={
x:Math.random()*(width-2*radius)+radius,
y:Math.random()*(height-2*radius)+radius,
vx:Math.pow(-1,Math.ceil(Math.random()*2))*Math.random()*8+2,
vy:Math.pow(-1,Math.ceil(Math.random()*2))*Math.random()*4+2,
radius:radius,
color:"rgb("+R+","+G+","+B+")"
}
balls[i]=ball;
}
motion.onclick= function () {
if(isMove){
isMove=false;
motion.innerText="静止";
}else{
isMove=true;
motion.innerHTML="运动";
}
}
white.onclick= function () {
themeColor="white";
}
black.onclick= function () {
themeColor="black";
}
setInterval(
function () {
drawBall();
if(isMove){
updateBall();
}
},40
)
}
function drawBall(){
context.clearRect(0,0,width,height);
if(themeColor=="black"){
context.fillStyle=themeColor;
context.fillRect(0,0,width,height);
}
for(var i=0;i<balls.length;i++){
context.globalCompositeOperation="lighter";
context.beginPath();
context.arc(balls[i].x,balls[i].y,balls[i].radius,0,Math.PI*2,true);
context.closePath();
context.fillStyle=balls[i].color;
context.fill();
}
}
function updateBall(){
for(var i=0;i<balls.length;i++){
var aBall=balls[i];
aBall.x+=aBall.vx;
aBall.y+=aBall.vy;
if(aBall.x<aBall.radius || aBall.x>width-aBall.radius){
aBall.vx=-aBall.vx;
}
if(aBall.y<aBall.radius || aBall.y>height-aBall.radius){
aBall.vy=-aBall.vy;
} }
}
4:效果静态图展示
最新文章
- [NHibernate]O/R Mapping基础
- iOS圆形图片裁剪,以及原型图片外面加一个圆环
- WCF 已超过传入消息(65536)的最大消息大小配额。若要增加配额,请使用相应绑定元素上的 MaxReceivedMessageSize 属性
- hdu2023 求平均成绩 ~~很闲~~~
- tomcat 解析(五)-Tomcat的核心组成和启动过程
- SQL日期(转)
- CSS实现三角形效果
- API 设计: RAML、Swagger、Blueprint三者的比较
- Servlet 获取IllegelStateException
- loadrunner11的移动端性能测试之脚本录制
- Activity绑定自定义视图
- Java IO学习--(一)概述
- 数组中的第K个最大元素leetcode(Top K的问题)
- Linux grep 命令详解
- URL.createObjectURL() 实现本地上传图片 并预览功能
- 17.结构体(typedef)
- html:布局class名和其他class名个人整理总结
- Linux CPU占用率监控工具小结
- php中静态方法的使用
- BIOS、EFI与UEFI详解
热门文章
- vijosP1437简单的口令
- Ubuntu Builder —— 一个制作自己的发行版的工具
- linux下的X server:linux图形界面原理
- 【转】Windows7打造全方位护眼系统
- SharePoint 2013版本功能对比介绍
- vs212创建mvc3项目,添加ADO.NET实体数据模型时产生 XXXX.Desiger.cs 文件为空
- PV,UV,IP
- 转自微软内部资料:编写高性能 Web 应用程序的 10 个技巧
- 【JS】Beginner2:Variables &; Data
- nyoj 96 一个水题目