回転行列によるオブジェクトの回転|HTML5

回転行列によるオブジェクトの回転|HTML5

回転行列によるオブジェクトの回転|HTML5(HTML5, JavaScript, CSS3) : デモ

回転行列によるオブジェクトの回転|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;
}
  
#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,
    theCanvas,
    theContents,
    balls = [],
    numBalls = 20;
      
    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',''));
      
    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.x = Math.random() * offsetX;
        ball.y = Math.random() * offsetY;
        ball.r = 10;
        balls.push(ball);
    }
     
    ctx = document.getElementById('canvas').getContext("2d");
    setBallAnimation();
     
    function setBallAnimation()
    {
        animation();
        requestAnimationFrame(setBallAnimation);
    }
      
    function animation()
    {
        var angle = 0.02;
        var cos = Math.cos(angle);
        var sin = Math.sin(angle);
        ctx.clearRect(0, 0, 300, 300);
         
        for(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();
            var x1 = ball.x - offsetX / 2;
            var y1 = ball.y - offsetY / 2;
            var x2 = cos * x1 - sin * y1;
            var y2 = cos * y1 + sin * x1;
            ball.x = offsetX / 2 + x2;
            ball.y = offsetY / 2 + y2;
        }
    }
}
 
// 各ブラウザ対応
window.requestAnimationFrame = (function()
{
    return window.requestAnimationFrame ||
    window.webkitRequestAnimationFrame ||
    window.mozRequestAnimationFrame ||
    window.oRequestAnimationFrame ||
    window.msRequestAnimationFrame ||
    function(callback)
    {
        window.setTimeout(callback, 1000/60);
    };
}());

コメントを残す

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