プログラミングとイラストレーション » フォワード・キネマティクス3D|HTML5
プログラミングとイラストレーション > HTML5 > フォワード・キネマティクス3D|HTML5

フォワード・キネマティクス3D|HTML5

フォワード・キネマティクス3D|HTML5

フォワード・キネマティクス3D|HTML5(HTML5, JavaScript, CSS3) : デモ

フォワード・キネマティクス3D|HTML5(HTML5, JavaScript, CSS3) : ZIPファイル(5kb)

1.HTML

<!DOCTYPE html>
<html lang="jp">
    <head>
        <meta charset="UTF-8" />
        <title>フォワード・キネマティクス・ふたつのセグメントを制御する</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 id="Z">Z</div>
            <div id="Y">Y</div>
            <div id="X">X</div>
        </div>
        <div id="deg"></div>
    </body>
</html>

2.CSS

@charset "utf-8";
  
body
{
    margin: 0;
    padding: 0;
    background-color: #fff;
    color: #000000;
    font-size: 12px;
    font-family:Verdana, HiraKakuProN-W3, sans-serif;
}
  
#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;
}
 
#Z,#Y,#X
{
    position: absolute;
    top: 212px;
    left: 10px;
    width: 50px;
    height: 10px;
}
 
#Y
{
    top: 233px;
}
 
#X
{
    top: 253px;
}
 
#deg
{
    position: absolute;
    top: 5px;
    left: 5px;
    width: 275px;
    height: 12px;
}

3.JavaScript

base.js

window.addEventListener("load", init, false);
 
function init()
{
    var mouseX = 0,
    mouseY = 0,
    offsetX = 0,
    offsetY = 0,
    ctx,
    buttonDown,
    buttonMove,
    buttonUp,
    theCanvas,
    balls = [],
    balls2 = [],
    dist2 = 0,
    numBalls = 2,
    btn1,
    btn2,
    btn3,
    slideStart = 30,
    slideEnd = 240,
    fl = 150,
    setCenterCz = 0;
     
    document.getElementById('canvas').addEventListener('touchstart', function() 
    {
        event.preventDefault();
    });
    document.getElementById('canvas').addEventListener('touchmove', function() 
    {
        event.preventDefault();
    });
    document.getElementById('canvas').addEventListener('touchend', function() 
    {
        event.preventDefault();
    });
      
    var 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',''));
     
    //ボタンタイプ
    if(g_mobiledevice === true)
    {
        buttonDown = 'touchstart';
        buttonMove = 'touchmove';
        buttonUp = 'touchend';
    }
    else
    {
        buttonDown = 'mousedown';
        buttonMove = 'mousemove';
        buttonUp = 'mouseup';
    }
     
    //OneSegment--start
    for(var i = 0; i < numBalls; i++)
    {
        var ball = new Object();
        ball.rColor = 0;
        ball.gColor = 0;
        ball.bColor = 0;
        ball.r = 10;
        switch(i)
        {
            case 0:
                ball.x = 100;
                ball.y = 0;
                ball.xpos = 100;
                ball.ypos = 0;
                ball.zpos = 0;
                break;
            case 1:
                ball.x = 0;
                ball.y = 0;
                ball.xpos = 0;
                ball.ypos = 0;
                ball.zpos = 0;
                break;
        }
        ball.vpX = offsetX / 2;
        ball.vpY = offsetY / 2;
        ball.cX = 0;
        ball.cY = 0;
        ball.scaleX = 1;
        ball.scaleY = 1;
        ball.cZ = setCenterCz;
        balls[i] = ball;
    }
     
    //center fixed
    balls[0].x += offsetX / 2;
    balls[0].y += offsetY / 2;
    balls[1].x += offsetX / 2;
    balls[1].y += offsetY / 2;
     
    var dx = balls[0].x - balls[1].x;
    var dy = balls[0].y - balls[1].y;
    var angle = Math.atan2(dy, dx);
    dist = Math.sqrt(dx * dx + dy * dy);
    //OneSegment--end
     
    btn1 = new Object();
    btn1.x = offsetX / 2;
    btn1.y = 220;
    btn1.r = 8;
    btn1.d = 0;
    btn1.f = false;
     
    btn2 = new Object();
    btn2.x = offsetX / 2;
    btn2.y = 240;
    btn2.r = 8;
    btn2.d = 0;
    btn2.f = false;
     
    btn3 = new Object();
    btn3.x = offsetX / 2;
    btn3.y = 260;
    btn3.r = 8;
    btn3.d = 0;
    btn3.f = false;
    
    document.getElementById('canvas').addEventListener(buttonDown, mouseDownHandler, false);
    document.getElementById('canvas').addEventListener(buttonUp, mouseUpHandler, false);
     
    ctx = document.getElementById('canvas').getContext("2d");
    setBallAnimation();
      
    function setBallAnimation()
    {
        animation();
        requestAnimationFrame(setBallAnimation);
    }
      
    function animation()
    {
        ctx.clearRect(0, 0, 275, 275);
        oneSegmentRotateX();
             
        ctx.clearRect(0, 0, 275, 275);
        oneSegmentRotateY();
         
        ctx.clearRect(0, 0, 275, 275);
        oneSegmentRotateZ();
         
        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();
         
        if(balls[0].scaleX > 1)
        {
            ctx.strokeStyle = "rgb(" + 0 + "," + 0 + "," + 0 + ")";
            ctx.lineWidth = 1;
            ctx.beginPath();
            ctx.arc(balls[1].x, balls[1].y, balls[1].r * balls[1].scaleX, 0, Math.PI * 2, true);
            ctx.fillStyle = "rgb(" + 0 + "," + 0 + "," + 0 + ")";
            ctx.fill();
            ctx.stroke();
             
            ctx.lineWidth = 1;
            ctx.beginPath();
            ctx.arc(balls[0].x, balls[0].y, balls[0].r * balls[0].scaleX, 0, Math.PI * 2, true);
            ctx.fillStyle = "rgb(" + 256 + "," + 0 + "," + 0 + ")";
            ctx.fill();
            ctx.stroke();
        }
        else
        {
            ctx.strokeStyle = "rgb(" + 0 + "," + 0 + "," + 0 + ")";
            ctx.lineWidth = 1;
            ctx.beginPath();
            ctx.arc(balls[0].x, balls[0].y, balls[0].r * balls[0].scaleX, 0, Math.PI * 2, true);
            ctx.fillStyle = "rgb(" + 256 + "," + 0 + "," + 0 + ")";
            ctx.fill();
            ctx.stroke();
             
            ctx.lineWidth = 1;
            ctx.beginPath();
            ctx.arc(balls[1].x, balls[1].y, balls[1].r * balls[1].scaleX, 0, Math.PI * 2, true);
            ctx.fillStyle = "rgb(" + 0 + "," + 0 + "," + 0 + ")";
            ctx.fill();
            ctx.stroke();
        }
         
        //ボタンの範囲内の線--Start
        ctx.strokeStyle = "#000000";
        ctx.lineWidth = 1;
        ctx.beginPath();
        ctx.moveTo(slideStart, 220);
        ctx.lineTo(slideEnd, 220);
        ctx.stroke();
         
        ctx.strokeStyle = "#000000";
        ctx.lineWidth = 1;
        ctx.beginPath();
        ctx.moveTo(slideStart, 240);
        ctx.lineTo(slideEnd, 240);
        ctx.stroke();
         
        ctx.strokeStyle = "#000000";
        ctx.lineWidth = 1;
        ctx.beginPath();
        ctx.moveTo(slideStart, 260);
        ctx.lineTo(slideEnd, 260);
        ctx.stroke();
        //ボタンの範囲内の線--End
         
        //ボタン表示--Start
        ctx.strokeStyle = "rgb(" + 0 + "," + 0 + "," + 0 + ")";
        ctx.lineWidth = 1;
        ctx.beginPath();
        ctx.arc(btn1.x, btn1.y, btn1.r, 0, Math.PI * 2, true);
        ctx.fillStyle = "rgb(" + 0 + "," + 0 + "," + 0 + ")";
        ctx.fill();
        ctx.stroke();
         
        ctx.strokeStyle = "rgb(" + 0 + "," + 0 + "," + 0 + ")";
        ctx.lineWidth = 1;
        ctx.beginPath();
        ctx.arc(btn2.x, btn2.y, btn2.r, 0, Math.PI * 2, true);
        ctx.fillStyle = "rgb(" + 0 + "," + 0 + "," + 0 + ")";
        ctx.fill();
        ctx.stroke();
         
        ctx.strokeStyle = "rgb(" + 0 + "," + 0 + "," + 0 + ")";
        ctx.lineWidth = 1;
        ctx.beginPath();
        ctx.arc(btn3.x, btn3.y, btn3.r, 0, Math.PI * 2, true);
        ctx.fillStyle = "rgb(" + 0 + "," + 0 + "," + 0 + ")";
        ctx.fill();
        ctx.stroke();
        //ボタン表示--End
         
        //document.getElementById('deg').innerHTML = balls[0].scaleX;
    }
     
    function oneSegmentRotateZ()
    {
        //OneSegment
        var radian = (btn1.x - offsetX / 2) * 0.001;
         
        var cos = Math.cos(radian);
        var sin = Math.sin(radian);
         
        var x1 = balls[0].xpos - balls[1].xpos;
        var y1 = balls[0].ypos - balls[1].ypos;
        var x2 = cos * x1 - sin * y1;
        var y2 = cos * y1 + sin * x1;
         
        balls[0].xpos = balls[1].xpos + x2;
        balls[0].ypos = balls[1].ypos + y2;
         
        perspective(balls[0]);
        perspective(balls[1]);
    }
     
    function oneSegmentRotateY()
    {
        //OneSegment
        var radian = (btn2.x - offsetX / 2) * 0.001;
         
        var cos = Math.cos(radian);
        var sin = Math.sin(radian);
         
        var x1 = balls[0].xpos - balls[1].xpos;
        var z1 = balls[0].zpos - balls[1].zpos;
        var x2 = cos * x1 - sin * z1;
        var z2 = cos * z1 + sin * x1;
         
        balls[0].xpos = balls[1].xpos + x2;
        balls[0].zpos = balls[1].zpos + z2;
         
        perspective(balls[0]);
        perspective(balls[1]);
    }
     
    function oneSegmentRotateX()
    {
        //OneSegment
        var radian = (btn3.x - offsetX / 2) * 0.001;
         
        var cos = Math.cos(radian);
        var sin = Math.sin(radian);
         
        var y1 = balls[0].ypos - balls[1].ypos;
        var z1 = balls[0].zpos - balls[1].zpos;
        var y2 = cos * y1 - sin * z1;
        var z2 = cos * z1 + sin * y1;
         
        balls[0].ypos = balls[1].ypos + y2;
        balls[0].zpos = balls[1].zpos + z2;
         
        perspective(balls[0]);
        perspective(balls[1]);
    }
     
    function perspective(ball)
    {
        if (ball.zpos > -fl)
        {
            var scale = fl / (fl + ball.zpos + ball.cZ);
            ball.scaleX = scale;
            ball.scaleY = scale;
            ball.x = ball.xpos * scale;
            ball.y = ball.ypos * scale;
            ball.x += ball.vpX;
            ball.y += ball.vpY;
        }
    }
      
    function setMouseXY(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;
        }
    }
     
    function mouseUpHandler()
    {
        btn1.f = false;
        btn2.f = false;
        btn3.f = false;
    }
     
    function mouseDownHandler(e)
    {
        setMouseXY(e);
        touchBall(btn1);
        touchBall(btn2);
        touchBall(btn3);
        document.getElementById('canvas').addEventListener(buttonMove, mouseMoveHandler, false);
    }
     
    function touchBall(ball)
    {
        var dx = mouseX - ball.x;
        var dy = mouseY - ball.y;
        ball.d = Math.sqrt(dx * dx + dy * dy);
          
        if(ball.d <= ball.r)
        {
            ball.f = true;
        }
    }
     
    function mouseMoveHandler(e)
    {
        setMouseXY(e);
        if(btn1.f)
        {
            btn1.x = mouseX;
            if(btn1.x > slideEnd)
            {
                btn1.x = slideEnd;
            }
            else
            if(btn1.x < slideStart)
            {
                btn1.x = slideStart;
            }
        }
        else
        if(btn2.f)
        {
            btn2.x = mouseX;
            if(btn2.x > slideEnd)
            {
                btn2.x = slideEnd;
            }
            else
            if(btn2.x < slideStart)
            {
                btn2.x = slideStart;
            }
        }
        else
        if(btn3.f)
        {
            btn3.x = mouseX;
            if(btn3.x > slideEnd)
            {
                btn3.x = slideEnd;
            }
            else
            if(btn3.x < slideStart)
            {
                btn3.x = slideStart;
            }
        }
    }
}
 
// 各ブラウザ対応
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;
}

コメントを残す

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

コメントフィード

トラックバック URL : http://www.htmlcode.jp/%e3%83%95%e3%82%a9%e3%83%af%e3%83%bc%e3%83%89%e3%83%bb%e3%82%ad%e3%83%8d%e3%83%9e%e3%83%86%e3%82%a3%e3%82%af%e3%82%b93d%ef%bd%9chtml5/trackback/