シリンダー 3D|Cylinder 3D|HTML5

シリンダー 3D|Cylinder 3D|HTML5

シリンダー 3D|Cylinder 3D|HTML5(HTML5, JavaScript, CSS3) : デモ

シリンダー 3D|Cylinder 3D|HTML5(HTML5, JavaScript, CSS3) : ZIPファイル(5kb)

1.HTML

<!DOCTYPE html>
<html lang="jp">
    <head>
        <meta charset="UTF-8" />
        <title>3d Cylinder</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,
    light,
    numFaces = 20,
    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
     */
    var index = 0;
    for(var i = 0; i < numFaces; i++)
    {
        var angle = Math.PI * 2 / numFaces * i;
        var xpos = Math.cos(angle) * 180;
        var ypos = Math.sin(angle) * 180;
        point3D(xpos, ypos, -100, vpX, vpY, 0, 0, 200, index);
        point3D(xpos, ypos,  100, vpX, vpY, 0, 0, 200, index+1);
        index += 2;
    }
     
    /*
     * pointA, pointB, pointC, color R, color G, color B, index
     */     
    index = 0;
    for(var i = 0; i < numFaces - 1; i++)
    {
        Triangle(points[index], points[index + 3], points[index + 1], 204, 204, 204, index);
        Triangle(points[index], points[index + 2], points[index + 3], 204, 204, 204, index + 1);
        index += 2;
    }
     
    Triangle(points[index], points[1], points[index + 1], 204, 204, 204, index);
    Triangle(points[index], points[0], points[1], 204, 204, 204, index + 1);
             
    Light();
    setAnimation();
     
    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, angleZ:0,
            screenX:0,screenY:0
        };
    }
     
    function Triangle(pointA, pointB, pointC, r, g, b, i)
    {
        var zpos = depth(pointA, pointB, pointC);
        triangles[i] = {pointA:pointA, pointB:pointB, pointC:pointC, r:r, g:g, b:b, zpos:zpos};
    }
     
    function draw(pointA, pointB, pointC, r, g, b)
    {
        /*if (isBackFace(pointA, pointB, pointC))
        {
            return;
        }*/
         
        var colorR = getAdjustedColor(pointA, pointB, pointC, r, g, b).red;
        var colorG = getAdjustedColor(pointA, pointB, pointC, r, g, b).green;
        var colorB = getAdjustedColor(pointA, pointB, pointC, r, g, b).blue;
         
        ctx.fillStyle = "rgba(" + colorR + "," + colorG + "," + colorB + "," + 1 + ")";  
        ctx.strokeStyle = "rgba(" + colorR + "," + colorG + "," + colorB + "," + 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();
    }
     
    /*
     * 光源設定
     */
    function Light()
    {
        light = {};
        light.x = -100,
        light.y = -100,
        light.z = -100,
        light.brightness = 1;
    }
     
    /*
     * 面の照明調整
     */
    function getAdjustedColor(pointA, pointB, pointC, r, g, b)
    {
        var red = r;
        var green = g;
        var blue = b;
         
        var lightFactor = getLightFactor(pointA, pointB, pointC);
         
        red *= lightFactor;
        green *= lightFactor;
        blue *= lightFactor;
         
        red = Math.round(red);
        green = Math.round(green);
        blue = Math.round(blue);
         
        return {red:red, green:green, blue:blue};
    }
     
    /*
     * 光線の角度
     */
    function getLightFactor(pointA, pointB, pointC)
    {
        var ab = {};
        ab.x = pointA.x - pointB.x;
        ab.y = pointA.y - pointB.y;
        ab.z = pointA.z - pointB.z;
         
        var bc = {};
        bc.x = pointB.x - pointC.x;
        bc.y = pointB.y - pointC.y;
        bc.z = pointB.z - pointC.z;
         
        var norm = {};
        norm.x = (ab.y * bc.z) - (ab.z * bc.y);
        norm.y = -((ab.x * bc.z) - (ab.z * bc.x));
        norm.z = (ab.x * bc.y) - (ab.y * bc.x);
         
        var dotProd = norm.x * light.x +
        norm.y * light.y +
        norm.z * light.z;
         
        var normMag = Math.sqrt(norm.x * norm.x +
        norm.y * norm.y + norm.z * norm.z);
         
        var lightMag = Math.sqrt(light.x * light.x +
        light.y * light.y +
        light.z * light.z);
         
        return (Math.acos(dotProd/(normMag*lightMag))/Math.PI) * light.brightness;
    }
     
    /*
     * 隠面消去
     */
    function isBackFace(pointA, pointB, pointC)
    {
        var cax = pointC.screenX - pointA.screenX;
        var cay = pointC.screenY - pointA.screenY;
        var bcx = pointB.screenX - pointC.screenX;
        var bcy = pointB.screenY - pointC.screenY;
        return cax * bcy > cay * bcx;
    }
     
    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++)
        {
            triangles[i].zpos = depth(triangles[i].pointA, triangles[i].pointB, triangles[i].pointC);
            draw(triangles[i].pointA, triangles[i].pointB, triangles[i].pointC, triangles[i].r, triangles[i].g, triangles[i].b);
        }
        sortZ();
    }
     
    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 depth(pointA, pointB, pointC)
    {
        var zpos = Math.min(pointA.z, pointB.z);
        zpos = Math.min(zpos, pointC.z);
        return zpos;
    }
     
    function sortZ()
    {
        triangles.sort(function(a, b) 
        {
            if(a["zpos"] < b["zpos"])
            {
                var v = 1;
            }
            else
            {
                v = -1;
            }
            return v;
        });
    }
     
    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;
}

コメントを残す

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