インバース・キネマティクス(1点を固定した3つのセグメント)|Inverse Kinematics|HTML5

インバース・キネマティクス(1点を固定した3つのセグメント)|Inverse Kinematics|HTML5

インバース・キネマティクス(1点を固定した3つのセグメント)|Inverse Kinematics|HTML5|HTML5(HTML5, JavaScript, CSS3) : デモ

インバース・キネマティクス(1点を固定した3つのセグメント)|Inverse Kinematics|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>
        <div id="deg"></div>
    </body>
</html>

2.CSS

@charset "utf-8";
  
body
{
    margin: 0;
    padding: 0;
    background-color: #fff;
    color: #000000;
}
  
#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;
}
 
#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 = [],
    balls3 = [],
    length = 40,
    numBalls = 2,
    baseX = 0,
    baseY = 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',''));
     
    baseX = offsetX / 2;
    baseY = offsetY / 2;
     
    //ボタンタイプ
    if(g_mobiledevice === true)
    {
        buttonDown = 'touchstart';
        buttonMove = 'touchmove';
        buttonUp = 'touchend';
    }
    else
    {
        buttonDown = 'mousedown';
        buttonMove = 'mousemove';
        buttonUp = 'mouseup';
    }
     
    /*
     * segmane3 start
     */
    setSegment(balls3, 0, 0, 0);
    /*
     * segmane3 end
     */
     
    /*
     * segmane2 start
     */
    setSegment(balls2, 0, 0, 0);
    /*
     * segmane2 end
     */
     
    /*
     * segmane1 start
     */
    setSegment(balls, 0, 0, 0);
    /*
     * segmane1 end
     */
 
    document.getElementById('canvas').addEventListener(buttonDown, mouseDownHandler, false);
    document.getElementById('canvas').addEventListener(buttonUp, mouseUpHandler, false);
     
    ctx = document.getElementById('canvas').getContext("2d");
     
    balls[1].x = baseX;
    balls[1].y = baseY;
    balls[0].x = length + balls[1].x;
    balls[0].y = balls[1].y;
     
    balls2[1].x = balls[0].x;
    balls2[1].y = balls[0].y;
    balls2[0].x = length + balls2[1].x;
    balls2[0].y = balls2[1].y;
     
    balls3[1].x = balls2[0].x;
    balls3[1].y = balls2[0].y;
    balls3[0].x = length + balls3[1].x;
    balls3[0].y = balls3[1].y;
     
    setBallAnimation();
      
    function setBallAnimation()
    {
        animation();
        requestAnimationFrame(setBallAnimation);
    }
     
    function mouseUpHandler()
    {
        balls3[0].f = false;
        balls2[0].f = false;
        balls[0].f = false;
    }
     
    function mouseDownHandler(e)
    {
        setMouseXY(e);
        touchBall(balls3[0]);
        touchBall(balls2[0]);
        touchBall(balls[0]);
        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 setSegment(array, r, g, b)
    {
        for(var i = 0; i < numBalls; i++)
        {
            var ball = {};
            ball.r = 15;
            ball.f = false;
             
            switch(i)
            {
                case 0:
                    ball.x = length;
                    ball.y = 0;
                    ball.rColor = r;
                    ball.gColor = g;
                    ball.bColor = b;
                    break;
                case 1:
                    ball.x = 0;
                    ball.y = 0;
                    ball.rColor = r;
                    ball.gColor = g;
                    ball.bColor = b;
                    break;
            }
            array[i] = ball;
        }
    }
     
    function animation()
    {
        ctx.clearRect(0, 0, 275, 275);
         
        drawLine(balls);
        drawLine(balls2);
        drawLine(balls3);
         
        drawBall(balls);
        drawBall(balls2);
        drawBall(balls3);
 
        if(balls3[0].f)
        {
            var flag = false;
            headSegment(balls3, flag);
            balls2[0].x = balls3[1].x;
            balls2[0].y = balls3[1].y;
            joint(balls2[1], balls2[0]);
            balls[0].x = balls2[1].x;
            balls[0].y = balls2[1].y;
             
            /*
             * segment1
             */
            balls[1].x = baseX;
            balls[1].y = baseY;
             
            dx = balls[0].x - balls[1].x;
            dy = balls[0].y - balls[1].y;
            radian = Math.atan2(dy, dx);
            balls[0].x = balls[1].x + length * Math.cos(radian);
            balls[0].y = balls[1].y + length * Math.sin(radian);
             
            balls2[1].x = balls[0].x;
            balls2[1].y = balls[0].y;
             
            /*
             * segment2
             */
            dx = balls2[0].x - balls2[1].x;
            dy = balls2[0].y - balls2[1].y;
            radian = Math.atan2(dy, dx);
            balls2[0].x = balls2[1].x + length * Math.cos(radian);
            balls2[0].y = balls2[1].y + length * Math.sin(radian);
             
            balls3[1].x = balls2[0].x;
            balls3[1].y = balls2[0].y;
             
            /*
             * segment3
             */
            dx = balls3[0].x - balls3[1].x;
            dy = balls3[0].y - balls3[1].y;
            radian = Math.atan2(dy, dx);
            balls3[0].x = balls3[1].x + length * Math.cos(radian);
            balls3[0].y = balls3[1].y + length * Math.sin(radian);
             
        }
        else
        if(balls2[0].f)
        {
            var flag = false;
            headSegment(balls2, flag);
            baseSegment();
            balls3[1].x = balls2[0].x;
            balls3[1].y = balls2[0].y;
            joint(balls3[0], balls3[1]);
        }
        else
        if(balls[0].f)
        {
            var flag = true;
            headSegment(balls, flag);
            balls2[1].x = balls[0].x;
            balls2[1].y = balls[0].y;
            joint(balls2[0], balls2[1]);
            balls3[1].x = balls2[0].x;
            balls3[1].y = balls2[0].y;
            joint(balls3[0], balls3[1]);
        }
    }
     
    function drawBall(balls)
    {
         
         
        for(var i = 0; i < numBalls - 1; i++)
        {
            ctx.strokeStyle = "rgb(" + balls[i].rColor + "," + balls[i].gColor + "," + balls[i].bColor + ")";
            ctx.lineWidth = 1;
            ctx.beginPath();
            ctx.arc(balls[i].x, balls[i].y, balls[i].r, 0, Math.PI * 2, true);
            ctx.fillStyle = "rgb(" + balls[i].rColor + "," + balls[i].gColor + "," + balls[i].bColor + ")";
            ctx.fill();
            ctx.stroke();
        }
    }
     
    function drawLine(balls)
    {
        ctx.strokeStyle = "#000000";
        ctx.lineWidth = 1;
        ctx.beginPath();
        ctx.moveTo(balls[0].x, balls[0].y);
        ctx.lineTo(balls[1].x, balls[1].y);
        ctx.stroke();
    }
     
    function joint(ballA, ballB)
    {
        var dx = ballB.x - ballA.x;
        var dy = ballB.y - ballA.y;
        var radian = Math.atan2(dy, dx);
        ballA.x = ballB.x - Math.cos(radian) * length;
        ballA.y = ballB.y - Math.sin(radian) * length;
    }
     
    function headSegment(balls, flag)
    {
        var dx = mouseX - balls[1].x;
        var dy = mouseY - balls[1].y;
        var rad = Math.atan2(dy, dx);
         
        dx = (balls[0].x - balls[1].x);
        dy = (balls[0].y - balls[1].y);
        var rad2 = Math.atan2(dy, dx);
         
        var radian = (rad - rad2) ;
         
        var cos = Math.cos(radian);
        var sin = Math.sin(radian);
         
        var x1 = balls[0].x - balls[1].x;
        var y1 = balls[0].y - balls[1].y;
        var x2 = cos * x1 - sin * y1;
        var y2 = cos * y1 + sin * x1;
        balls[0].x = balls[1].x + x2;
        balls[0].y = balls[1].y + y2;
         
        /*
         * 
         */
        if(!flag)
        {
            balls[0].x += mouseX - balls[0].x;
            balls[0].y += mouseY - balls[0].y;
             
            dx = balls[1].x - balls[0].x;
            dy = balls[1].y - balls[0].y;
            radian = Math.atan2(dy, dx);
            balls[1].x = balls[0].x + length * Math.cos(radian);
            balls[1].y = balls[0].y + length * Math.sin(radian);
        }
        else
        {
            dx = balls[0].x - balls[1].x;
            dy = balls[0].y - balls[1].y;
            radian = Math.atan2(dy, dx);
            balls[0].x = balls[1].x + length * Math.cos(radian);
            balls[0].y = balls[1].y + length * Math.sin(radian);
             
            balls[1].x = baseX;
            balls[1].y = baseY;
        }
    }
     
    function baseSegment()
    {
        //TwoSegment//base--start
        var dx = balls2[1].x - balls[1].x;
        var dy = balls2[1].y - balls[1].y;
        var rad = Math.atan2(dy, dx);
         
        dx = (balls[0].x - balls[1].x);
        dy = (balls[0].y - balls[1].y);
        var rad2 = Math.atan2(dy, dx);
         
        radian = (rad - rad2) ;
         
        var cos = Math.cos(radian);
        var sin = Math.sin(radian);
         
        var x1 = balls[0].x - balls[1].x;
        var y1 = balls[0].y - balls[1].y;
        var x2 = cos * x1 - sin * y1;
        var y2 = cos * y1 + sin * x1;
        balls[0].x = balls[1].x + x2;
        balls[0].y = balls[1].y + y2;
        //TwoSegment//base--end
         
        //offset--start 第1関節の座標位置を第2関節へ結合補正
        dx = balls2[1].x - balls[0].x;
        dy = balls2[1].y - balls[0].y;
         
        balls2[1].x -= dx;
        balls2[1].y -= dy;
         
        balls2[0].x -= dx;
        balls2[0].y -= dy;
        //offset-end
    }
     
    function mouseMoveHandler(e)
    {
        setMouseXY(e);
    }
     
    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;
        }
    }
}
 
// 各ブラウザ対応
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;
}

コメントを残す

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