canvas 模拟地球绕太阳转动 月球绕地球转动
2016-10-29 15:40
260 查看
requestAnimationFrame,Web中写动画的另一种选择
HTML5/CSS3时代,我们要在web里做动画选择其实已经很多了:你可以用CSS3的animattion+keyframes;
你也可以用css3的transition;
你还可以用通过在canvas上作图来实现动画,也可以借助jQuery动画相关的API方便地实现;
当然最原始的你还可以使用window.setTimout()或者window.setInterval()通过不断更新元素的状态位置等来实现动画,前提是画面的更新频率要达到每秒60次才能让肉眼看到流畅的动画效果。
现在又多了一种实现动画的方案,那就是还在草案当中的window.requestAnimationFrame()方法。
window.requestAnimationFrame方法
来看MDN上对其给出的诠释:window.requestAnimationFrame() 将告知浏览器你马上要开始动画效果了,后者需要在下次动画前调用相应方法来更新画面。这个方法就是传递给window.requestAnimationFrame()的回调函数。
也可这个方法原理其实也就跟setTimeout/setInterval差不多,通过递归调用同一方法来不断更新画面以达到动起来的效果,但它优于setTimeout/setInterval的地方在于它是由浏览器专门为动画提供的API,在运行时浏览器会自动优化方法的调用,并且如果页面不是激活状态下的话,动画会自动暂停,有效节省了CPU开销。
基本语法
可以直接调用,也可以通过window来调用,接收一个函数作为回调,返回一个ID值,通过把这个ID值传给window.cancelAnimationFrame()可以取消该次动画。window.requestAnimationFrame(move);//move为回调函数
arc()方法:
arc(x,y,radius,x1,y2,false) 六个参数 x,y 圆心的坐标 , radius 圆的半径,x1为其实弧度,y1为终止弧度,如果画圆 x1 = 0,y1 = 2*Math.PI, 最后一个参数画圆的方向,有两个值 false (顺时针)和 true (逆时针)。clearRect() 方法:
clearRect(x,y,w,h) 四个参数 x,y 为要清除位置的起始坐标, w,h 为要清除的宽高开始画圆
画太阳,地球,月球的过程都是用 canvas 画圆,因为它们除了 圆心坐标和半径不一样以为 ,其他过程都是一样的,所以我将画圆的方法封装起来,在画的时候给不同的参数来调用这个类封装类
function Point(x,y){ this.x = x; //圆心的 X 坐标 this.y = y; //圆心的 Y 坐标 } function Circle(origin,radius,speed){ this.origin = origin; //圆心 this.radius = radius; //半径 this.speed = speed; //转动速度 this.draw = drawCircle; //画圆的方法 } function drawCircle(){ con.beginPath(); //开始绘制 con.arc(this.origin.x,this.origin.y,this.radius,0,2*Math.PI,false); //绘制圆 con.stroke(); //绘制的框 con.closePath(); //绘制结束 }
画太阳,地球,月亮
这里只需要调用前面封装好的类,传入参数即可//画太阳 var sun = new Circle(new Point(400,300),30,0); sun.draw(); //画地球轨迹 var earthPath = new Circle(sun.origin,200,0); earthPath.draw(); //画地球 var earth = new Circle(new Point(earthPath.origin.x + earthPath.radius, earthPath.origin.y),20,0); earth.draw(); //画月亮轨迹 var moonPath = new Circle(earth.origin,80,0); moonPath.draw(); //画月亮 var moon = new Circle(new Point(moonPath.origin.x + moonPath.radius, moonPath.origin.y),10,0); moon.draw();
让地球和月亮转动起来
function move(){ con.clearRect(0,0,canvas.width,canvas.height); sun.draw(); earthPath.draw(); moonPath.draw(); earth.draw(); moon.draw(); earth.origin.x = earthPath.origin.x + earthPath.radius * Math.cos(earth.speed); earth.origin.y = earthPath.origin.y + earthPath.radius * Math.sin(earth.speed); earth.speed += 0.02; moon.origin.x = moonPath.origin.x + moonPath.radius * Math.cos(moon.speed); moon.origin.y = moonPath.origin.y + moonPath.radius * Math.sin(moon.speed); moon.speed += -0.05; window.requestAnimationFrame(move); } move();
效果图:
画面虽然简单了点 ,但转动的效果还是可以看见的完整代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
canvas {border: 1px solid black;}
</style>
</head>
<body>
<canvas id="myCanvas" width="800" height="600"></canvas>
</body>
<script type="text/javascript">
var canvas = document.getElementById('myCanvas');
var con = canvas.getContext('2d');
function Point(x,y){ this.x = x; //圆心的 X 坐标 this.y = y; //圆心的 Y 坐标 } function Circle(origin,radius,speed){ this.origin = origin; //圆心 this.radius = radius; //半径 this.speed = speed; //转动速度 this.draw = drawCircle; //画圆的方法 } function drawCircle(){ con.beginPath(); //开始绘制 con.arc(this.origin.x,this.origin.y,this.radius,0,2*Math.PI,false); //绘制圆 con.stroke(); //绘制的框 con.closePath(); //绘制结束 }//画太阳
var sun = new Circle(new Point(400,300),30,0);
sun.draw();
//画地球轨迹0
var earthPath = new Circle(sun.origin,200,0);
earthPath.draw();
//画地球
var earth = new Circle(new Point(earthPath.origin.x + earthPath.radius, earthPath.origin.y),20,0);
earth.draw();
//画月亮轨迹
var moonPath = new Circle(earth.origin,80,0);
moonPath.draw();
//画月亮
var moon = new Circle(new Point(moonPath.origin.x + moonPath.radius, moonPath.origin.y),10,0);
moon.draw();
function move(){ con.clearRect(0,0,canvas.width,canvas.height); sun.draw(); earthPath.draw(); moonPath.draw(); earth.draw(); moon.draw(); earth.origin.x = earthPath.origin.x + earthPath.radius * Math.cos(earth.speed); earth.origin.y = earthPath.origin.y + earthPath.radius * Math.sin(earth.speed); earth.speed += 0.02; moon.origin.x = moonPath.origin.x + moonPath.radius * Math.cos(moon.speed); moon.origin.y = moonPath.origin.y + moonPath.radius * Math.sin(moon.speed); moon.speed += -0.05; window.requestAnimationFrame(move); } move();
</script>
</html>
相关文章推荐
- 玩转html5(五)---月球绕着地球转,地球绕着太阳转(canvas实现,同样可以动哦)
- OpenGL模拟太阳、地球、月球公转自转
- [HTML5-SVG]使用svg、CSS3、raphaeljs:模拟月球绕地球,地球绕太阳旋转
- 玩转html5(五)---月球绕着地球转,地球绕着太阳转(canvas实现,同样可以动哦)
- 玩转html5(五)---月球绕着地球转,地球绕着太阳转
- openGL+VS2010的例程--太阳地球月球运动模型增强版(三维)
- WPF太阳、地球、月球运动轨迹模拟
- 玩转html5(五)---月球绕着地球转,地球绕着太阳转(canvas实现,同样可以动哦)
- Canvas模拟太阳地球月球的运动过程
- 开玩笑html5(五岁以下儿童)---绕地球月球,地球绕太阳运动(canvas实现,同样可以移动哦)
- OpenGL:太阳、地球、月球模型模拟动画
- VS 平台下 OpenGL 实现地球、月球、太阳运动
- [OpenGL]使用简单的视角变化实现太阳和地球的转动
- openGL+VS2010的例程--太阳地球月球运动模型(三维)
- 想看到太阳照在地球的那个角落了吗?
- 《佛密诸事》第二十二章:太阳与地球智慧生命
- NASA:太阳2013将强烈磁暴 如百枚氢弹袭击地球
- osg,纹理贴图,地球、月球、火星
- SVG 模拟太阳 地球 月亮旋转
- 太阳、地球、月亮