正方形 3D|Square 3D|HTML5

正方形 3D|Square 3D|HTML5

正方形 3D|Square 3D|HTML5(HTML5, JavaScript, CSS3) : デモ

正方形 3D|Square 3D|HTML5(HTML5, JavaScript, CSS3) : ZIPファイル(4kb)

1.HTML

<!DOCTYPE html>
<html lang="jp">
    <head>
        <meta charset="UTF-8" />
        <title>3d Panel</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,
    points = [],
    triangles = [],
    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);
     
    /*
     * x, y, z, vpX, vpY, cX, cY, cZ, index
     */
    //4隅
    point3D( -150, -150, 0, vpX, vpY, 0, 0, 200, 0);
    point3D( 150, -150, 0, vpX, vpY, 0, 0, 200, 1);
    point3D( 150, 150, 0, vpX, vpY, 0, 0, 200, 2);
    point3D( -150, 150, 0, vpX, vpY, 0, 0, 200, 3);
     
    /*
     * pointA, pointB, pointC, color R, color G, color B, index
     */
    //正面
    Triangle(points[0], points[1], points[2], 256, 0, 0, 0);
    Triangle(points[0], points[2], points[3], 0, 0, 256, 1);
     
    function point3D(x, y, z, vpX, vpY, cX, cY, cZ, i)
    {
        var p = {};
        p.x = x;
        p.y = y;
        p.z = z;
        p.vpX = vpX;
        p.vpY = vpY;
        p.cX = cX;
        p.cY = cY;
        p.cZ = cZ;
        p.angleX = 0;
        p.angleY = 0;
        p.angleZ = 0;
        p.screenX = 0;
        p.screenY = 0;
        points[i] = p;
    }
     
    function Triangle(pointA, pointB, pointC, r, g, b, i)
    {
        var t = {};
        t.pointA = pointA;
        t.pointB = pointB;
        t.pointC = pointC;
        t.r = r;
        t.g = g;
        t.b = b;
        triangles[i] = t;
    }
     
    function draw(pointA, pointB, pointC, r, g, b)
    {
        ctx.fillStyle = "rgba(" + r + "," + g + "," + b + "," + 1 + ")";  
        ctx.strokeStyle = "rgba(" + r + "," + g + "," + b + "," + 1 + ")";
        ctx.lineWidth = 1;
        ctx.beginPath();
        ctx.moveTo(pointA.screenX, pointA.screenY);
        ctx.lineTo(pointB.screenX, pointB.screenY);
        ctx.lineTo(pointC.screenX, pointC.screenY);
        ctx.lineTo(pointA.screenX, pointA.screenY);
        ctx.fill();
        ctx.stroke();
    }
      
    setAnimation();
      
    function setAnimation()
    {
        animation();
        requestAnimationFrame(setAnimation);
    }
      
    function animation()
    {
         
        var angleX = (mouseY - vpY) * 0.001;
        var angleY = (mouseX - vpX) * 0.001;
         
        for(var i = 0; i < points.length; i++)
        {
            var point = points[i];
             
            setRotateX(point, angleX);
            setScreenX(point);
            setRotateY(point, angleY);
            setScreenY(point);
             
        }
         
        ctx.clearRect(0, 0, 300, 300);
        for(i = 0; i < triangles.length; i++)
        {
            draw(triangles[i].pointA, triangles[i].pointB, triangles[i].pointC, triangles[i].r, triangles[i].g, triangles[i].b);
        }
    }
     
    function setScreenX(point)
    {
        var scale = fl / (fl + point.z + point.cZ);
        point.screenX = point.vpX + (point.cX + point.x) * scale;
    }
     
    function setScreenY(point)
    {
        var scale = fl / (fl + point.z + point.cZ);
        point.screenY = point.vpY + (point.cY + point.y) * scale;
    }
     
    function setRotateX(point, angleX)
    {
        point.angleX = angleX;
         
        var cosX = Math.cos(point.angleX);
        var sinX = Math.sin(point.angleX);
         
        var y1 = point.y * cosX - point.z * sinX;
        var z1 = point.z * cosX + point.y * sinX;
         
        point.y = y1;
        point.z = z1;
    }
     
    function setRotateY(point, angleY)
    {
        point.angleY = angleY;
         
        var cosY = Math.cos(point.angleY);
        var sinY = Math.sin(point.angleY);
         
        var x1 = point.x * cosY - point.z * sinY;
        var z1 = point.z * cosY + point.x * sinY;
         
        point.x = x1;
        point.z = z1;
    }
     
    function setRotateZ(point, angleZ)
    {
        var cosZ = Math.cos(point.angleZ);
        var sinZ = Math.sin(point.angleZ);
         
        var x1 = point.x * cosZ - point.y * sinZ;
        var y1 = point.y * cosZ + point.x * sinZ;
         
        point.x = x1;
        point.y = y1;
    }
     
    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;
}

コメントを残す

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