| 注册
请输入搜索内容

热门搜索

Java Linux MySQL PHP JavaScript Hibernate jQuery Nginx
jopen
11年前发布

用html5中canvas实现的弹跳小球

<!DOCTYPE>    <html>    <head>        <title>canvas 测试案例二:弹球游戏</title>        <style>        </style>    </head>    <body>        <canvas id="canvas" class="canvas" width='400px' height='300px'>Your browser does not support the HTML5 element canvas</canvas>        <script>            var boxX = 20,                boxY = 30, //盒子的左上角 x y 的初始位置                boxWidth = 350,                boxHeight = 250, //盒子的宽高                ballRad = 10, // 球的半径                boxRoundX = boxWidth  boxX - ballRad, //右边界                boxRoundY= boxHeight  boxY - ballRad, //下边界                inboxRoundX = boxX  ballRad, //左边界                inboxRoundY= boxY  ballRad, // 上边界                ballX = 50,                ballY = 60, //球的初始位置                 ballVX = 4, //初始水平位移                ballVY= 8; //初始垂直位移            function init () {                var ctx = document.getElementById('canvas').getContext('2d');                ctx.linewidth = ballRad;                ctx.fillStyle = 'rgb(200, 0, 50)';                moveBall();                setInterval(moveBall, 25);                    // 移动球的函数                function moveBall () {                    ctx.clearRect(boxX, boxY, boxWidth, boxHeight);                    changDirect();                    ctx.beginPath();                    ctx.arc(ballX, ballY, ballRad, 0, Math.PI * 2, false);                    ctx.fill();                    ctx.strokeRect(boxX, boxY, boxWidth, boxHeight);                }                    // 判断距离方向函数                function changDirect () {                    var nextX = ballX  ballVX,                        nextY = ballY  ballVY;                    if (nextX > boxRoundX) {                        ballVX = -ballVX;                        nextX = boxRoundX;                    }                    if (nextX < inboxRoundX) {                        ballVX = -ballVX;                        nextX = inboxRoundX;                    }                    if (nextY > boxRoundY) {                        ballVY = -ballVY;                        nextY = boxRoundY;                    }                    if (nextY < inboxRoundY) {                        ballVY = -ballVY;                        nextY = inboxRoundY;                    }                    ballX = nextX;                    ballY = nextY;                }            }                init();        </script>    </body>    </html>
来自:https://github.com/chenkehxx/practice/commit/65067f7bb7948abe94b052f3e69fc2b1892ab167

 本文由用户 jopen 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
 转载本站原创文章,请注明出处,并保留原始链接、图片水印。
 本站是一个以用户分享为主的开源技术平台,欢迎各类分享!
 本文地址:https://www.open-open.com/lib/view/open1389335053320.html
HTML5 前端技术