点此下载代码并用Chrome或是Firefox打开index.html

图例:

代码:

<!DOCTYPE html>
<html lang="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<head>
     <title>碰撞球 19.3.3 18:11 by:逆火 horn19782016@163.com</title>

     <style>
     #canvas{
        background:#ffffff;
        cursor:pointer;
        margin-left:10px;
        margin-top:10px;
        -webkit-box-shadow:3px 3px 6px rgba(0,0,0,0.5);
        -moz-box-shadow:3px 3px 6px rgba(0,0,0,0.5);
        box-shadow:3px 3px 6px rgba(0,0,0,0.5);
     }

     #controls{
        margin-top:10px;
        margin-left:15px;
     }
     </style>

    </head>

     <body onload="init()">
        <div id="controls">
            <input id='animateBtn' type='button' value='运动'/>
        </div>

        <canvas id="canvas" width="750px" height="500px" >
            出现文字表示你的浏览器不支持HTML5
        </canvas>
     </body>
</html>
<script type="text/javascript">
<!--
var paused=true;
animateBtn.onclick=function(e){
    paused=! paused;

    if(paused){
        animateBtn.value="运动";
    }else{

        animateBtn.value="暂停";
        window.requestAnimationFrame(animate);
    }
}

var ctx;// 绘图环境
var balls;// 球数组
function init(){

    var canvas=document.getElementById('canvas');
    canvas.width=750;
    canvas.height=500;
    ctx=canvas.getContext('2d');

    balls=[
        {
            x:150,
            y:250,
            vx:-7.2,
            vy:3.8,
            radius:25,
            innerColor:'rgba(255,25,0,1)',
            middleColor:'rgba(255,25,0,0.7)',
            outerColor:'rgba(255,25,0,0.5)',
            strokeStyle:'gray',
        },

        {
            x:650,
            y:50,
            vx:-8.2,
            vy:2.5,
            radius:25,
            innerColor:'rgba(113,232,227,1)',
            middleColor:'rgba(113,232,227,0.7)',
            outerColor:'rgba(113,232,227,0.5)',
            strokeStyle:'red',
        },

        {
            x:50,
            y:150,
            vx:12,
            vy:-14,
            radius:25,
            innerColor:'rgba(23,45,227,1)',
            middleColor:'rgba(23,45,227,0.7)',
            outerColor:'rgba(23,45,227,0.5)',
            strokeStyle:'blue',
        },

        {
            x:30,
            y:60,
            vx:8,
            vy:-24,
            radius:25,
            innerColor:'rgba(23,45,69,1)',
            middleColor:'rgba(23,45,69,0.7)',
            outerColor:'rgba(23,45,69,0.5)',
            strokeStyle:'yellow',
        },

        {
            x:300,
            y:300,
            vx:80,
            vy:-34,
            radius:25,
            innerColor:'rgba(123,45,69,1)',
            middleColor:'rgba(123,45,69,0.7)',
            outerColor:'rgba(123,45,69,0.5)',
            strokeStyle:'navy',
        },
   ];

};

function update(){
    for(var i=0;i<balls.length;i++){
        var ball=balls[i];

        // 与左右墙壁的碰撞检测
        if(ball.x+ball.vx+ball.radius>ctx.canvas.width || ball.x+ball.vx-ball.radius<0){
            ball.vx=-ball.vx;
        }

        // 与上下墙壁的碰撞检测
        if(ball.y+ball.vy+ball.radius>ctx.canvas.height || ball.y+ball.vy-ball.radius<0){
            ball.vy=-ball.vy;
        }

        // 小球之前的碰撞检测
        for(var j=0;j<balls.length;j++){
            if(i!=j){
                var other=balls[j];

                var distance=Math.sqrt( Math.pow((ball.x+ball.vx-other.x-other.vx),2)+Math.pow((ball.y+ball.vy-other.y-other.vy),2));

                if(distance<ball.radius+other.radius){
                    // 如果两个碰撞小球的质量相等,联立动量守恒和能量守恒方程时可解得:两个小球碰撞后交换速度。
                    var x=ball.vx;
                    ball.vx=other.vx;
                    other.vx=x;

                    var y=ball.vy;
                    ball.vy=other.vy;
                    other.vy=y;
                }
            }
        }

        ball.x+=ball.vx;
        ball.y+=ball.vy;
    }
}

function draw(){
    ctx.clearRect(0,0,ctx.canvas.width,ctx.canvas.height);

    for(var i=0;i<balls.length;i++){
        var ball=balls[i];

        gradient=ctx.createRadialGradient(ball.x,ball.y,0,ball.x,ball.y,ball.radius);
        gradient.addColorStop(0.3,ball.innerColor);
        gradient.addColorStop(0.5,ball.middleColor);
        gradient.addColorStop(1.0,ball.outerColor);

        ctx.save();
        ctx.beginPath();
        ctx.arc(ball.x,ball.y,ball.radius,0,Math.PI*2,false);
        ctx.fillStyle=gradient;
        ctx.strokeStyle=ball.strokeStyle;
        ctx.fill();
        ctx.stroke();
        ctx.restore();
    }
}

function animate(){
    if(!paused){

        update();
        draw();

        setTimeout( function(){
            window.requestAnimationFrame(animate); /// 让浏览器自行决定帧速率
        }, 0.10 * 1000 );// 延时执行
    }
}
//-->
</script>

2019年3月3日20点17分

[Canvas]碰撞球 增加小球间碰撞检测的更多相关文章

  1. [Canvas]碰撞球

    观赏动态效果请点此下载并用Chrome/Firefox打开index.html 图例: 代码: <!DOCTYPE html> <html lang="utf-8" ...

  2. 2017了,回家前 "年末" 分享:下雨,飘雪,红包雨,碰撞球,自定义View

    (本博客为原创:http://www.cnblogs.com/linguanh/) 目录: 效果展示 感想 代码拆解 开源地址 效果展示 有没有兴趣继续看下去,直接看下"颜值"是第 ...

  3. [原创]基于html5新标签canvas写的一个小画板

    最近刚学了canvas,写个小应用练习下 源代码 <!DOCTYPE> <html> <head> <meta http-equiv="Conten ...

  4. Html5 Canvas动画旋转的小方块;

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...

  5. canvas学习笔记、小函数整理

    http://bbs.csdn.net/topics/391493648 canvas实例分享 2016-3-16 http://bbs.csdn.net/topics/390582151 html5 ...

  6. 利用canvas写一个验证码小功能

    刚刚开始接触canvas,写个验证码小功能练练手,实现效果图如下: 主要代码如下: html <!DOCTYPE html> <html lang="en"> ...

  7. 第八讲:HTML5中canvas实现小球击打小方块游戏

    源码:http://download.csdn.net/detail/liumingm900913/7469969 游戏开发流程: 1.创建画布: 将画布放在div标签里面,这样能够控制画布居中的位置 ...

  8. canvas写的一个小时钟demo

    <!DOCTYPE html> <html> <head> <title>HTML5 Canvas Demo of clock</title> ...

  9. opencv(4)实现数据增加小工具

    数据增加(data augmentation),作为一种深度学习中的常用手段,数据增加对模型的泛化性和准确性都有帮助.数据增加的具体使用方式一般有两种,一种是实时增加,比如在Caffe中加入数据扰动层 ...

随机推荐

  1. 采用ASP.NET IIS 注册工具 (Aspnet_regiis.exe)对web.config实行本地加密

    加密原因:我们通常将一些重要的配置信息写在Web.config里面,其中数据库链接就是这样的信息.将这些数据直接明文显示,显然不太安全. 工具: 采用ASP.NET IIS 注册工具 (Aspnet_ ...

  2. Enable WiX project in Visual Studio 2013

    I have a VS 2012 solution with WiX Installer projects. However, when I open the solution in VS 2013 ...

  3. vs断点未能绑定

    原文链接:http://blog.csdn.net/pc0de/article/details/41790063 突然发现所有的c++项目在调试的时候加断点都会报错:”不能设置下面的断点.....断点 ...

  4. ASP.NET Web API实践系列03,路由模版, 路由惯例, 路由设置

    ASP.NET Web API的路由和ASP.NET MVC相似,也是把路由放在RouteTable中的.可以在App_Start文件夹中的WebApiConfig.cs中设置路由模版.默认的路由模版 ...

  5. 利用mvn deploy命令上传包(转)

    本文转自https://blog.csdn.net/chenaini119/article/details/52764543 mvn安装 下载maven的bin,在apache官方网站可以下载. ht ...

  6. JAVA BigDecimal的相加

    之前很少使用这样的一个对象BigDecimal,今天在改需求的时候遇到了,结果坑爹的怎么相加最后都为零. 代码如下: BigDecimal totalAmount = new BigDecimal(0 ...

  7. iOS中使用RegexKitLite来试用正则表达式

    转:http://blog.csdn.net/nullcn/article/details/6338592 准备工作,下载RegexKitLite 软件包,解压后有2个文件,需要加载到project中 ...

  8. 浅析iOS程序设计模式(基于MVC)

    接触iOS手机开发有一段时间了.总体来说,苹果公司设计的开发环境还是非常人性化的.很容易上手,也方便深入. 在组织大型项目的代码文件时,我们常用MVC的思想.MVC的概念讲起来非常简单,就和对象(ob ...

  9. C#输出到Release VS中Release模式下生成去掉生成pdb文件

    Release 与 Debug 的区别就不多说了, 简单来说 Release 优化过, 性能高一些. Debug 为方便调试. 默认情况下是 Debug, 那如何改成 Release 呢? 项目上右键 ...

  10. 浅析c++中virtual关键字

    http://blog.csdn.net/djh512/article/details/8973606 1.virtual关键字主要是什么作用? c++中的函数调用默认不适用动态绑定.要触发动态绑定, ...