衝突によるバネ運動|HTML5

衝突によるバネ運動|HTML5

衝突によるバネ運動|HTML5|HTML5(HTML5, JavaScript, CSS3) : デモ

衝突によるバネ運動|HTML5|HTML5(HTML5, JavaScript, CSS3) : ZIPファイル(3kb)

1.HTML

<!DOCTYPE html>
<html lang="jp">
    <head>
        <meta charset="UTF-8" />
        <title>衝突によるバネ運動</title>
        <link href="css/base.css" rel="stylesheet" type="text/css">
        <script src="js/base.js"></script>
    </head>
    <body>
        <div id="contents">
            <canvas id="canvas" width="275" height="275"></canvas>
        </div>
    </body>
</html>

2.CSS

@charset "utf-8";
 
body
{
    margin:0;
    padding:0;
    background-color: #fff;
    font-family:Helvetica, HiraKakuProN-W3, sans-serif; 
    font-size:12px;
    color:#000;
}
 
#contents
{
    position: absolute;
    top: 0;
    left: 0;
    width: 275px;
    height:275px;
    border: 1px solid #000;
    overflow:hidden;
}
 
#canvas
{
    position: absolute;
    top: 0;
    left: 0;
    width: 275px;
    height:275px;
}

3.JavaScript

base.js

window.addEventListener("load", init, false);
 
function init()
{
    var offsetX = 0;
    var offsetY = 0;
    var ctx = null;
    var spring = 0.2;
    var bounce = -1;
    var theCanvas = null;
     
    var balls = new Array();
    var numBalls = 10;
     
    var centerBall = new Object();
    centerBall.f=false;
    centerBall.x = 0;
    centerBall.y = 0;
    centerBall.vx = 0;
    centerBall.vy = 0;
    centerBall.r = 50;
    centerBall.d = 0;
     
    var theContents = document.getElementById("contents");
    offsetX = (theContents.currentStyle || document.defaultView.getComputedStyle(theContents,'')).width;
    offsetX = Number(offsetX.replace('px',''));
 
    offsetY = (theContents.currentStyle || document.defaultView.getComputedStyle(theContents,'')).height;
    offsetY = Number(offsetY.replace('px',''));
     
    centerBall.x = offsetX/2;
    centerBall.y = offsetY/2;
     
    for(var i = 0; i < numBalls; i++)
    {
         
        var ball = new Object();
        ball.red = Math.floor(Math.random()*256);
        ball.green = Math.floor(Math.random()*256);
        ball.blue = Math.floor(Math.random()*256);
        ball.f = false;
        ball.x = Math.random() * offsetX;
        ball.y = Math.random() * offsetY;
        ball.vx = Math.random() * 6-3;
        ball.vy = Math.random() * 6-3;
        ball.r = 10;
        ball.d = 0;
        balls.push(ball);
    }
    
    setBallAnimation();
     
    function setBallAnimation()
    {
        ctx = document.getElementById('canvas').getContext("2d");
        setInterval(animation, 1000/60);
    }
     
    function animation()
    {
        ctx.clearRect(0,0,300,300);
         
         
        for(var i = 0; i<numBalls; i++)
        {
            var ball = balls[i];
            ctx.strokeStyle ="rgb("+ball.red+","+ball.green+","+ball.blue+")";
            ctx.lineWidth = 1;
            ctx.beginPath();
            ctx.arc(ball.x,ball.y, ball.r,0,Math.PI*2,true);
            ctx.fillStyle="rgb("+ball.red+","+ball.green+","+ball.blue+")";
            ctx.fill();
            ctx.stroke();
             
            move(ball);
            var dx = ball.x - centerBall.x;
            var dy = ball.y - centerBall.y;
            var dist = Math.sqrt(dx*dx+dy*dy);
            var minDist = ball.r+centerBall.r;
            if(dist<minDist)
            {
                var angle = Math.atan2(dy,dx);
                var tx = centerBall.x + Math.cos(angle)*minDist;
                var ty = centerBall.y + Math.sin(angle)*minDist;
                ball.vx += (tx - ball.x) * spring;
                ball.vy += (ty - ball.y) * spring;
            }
        }
         
        ctx.strokeStyle = "#cccccc";
        ctx.lineWidth = 1;
        ctx.beginPath();
        ctx.arc(centerBall.x, centerBall.y, centerBall.r,0,Math.PI*2,true);
        ctx.fillStyle="#cccccc";
        ctx.fill();
        ctx.stroke();
    }
     
    function move(ball)
    {
        ball.x += ball.vx;
        ball.y += ball.vy;
        if(ball.x + ball.r > offsetX)
        {
            ball.x = offsetX - ball.r;
            ball.vx *= bounce;
        }
        else
        if(ball.x - ball.r < 0)
        {
            ball.x = ball.r;
            ball.vx *= bounce;
        }
         
        if(ball.y + ball.r > offsetY)
        {
            ball.y = offsetY - ball.r;
            ball.vy *= bounce;
        }
        else
        if(ball.y - ball.r < 0)
        {
            ball.y = ball.r;
            ball.vy *= bounce;
        }
    }
}

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です