[js高手之路] html5 canvas动画教程 - 匀速运动
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了[js高手之路] html5 canvas动画教程 - 匀速运动,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3027字,纯文字阅读大概需要5分钟。
内容图文
匀速运动:指的是物体在一条直线上运动,并且物体在任何相等时间间隔内通过的位移都是相等的。其实就是匀速直线运动,它的特点是加速度为0,从定义可知,在任何相等的时间间隔内,速度大小和方向是相同的。
1 <head> 2 <meta charset=‘utf-8‘ /> 3 <style> 4 #canvas { 5 border: 1px dashed #aaa; 6 } 7 </style> 8 <script> 9 window.onload = function () { 10var oCanvas = document.querySelector("#canvas"), 11 oGc = oCanvas.getContext(‘2d‘), 12 width = oCanvas.width, height = oCanvas.height, 13 x = 0; 14function drawBall( x, y, cxt ){ 15 cxt.fillStyle = ‘#09f‘; 16 cxt.beginPath(); 17 cxt.arc( x, y, 20, 0, 2 * Math.PI ); 18 cxt.closePath(); 19 cxt.fill(); 20 } 21 ( function linear(){ 22 oGc.clearRect( 0, 0, width, height ); 23 drawBall( x, height / 2, oGc ); 24 x += 2; 25 console.log( x ); 26 requestAnimationFrame( linear ); 27 } )(); 28 } 29 </script> 30 </head> 31 <body> 32 <canvas id="canvas" width="1200" height="600"></canvas> 33 </body>
上述实例让一个半径20px的小球 从x=0, y=canvas高度的一半,以每帧2px的速度向右匀速运动.
我们可以把小球封装成一个对象:
ball.js文件:
1 function Ball( x, y, r, color ){ 2 this.x = x || 0; 3this.y = y || 0; 4this.radius = r || 20; 5this.color = color || ‘#09f‘; 6} 7 Ball.prototype = { 8 constructor : Ball, 9 stroke : function( cxt ){ 10 cxt.strokeStyle = this.color; 11 cxt.beginPath(); 12 cxt.arc( this.x, this.y, this.radius, 0, 2 * Math.PI ); 13 cxt.closePath(); 14 cxt.stroke(); 15 }, 16 fill : function( cxt ){ 17 cxt.fillStyle = this.color; 18 cxt.beginPath(); 19 cxt.arc( this.x, this.y, this.radius, 0, 2 * Math.PI ); 20 cxt.closePath(); 21 cxt.fill(); 22 } 23 }
该小球对象,可以定制位置半径和颜色,支持两种渲染方式(描边和填充)
1 <head> 2 <meta charset=‘utf-8‘ /> 3 <style> 4 #canvas { 5 border: 1px dashed #aaa; 6 } 7 </style> 8 <script src="./ball.js"></script> 9 <script> 10 window.onload = function () { 11var oCanvas = document.querySelector("#canvas"), 12 oGc = oCanvas.getContext(‘2d‘), 13 width = oCanvas.width, height = oCanvas.height, 14 ball = new Ball( 0, height / 2 ); 15 (function linear() { 16 oGc.clearRect(0, 0, width, height); 17 ball.fill( oGc ); 18 ball.x += 2; 19 requestAnimationFrame(linear); 20 })(); 21 } 22 </script> 23 </head> 2425 <body> 26 <canvas id="canvas" width="1200" height="600"></canvas> 27 </body>
斜线匀速运动:
1 <head> 2 <meta charset=‘utf-8‘ /> 3 <style> 4 #canvas { 5 border: 1px dashed #aaa; 6 } 7 </style> 8 <script src="./ball.js"></script> 9 <script> 10 window.onload = function () { 11var oCanvas = document.querySelector("#canvas"), 12 oGc = oCanvas.getContext(‘2d‘), 13 width = oCanvas.width, height = oCanvas.height, 14 ball = new Ball( 0, height ); 15 (function linear() { 16 oGc.clearRect(0, 0, width, height); 17 ball.fill( oGc ); 18 ball.x += 2; 19 ball.y -= 1; 20 requestAnimationFrame(linear); 21 })(); 22 } 23 </script> 24 </head> 2526 <body> 27 <canvas id="canvas" width="1200" height="600"></canvas> 28 </body>
原文:http://www.cnblogs.com/ghostwu/p/7638492.html
内容总结
以上是互联网集市为您收集整理的[js高手之路] html5 canvas动画教程 - 匀速运动全部内容,希望文章能够帮你解决[js高手之路] html5 canvas动画教程 - 匀速运动所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。