線 3D|Lines 3D|HTML5

線 3D|Lines 3D|HTML5

線 3D|Lines 3D|HTML5|HTML5(HTML5, JavaScript, CSS3) : デモ

線 3D|Lines 3D|HTML5(HTML5, JavaScript, CSS3) : ZIPファイル(4kb)

画面をタッチムーブすると、回転速度が変化します。

1.HTML

<!DOCTYPE html>
<html lang="jp">
    <head>
        <meta charset="UTF-8" />
        <title>Lines3D1</title>
        <link href="css/base.css" rel="stylesheet" type="text/css">
        <script src="js/devicecheck.js"></script>
        <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 = 30,
    fl = 250,
    vpX = 0,
    vpY = 0,
    mouseX = 0,
    mouseY = 0,
    buttonDown,
    buttonMove,
    buttonUp,
    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");
      
    //ボタンタイプ
    if(g_mobiledevice === true)
    {
        buttonDown = 'touchstart';
        buttonMove = 'touchmove';
        buttonUp = 'touchend';
    }
    else
    {
        buttonDown = 'mousedown';
        buttonMove = 'mousemove';
        buttonUp = 'mouseup';
    }
      
    document.getElementById('canvas').addEventListener('touchstart', function() 
    {
        event.preventDefault();
    });
    document.getElementById('canvas').addEventListener('touchmove', function() 
    {
        event.preventDefault();
    });
    document.getElementById('canvas').addEventListener('touchend', function() 
    {
        event.preventDefault();
    });
      
    document.getElementById('canvas').addEventListener(buttonMove, mouseMoveHandler, false);
      
    initBall();
      
    function initBall()
    {
        for(var i = 0; i < numBalls; i++)
        {
            var ball = {};
            ball.radius = 5;
            /*ball.red = Math.floor(Math.random() * 256);
            ball.green = Math.floor(Math.random() * 256);
            ball.blue = Math.floor(Math.random() * 256);*/
            ball.red = 0;
            ball.green = 0;
            ball.blue = 0;
            ball.vx = 0;
            ball.vy = 0;
            ball.vz = 0;
            ball.x = 0;
            ball.y = 0;
            ball.xpos = Math.random() * offsetX - offsetX/2;
            ball.ypos = Math.random() * offsetY - offsetY/2;
            ball.zpos = Math.random() * offsetX - offsetX/2;
            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);
         
        var angleX = (mouseY - vpY) * 0.001;
        var angleY = (mouseX - vpX) * 0.001;
        for(var i = 0; i < numBalls; i++)
        {
            var ball = balls[i];
            rotateX(ball, angleX);
            rotateY(ball, angleY);
            doPerspective(ball);
        }
        drawLines();
    }
     
    function drawLines()
    {
        ctx.strokeStyle = "#000000";
        ctx.lineWidth = 1;
        ctx.beginPath();
        ctx.moveTo(balls[0].x, balls[0].y);
         
        for(var i = 1; i < balls.length; i++)
        {
            ctx.lineTo(balls[i].x, balls[i].y);
        }
         
        ctx.stroke();
    }
      
    function rotateX(ball, angleX)
    {
        var cosX = Math.cos(angleX);
        var sinX = Math.sin(angleX);
          
        var y1 = ball.ypos * cosX - ball.zpos * sinX;
        var z1 = ball.zpos * cosX + ball.ypos * sinX;
          
        ball.ypos = y1;
        ball.zpos = z1;
    }
      
    function rotateY(ball, angleY)
    {
        var cosY = Math.cos(angleY);
        var sinY = Math.sin(angleY);
          
        var x1 = ball.xpos * cosY - ball.zpos * sinY;
        var z1 = ball.zpos * cosY + ball.xpos * sinY;
          
        ball.xpos = x1;
        ball.zpos = z1;
    }
      
    function doPerspective(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();
        }
    }
      
    function mouseMoveHandler(e)
    {
        if(g_mobiledevice)
        {
            var rect = event.currentTarget.getBoundingClientRect();
            mouseX = event.touches[0].pageX - rect.left;
            mouseY = event.touches[0].pageY - rect.top;
        }
        else
        if(g_browsername == 'MSIE'||g_browsername == 'Opera')
        {
            rect = document.getElementById("canvas").getBoundingClientRect();
            mouseX = event.clientX - rect.left;
            mouseY = event.clientY - rect.top;
        }
        else
        if(g_browsername == 'Firefox')
        {
            rect = document.getElementById("canvas").getBoundingClientRect();
            mouseX = e.clientX - rect.left;
            mouseY = e.clientY - rect.top;
        }
        else
        {
            rect = event.currentTarget.getBoundingClientRect();
            mouseX = event.clientX - rect.left;
            mouseY = event.clientY - rect.top;
        }
    }
}
  
// 各ブラウザ対応
window.requestAnimationFrame = (function()
{
    return window.requestAnimationFrame ||
    window.webkitRequestAnimationFrame ||
    window.mozRequestAnimationFrame ||
    window.oRequestAnimationFrame ||
    window.msRequestAnimationFrame ||
    function(callback)
    {
        window.setTimeout(callback, 1000/60);
    };
}());

devicecheck.js

var g_mobiledevice = false; //iPhone、iPad、Androidのときtrue
var g_browsername = 'unknown';
var g_browserver = -1;
 
if(checkUserAgent() == false)
{
    window.alert('このブラウザは対象外です');
}
 
function checkUserAgent()
{
    var ua = navigator.userAgent;
 
    //iPhoneか?  
    var mstr = ua.match(/iPhone OS \d+/);
    if(mstr != null){
        var vstr = mstr[0].match(/\d+/);
        if(parseInt(vstr[0]) >= 3) 
        {
            g_mobiledevice = true;
            g_browsername = 'iPhone';
            //alert('major-version ' + vstr);
            return true;
        }
    }
    //iPadか?
    if(ua.indexOf('iPad') > -1)
    {
        mstr = ua.match(/CPU OS \d+/);
        if(mstr != null)
        {
            var vstr = mstr[0].match(/\d+/);
            if(parseInt(vstr[0]) >= 3) {
                g_mobiledevice = true;
                g_browsername = 'iPad';
                //alert('major-version ' + vstr);
                return true;
            }       
        }       
    }   
    //Androidか? 
    var mstr = ua.match(/Android \d+\.\d+/);
    if(mstr != null)
    {
        g_browsername = 'Android';
        var vstr = mstr[0].match(/\d+\.\d+/);
        g_browserver = parseFloat(vstr[0]);
        g_mobiledevice = true;
        if(pg_browserver > 2.1) 
        {
            //alert('version ' + vstr);
            return true;
        }
    }
    //Chromeか?
    mstr = ua.match(/Chrome\/\d+/);
    if(mstr != null)
    {
        g_browsername = 'Chrome';
        var vstr = mstr[0].match(/\d+/);
        g_browserver = parseInt(vstr[0]);
        if(g_browserver >= 9) 
        {
            //alert('major-version ' + vstr);
             
            return true;
        }       
    }
    //Safariか?
    if(ua.indexOf('Safari') > -1)
    {
        mstr = ua.match(/Version\/\d+/);
        if(mstr != null){
            var vstr = mstr[0].match(/\d+/);
            if(parseInt(vstr[0]) >= 5) 
            {
                g_browsername = 'Safari';
                //alert('major-version ' + vstr);
                return true;
            }       
        }       
    }
    //Internet Explorerか?
    mstr = ua.match(/MSIE \d+/);
    if(mstr != null)
    {
        var vstr = mstr[0].match(/\d+/);
        if(parseInt(vstr[0]) >= 9) 
        {
            g_browsername = 'MSIE';
            //alert('major-version ' + vstr);
            return true;
        }       
    }
    //Firefoxか?
    mstr = ua.match(/Firefox\/\d+/);
    if(mstr != null){
        var vstr = mstr[0].match(/\d+/);
        if(parseInt(vstr[0]) >= 4) 
        {
            g_browsername = 'Firefox';
            //alert('major-version ' + vstr);
            return true;
        }       
    }
    //Operaか?
    if(ua.indexOf('Opera') > -1)
    {
        mstr = ua.match(/Version\/\d+/);
        if(mstr != null){
            var vstr = mstr[0].match(/\d+/);
            if(parseInt(vstr[0]) >= 11) 
            {
                g_browsername = 'Opera';
                //alert('major-version ' + vstr);
                return true;
            }       
        }       
    }
     
    return false;
}

コメントを残す

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