円錐 3D|Corn 3D|HTML5

円錐 3D|Corn 3D|HTML5

円錐 3D|Corn 3D|HTML5(HTML5, JavaScript, CSS3) : デモ

円錐 3D|Corn 3D|HTML5(HTML5, JavaScript, CSS3) : ZIPファイル(5kb)

1.HTML

<!DOCTYPE html>
<html lang="jp">
    <head>
        <meta charset="UTF-8" />
        <title>3d Corn</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,
    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);
     
     
    var pointXY = [];
    var r = 100;
    setXY();
    function setXY()
    {
        for (var i = 0; i < 36; i++)
        {
            var x = Math.cos(i * 10 * Math.PI / 180) * r;
            var y = Math.sin(i * 10 * Math.PI / 180) * r;
            pointXY[i] = {x:x, y:y};
             
        }
    }
     
    /*for(var i = 0; i < pointXY.length; i++)
    {
        document.getElementById('txt').innerHTML += pointXY[i].x + ', ' + pointXY[i].y + '<br/>';
    }*/
    //window.alert(pointXY.length);
    
    /*
     * x, y, z, vpX, vpY, cX, cY, cZ, index
     */
    //
    var z = 100;
    point3D( pointXY[0].x, pointXY[0].y, z, vpX, vpY, 0, 0, 200, 0);
    point3D( pointXY[1].x, pointXY[1].y, z, vpX, vpY, 0, 0, 200, 1);
    point3D( pointXY[2].x, pointXY[2].y, z, vpX, vpY, 0, 0, 200, 2);
    point3D( pointXY[3].x, pointXY[3].y, z, vpX, vpY, 0, 0, 200, 3);
    point3D( pointXY[4].x, pointXY[4].y, z, vpX, vpY, 0, 0, 200, 4);
    point3D( pointXY[5].x, pointXY[5].y, z, vpX, vpY, 0, 0, 200, 5);
    point3D( pointXY[6].x, pointXY[6].y, z, vpX, vpY, 0, 0, 200, 6);
    point3D( pointXY[7].x, pointXY[7].y, z, vpX, vpY, 0, 0, 200, 7);
    point3D( pointXY[8].x, pointXY[8].y, z, vpX, vpY, 0, 0, 200, 8);
    point3D( pointXY[9].x, pointXY[9].y, z, vpX, vpY, 0, 0, 200, 9);
    point3D( pointXY[10].x, pointXY[10].y, z, vpX, vpY, 0, 0, 200, 10);
    point3D( pointXY[11].x, pointXY[11].y, z, vpX, vpY, 0, 0, 200, 11);
    point3D( pointXY[12].x, pointXY[12].y, z, vpX, vpY, 0, 0, 200, 12);
    point3D( pointXY[13].x, pointXY[13].y, z, vpX, vpY, 0, 0, 200, 13);
    point3D( pointXY[14].x, pointXY[14].y, z, vpX, vpY, 0, 0, 200, 14);
    point3D( pointXY[15].x, pointXY[15].y, z, vpX, vpY, 0, 0, 200, 15);
    point3D( pointXY[16].x, pointXY[16].y, z, vpX, vpY, 0, 0, 200, 16);
    point3D( pointXY[17].x, pointXY[17].y, z, vpX, vpY, 0, 0, 200, 17);
    point3D( pointXY[18].x, pointXY[18].y, z, vpX, vpY, 0, 0, 200, 18);
    point3D( pointXY[19].x, pointXY[19].y, z, vpX, vpY, 0, 0, 200, 19);
    point3D( pointXY[20].x, pointXY[20].y, z, vpX, vpY, 0, 0, 200, 20);
    point3D( pointXY[21].x, pointXY[21].y, z, vpX, vpY, 0, 0, 200, 21);
    point3D( pointXY[22].x, pointXY[22].y, z, vpX, vpY, 0, 0, 200, 22);
    point3D( pointXY[23].x, pointXY[23].y, z, vpX, vpY, 0, 0, 200, 23);
    point3D( pointXY[24].x, pointXY[24].y, z, vpX, vpY, 0, 0, 200, 24);
    point3D( pointXY[25].x, pointXY[25].y, z, vpX, vpY, 0, 0, 200, 25);
    point3D( pointXY[26].x, pointXY[26].y, z, vpX, vpY, 0, 0, 200, 26);
    point3D( pointXY[27].x, pointXY[27].y, z, vpX, vpY, 0, 0, 200, 27);
    point3D( pointXY[28].x, pointXY[28].y, z, vpX, vpY, 0, 0, 200, 28);
    point3D( pointXY[29].x, pointXY[29].y, z, vpX, vpY, 0, 0, 200, 29);
    point3D( pointXY[30].x, pointXY[30].y, z, vpX, vpY, 0, 0, 200, 30);
    point3D( pointXY[31].x, pointXY[31].y, z, vpX, vpY, 0, 0, 200, 31);
    point3D( pointXY[32].x, pointXY[32].y, z, vpX, vpY, 0, 0, 200, 32);
    point3D( pointXY[33].x, pointXY[33].y, z, vpX, vpY, 0, 0, 200, 33);
    point3D( pointXY[34].x, pointXY[34].y, z, vpX, vpY, 0, 0, 200, 34);
    point3D( pointXY[35].x, pointXY[35].y, z, vpX, vpY, 0, 0, 200, 35);
    point3D( 0, 0, z, vpX, vpY, 0, 0, 200, 36);
    point3D( 0, 0, -100, vpX, vpY, 0, 0, 200, 37);
     
    /*
     * pointA, pointB, pointC, color R, color G, color B, index
     */
    var r = 256;
    var g = 0;
    var b = 0;
    Triangle(points[36], points[1], points[0], r, g, b, 0);
    Triangle(points[36], points[2], points[1], r, g, b, 1);
    Triangle(points[36], points[3], points[2], r, g, b, 2);
    Triangle(points[36], points[4], points[3], r, g, b, 3);
    Triangle(points[36], points[5], points[4], r, g, b, 4);
    Triangle(points[36], points[6], points[5], r, g, b, 5);
    Triangle(points[36], points[7], points[6], r, g, b, 6);
    Triangle(points[36], points[8], points[7], r, g, b, 7);
    Triangle(points[36], points[9], points[8], r, g, b, 8);
    Triangle(points[36], points[10], points[9], r, g, b, 9);
    Triangle(points[36], points[11], points[10], r, g, b, 10);
    Triangle(points[36], points[12], points[11], r, g, b, 11);
    Triangle(points[36], points[13], points[12], r, g, b, 12);
    Triangle(points[36], points[14], points[13], r, g, b, 13);
    Triangle(points[36], points[15], points[14], r, g, b, 14);
    Triangle(points[36], points[16], points[15], r, g, b, 15);
    Triangle(points[36], points[17], points[16], r, g, b, 16);
    Triangle(points[36], points[18], points[17], r, g, b, 17);
    Triangle(points[36], points[19], points[18], r, g, b, 18);
    Triangle(points[36], points[20], points[19], r, g, b, 19);
    Triangle(points[36], points[21], points[20], r, g, b, 20);
    Triangle(points[36], points[22], points[21], r, g, b, 21);
    Triangle(points[36], points[23], points[22], r, g, b, 22);
    Triangle(points[36], points[24], points[23], r, g, b, 23);
    Triangle(points[36], points[25], points[24], r, g, b, 24);
    Triangle(points[36], points[26], points[25], r, g, b, 25);
    Triangle(points[36], points[27], points[26], r, g, b, 26);
    Triangle(points[36], points[28], points[27], r, g, b, 27);
    Triangle(points[36], points[29], points[28], r, g, b, 28);
    Triangle(points[36], points[30], points[29], r, g, b, 29);
    Triangle(points[36], points[31], points[30], r, g, b, 30);
    Triangle(points[36], points[32], points[31], r, g, b, 31);
    Triangle(points[36], points[33], points[32], r, g, b, 32);
    Triangle(points[36], points[34], points[33], r, g, b, 33);
    Triangle(points[36], points[35], points[34], r, g, b, 34);
    Triangle(points[36], points[0], points[35], r, g, b, 35);
     
    Triangle(points[37], points[0], points[1], r, g, b, 36);
    Triangle(points[37], points[1], points[2], r, g, b, 37);
    Triangle(points[37], points[2], points[3], r, g, b, 38);
    Triangle(points[37], points[3], points[4], r, g, b, 39);
    Triangle(points[37], points[4], points[5], r, g, b, 40);
    Triangle(points[37], points[5], points[6], r, g, b, 41);
    Triangle(points[37], points[6], points[7], r, g, b, 42);
    Triangle(points[37], points[7], points[8], r, g, b, 43);
    Triangle(points[37], points[8], points[9], r, g, b, 44);
    Triangle(points[37], points[9], points[10], r, g, b, 45);
    Triangle(points[37], points[10], points[11], r, g, b, 46);
    Triangle(points[37], points[11], points[12], r, g, b, 47);
    Triangle(points[37], points[12], points[13], r, g, b, 48);
    Triangle(points[37], points[13], points[14], r, g, b, 49);
    Triangle(points[37], points[14], points[15], r, g, b, 50);
    Triangle(points[37], points[15], points[16], r, g, b, 51);
    Triangle(points[37], points[16], points[17], r, g, b, 52);
    Triangle(points[37], points[17], points[18], r, g, b, 53);
    Triangle(points[37], points[18], points[19], r, g, b, 54);
    Triangle(points[37], points[19], points[20], r, g, b, 55);
    Triangle(points[37], points[20], points[21], r, g, b, 56);
    Triangle(points[37], points[21], points[22], r, g, b, 57);
    Triangle(points[37], points[22], points[23], r, g, b, 58);
    Triangle(points[37], points[23], points[24], r, g, b, 59);
    Triangle(points[37], points[24], points[25], r, g, b, 60);
    Triangle(points[37], points[25], points[26], r, g, b, 61);
    Triangle(points[37], points[26], points[27], r, g, b, 62);
    Triangle(points[37], points[27], points[28], r, g, b, 63);
    Triangle(points[37], points[28], points[29], r, g, b, 64);
    Triangle(points[37], points[29], points[30], r, g, b, 65);
    Triangle(points[37], points[30], points[31], r, g, b, 66);
    Triangle(points[37], points[31], points[32], r, g, b, 67);
    Triangle(points[37], points[32], points[33], r, g, b, 68);
    Triangle(points[37], points[33], points[34], r, g, b, 69);
    Triangle(points[37], points[34], points[35], r, g, b, 70);
    Triangle(points[37], points[35], points[0], r, g, b, 71);
     
    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;
}

コメントを残す

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