如何用html5 canvas实现匀速运动
canvas
h5
html5
匀速运动:指的是物体在一条直线上运动,并且物体在任何相等时间间隔内通过的位移都是相等的。其实就是匀速直线运动,它的特点是加速度为0,从定义可知,在任何相等的时间间隔内,速度大小和方向是相同的。
<head> <meta charset='utf-8' /> <style> #canvas { border: 1px dashed #aaa; } </style> <script> window.onload = function () { var oCanvas = document.querySelector("#canvas"), oGc = oCanvas.getContext('2d'), width = oCanvas.width, height = oCanvas.height, x = 0; function drawBall( x, y, cxt ){ cxt.fillStyle = '#09f'; cxt.beginPath(); cxt.arc( x, y, 20, 0, 2 * Math.PI ); cxt.closePath(); cxt.fill(); } ( function linear(){ oGc.clearRect( 0, 0, width, height ); drawBall( x, height / 2, oGc ); x += 2; console.log( x ); requestAnimationFrame( linear ); } )(); } </script> </head> <body> <canvas id="canvas" width="1200" height="600"></canvas> </body>
登录后复制
上述实例让一个半径20px的小球 从x=0, y=canvas高度的一半,以每帧2px的速度向右匀速运动.
我们可以把小球封装成一个对象:
ball.js文件:
function Ball( x, y, r, color ){ this.x = x || 0; this.y = y || 0; this.radius = r || 20; this.color = color || '#09f'; } Ball.prototype = { constructor : Ball, stroke : function( cxt ){ cxt.strokeStyle = this.color; cxt.beginPath(); cxt.arc( this.x, this.y, this.radius, 0, 2 * Math.PI ); cxt.closePath(); cxt.stroke(); }, fill : function( cxt ){ cxt.fillStyle = this.color; cxt.beginPath(); cxt.arc( this.x, this.y, this.radius, 0, 2 * Math.PI ); cxt.closePath(); cxt.fill(); } }
登录后复制
该小球对象,可以定制位置半径和颜色,支持两种渲染方式(描边和填充)
<head> <meta charset='utf-8' /> <style> #canvas { border: 1px dashed #aaa; } </style> <script src="./ball.js"></script> <script> window.onload = function () { var oCanvas = document.querySelector("#canvas"), oGc = oCanvas.getContext('2d'), width = oCanvas.width, height = oCanvas.height, ball = new Ball( 0, height / 2 ); (function linear() { oGc.clearRect(0, 0, width, height); ball.fill( oGc ); ball.x += 2; requestAnimationFrame(linear); })(); } </script> </head> <body> <canvas id="canvas" width="1200" height="600"></canvas> </body>
登录后复制
斜线匀速运动:
<head> <meta charset='utf-8' /> <style> #canvas { border: 1px dashed #aaa; } </style> <script src="./ball.js"></script> <script> window.onload = function () { var oCanvas = document.querySelector("#canvas"), oGc = oCanvas.getContext('2d'), width = oCanvas.width, height = oCanvas.height, ball = new Ball( 0, height ); (function linear() { oGc.clearRect(0, 0, width, height); ball.fill( oGc ); ball.x += 2; ball.y -= 1; requestAnimationFrame(linear); })(); } </script> </head> <body> <canvas id="canvas" width="1200" height="600"></canvas> </body>
登录后复制
任意方向的匀速运动(速度分解)
<head> <meta charset='utf-8' /> <style> #canvas { border: 1px dashed #aaa; } </style> <script src="./ball.js"></script> <script> window.onload = function () { var oCanvas = document.querySelector("#canvas"), oGc = oCanvas.getContext('2d'), width = oCanvas.width, height = oCanvas.height, ball = new Ball( 0, 0 ), speed = 5, vx = speed * Math.cos( 10 * Math.PI / 180 ), vy = speed * Math.sin( 10 * Math.PI / 180 ); (function linear() { oGc.clearRect(0, 0, width, height); ball.fill( oGc ); ball.x += vx; ball.y += vy; requestAnimationFrame(linear); })(); } </script> </head> <body> <canvas id="canvas" width="1200" height="600"></canvas> </body>
登录后复制
以上是如何用html5 canvas实现匀速运动的详细内容。更多信息请关注PHP中文网其他相关文章!
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章
R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前
By 尊渡假赌尊渡假赌尊渡假赌
刺客信条阴影:贝壳谜语解决方案
2 周前
By DDD
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前
By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
4 周前
By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)
