プログラミングとイラストレーション » 複数のボールの跳ね返り3D|Ball Rebouding 3D|HTML5
プログラミングとイラストレーション > HTML5 > 複数のボールの跳ね返り3D|Ball Rebouding 3D|HTML5

複数のボールの跳ね返り3D|Ball Rebouding 3D|HTML5

複数のボールの跳ね返り3D|Ball Rebouding 3D|HTML5

複数のボールの跳ね返り3D|Ball Rebouding 3D|HTML5(HTML5, JavaScript, CSS3) : デモ

複数のボールの跳ね返り3D|Ball Rebouding 3D|HTML5(HTML5, JavaScript, CSS3) : ZIPファイル(3kb)

1.HTML

<!DOCTYPE html>
<html lang="jp">
    <head>
        <meta charset="UTF-8" />
        <title>複数の跳ね返り3D</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;
}
  
#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,
    offsetY = 0,
    ctx,
    balls = [],
    numBalls = 15,
    fl = 150,
    vpX = 0,
    vpY = 0,
    top = -150,
    bottom = 150,
    left = -150,
    right = 150,
    front = 150,
    back = -150,
    theContents;
     
    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', ''));
     
    vpX = offsetX / 2;
    vpY = offsetY / 2;
     
    ctx = document.getElementById('canvas').getContext("2d");
     
    for(var i = 0; i < numBalls; i++)
    {
        var ball = {};
        ball.radius = 15;
        ball.red = Math.floor(Math.random() * 256);
        ball.green = Math.floor(Math.random() * 256);
        ball.blue = Math.floor(Math.random() * 256);
        ball.vx = Math.random() * 10 - 5;
        ball.vy = Math.random() * 10 - 5;
        ball.vz = Math.random() * 10 - 5;
        ball.x = 0;
        ball.y = 0;
        ball.xpos = 0;
        ball.ypos = 0;
        ball.zpos = 0;
        ball.scaleX = 1;
        ball.scaleY = 1;
        ball.visible = false;
        balls[i] = ball;
    }
     
    setAnimation();
     
    function setAnimation()
    {
        animation();
        requestAnimationFrame(setAnimation);
    }
     
    function animation()
    {
        ctx.clearRect(0, 0, 300, 300);
        sortZ();
        for(var i = 0; i < numBalls; i++)
        {
            var ball = balls[i];
            move(ball);
        }
    }
     
    function move(ball)
    {
        var radius =  ball.radius;
         
        ball.xpos += ball.vx;
        ball.ypos += ball.vy;
        ball.zpos += ball.vz;
         
        if(ball.xpos + radius > right)
        {
            ball.xpos = right - radius;
            ball.vx *= -1;
        }
        else if(ball.xpos - radius < left)
        {
            ball.xpos = left + radius;
            ball.vx *= -1;
        }
        if(ball.ypos + radius > bottom)
        {
            ball.ypos = bottom - radius;
            ball.vy *= -1;
        }
        else if(ball.ypos - radius < top)
        {
            ball.ypos = top + radius;
            ball.vy *= -1;
        }
        if(ball.zpos + radius > front)
        {
            ball.zpos = front - radius;
            ball.vz *= -1;
        }
        else if(ball.zpos - radius < back)
        {
            ball.zpos = back + radius;
            ball.vz *= -1;
        }
         
        if(ball.zpos > -fl)
        {
            var scale = fl / (fl + ball.zpos);
            ball.scaleX = scale;
            ball.scaleY = scale;
            ball.x = vpX + ball.xpos * scale;
            ball.y = vpY + ball.ypos * scale;
            ball.visible = true;
        }
        else
        {
            ball.visible = false;
        }
         
        if(ball.visible)
        {
            ctx.strokeStyle ="rgb(" + ball.red + "," + ball.green + "," + ball.blue + ")";
            ctx.lineWidth = 1;
            ctx.beginPath();
            ctx.arc(ball.x, ball.y, ball.radius * ball.scaleX, 0, Math.PI * 2, true);
            ctx.fillStyle = "rgb(" + ball.red + "," + ball.green + "," + ball.blue + ")";
            ctx.fill();
            ctx.stroke();
        }
    }
     
    /*
     * 配列の並べ替え: 降順
     */
    function sortZ()
    {
        balls.sort( function( a, b ) { return b.zpos - a.zpos; } );
    }
}
 
// 各ブラウザ対応
window.requestAnimationFrame = (function()
{
    return window.requestAnimationFrame ||
    window.webkitRequestAnimationFrame ||
    window.mozRequestAnimationFrame ||
    window.oRequestAnimationFrame ||
    window.msRequestAnimationFrame ||
    function(callback)
    {
        window.setTimeout(callback, 1000/60);
    };
}());

コメントを残す

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

コメントフィード

トラックバック URL : http://www.htmlcode.jp/%e8%a4%87%e6%95%b0%e3%81%ae%e3%83%9c%e3%83%bc%e3%83%ab%e3%81%ae%e8%b7%b3%e3%81%ad%e8%bf%94%e3%82%8a3d%ef%bd%9cball-rebouding-3d%ef%bd%9chtml5/trackback/