イージング 3D|Easing 3D|HTML5

イージング 3D|Easing 3D|HTML5

イージング 3D|Easing 3D|HTML5(HTML5, JavaScript, CSS3) : デモ

イージング 3D|Easing 3D|HTML5(HTML5, JavaScript, CSS3) : ZIPファイル(4kb)

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,
    ball,
    fl = 250,
    vpX = 0,
    vpY = 0,
    bounce = -0.7,
    top = -150,
    bottom = 150,
    left = -150,
    right = 150,
    front = 150,
    back = -150,
    easing = 0.1,
    tx = Math.random() * 500 - 250,
    ty = Math.random() * 500 - 250,
    tz = Math.random() * 500,
    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;
     
    ball = new Object();
    ball.radius = 40;
    ball.red = Math.floor(Math.random() * 256);
    ball.green = Math.floor(Math.random() * 256);
    ball.blue = Math.floor(Math.random() * 256);
    ball.xpos = 0;
    ball.ypos = 0;
    ball.zpos = 0;
    ball.vx = 0;
    ball.vy = 0;
    ball.vz = 0;
    ball.x = 0;
    ball.y = 0;
    ball.mass = 1;
    ball.visible = false;
    ball.scaleX = 1;
    ball.scaleY = 1;
     
    ctx = document.getElementById('canvas').getContext("2d");
     
    setAnimation();
     
    function setAnimation()
    {
        animation();
        requestAnimationFrame(setAnimation);
    }
     
    function animation()
    {
        ctx.clearRect(0, 0, 300, 300);
        var dx = tx - ball.xpos;
        var dy = ty - ball.ypos;
        var dz = tz - ball.zpos;
        ball.xpos += dx * easing;
        ball.ypos += dy * easing;
        ball.zpos += dz * easing;
         
        var dist = Math.sqrt(dx*dx + dy*dy + dz*dz);
             
        if(dist < 1)
        {
            tx = Math.random() * 500 - 250;
            ty = Math.random() * 500 - 250;
            tz = Math.random() * 500
        }
        move(ball);
    }
     
    function move(ball)
    {
        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();
        }
    }
}
 
// 各ブラウザ対応
window.requestAnimationFrame = (function()
{
    return window.requestAnimationFrame ||
    window.webkitRequestAnimationFrame ||
    window.mozRequestAnimationFrame ||
    window.oRequestAnimationFrame ||
    window.msRequestAnimationFrame ||
    function(callback)
    {
        window.setTimeout(callback, 1000/60);
    };
}());

コメントを残す

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