「A」3D|”A” 3D|HTML5

「A」3D|”A” 3D|HTML5

「A」3D|”A” 3D|HTML5(HTML5, JavaScript, CSS3) : デモ

「A」3D|”A” 3D|HTML5(HTML5, JavaScript, CSS3) : ZIPファイル(4kb)

タッチムーブで文字「A」がグリグリ回ります。

1.HTML

<!DOCTYPE html>
<html lang="jp">
    <head>
        <meta charset="UTF-8" />
        <title>3d SpinningA</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
     */
    point3D( -50, -250, 100, vpX, vpY, 0, 0, 200, 0);
    point3D( 50, -250, 100, vpX, vpY, 0, 0, 200, 1);
    point3D( 200,  250, 100, vpX, vpY, 0, 0, 200, 2);
    point3D( 100,  250, 100, vpX, vpY, 0, 0, 200, 3);
    point3D( 50,  100, 100, vpX, vpY, 0, 0, 200, 4);
    point3D( -50,  100, 100, vpX, vpY, 0, 0, 200, 5);
    point3D( -100,  250, 100, vpX, vpY, 0, 0, 200, 6);
    point3D( -200,  250, 100, vpX, vpY, 0, 0, 200, 7);
    point3D( 0, -150, 100, vpX, vpY, 0, 0, 200, 8);
    point3D( 50, 0, 100, vpX, vpY, 0, 0, 200, 9);
    point3D( -50, 0, 100, vpX, vpY, 0, 0, 200, 10);
     
    /*
     * pointA, pointB, pointC, color R, color G, color B, index
     */
    Triangle(points[0], points[1],  points[8], 102, 102, 204, 0);
    Triangle(points[1], points[9],  points[8], 102, 102, 204, 1);
    Triangle(points[1], points[2],  points[9], 102, 102, 204, 2);
    Triangle(points[2], points[4],  points[9], 102, 102, 204, 3);
    Triangle(points[2], points[3],  points[4], 102, 102, 204, 4);
    Triangle(points[4], points[5],  points[9], 102, 102, 204, 5);
    Triangle(points[9], points[5],  points[10], 102, 102, 204, 6);
    Triangle(points[5], points[6],  points[7], 102, 102, 204, 7);
    Triangle(points[5], points[7],  points[10], 102, 102, 204, 8);
    Triangle(points[0], points[10], points[7], 102, 102, 204, 9);
    Triangle(points[0], points[8],  points[10], 102, 102, 204, 10);
     
    function point3D(x, y, z, vpX, vpY, cX, cY, cZ, i)
    {
        points[i] = 
        {
            x:x,y:y,z:z,vpX:vpX,vpY:vpY,cX:cX,cY:cY,cZ:cZ,angleX:0,angleY:0,
        };
    }
     
    function Triangle(pointA, pointB, pointC, r, g, b, i)
    {
        triangles[i] = 
        {
            pointA:pointA,
            pointB:pointB,
            pointC:pointC,
            r:r,
            g:g,
            b:b
        };
    }
     
    function draw(pointA, pointB, pointC, r, g, b)
    {
        ctx.fillStyle = "rgba(" + r + "," + g + "," + b + "," + 0.5 + ")";  
        ctx.strokeStyle = "rgba(" + r + "," + g + "," + b + "," + 0.05 + ")";
        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()
    {
        ctx.clearRect(0, 0, 300, 300);
        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);
        }
         
        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;
}

コメントを残す

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