インバース・キネマティクス・ふたつのセグメント|HTML5

インバース・キネマティクス・ふたつのセグメント|HTML5

インバース・キネマティクス・ふたつのセグメント|HTML5(HTML5, JavaScript, CSS3) : デモ

インバース・キネマティクス・ふたつのセグメント|HTML5(HTML5, JavaScript, CSS3) : ZIPファイル(4kb)

ボールにタッチして動かす。

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 = [],
    length = 100,
    length2 = 100,
    numBalls = 2,
    numBalls2 = 2;
     
    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';
    }
     
    for(var i = 0; i < numBalls; i++)
    {
        var ball = new Object();
        ball.r = 15;
        ball.f = false;
        ball.rColor = 0;
        ball.gColor = 0;
        ball.bColor = 0;
        switch(i)
        {
            case 0:
                ball.x = length;
                ball.y = 0;
                break;
            case 1:
                ball.x = 0;
                ball.y = 0;
                break;
        }
        balls[i] = ball;
    }
     
    var dx = balls[0].x - balls[1].x;
    var dy = balls[0].y - balls[1].y;
    var radian = Math.atan2(dy, dx);
    var dist = Math.sqrt(dx * dx + dy * dy);
     
    balls[0].x += dist * Math.cos(radian) - 65;
    balls[0].y += dist * Math.sin(radian) + offsetY / 2;
    balls[1].x += dist * Math.cos(radian) - 65;
    balls[1].y += dist * Math.sin(radian) + offsetY / 2;
     
     
    for(i = 0; i < numBalls2; i++)
    {
        var ball = new Object();
        ball.r = 15;
        ball.f = false;
        ball.rColor = 0;
        ball.gColor = 0;
        ball.bColor = 0;
        switch(i)
        {
            case 0:
                ball.x = length2;
                ball.y = 0;
                break;
            case 1:
                ball.x = 0;
                ball.y = 0;
                break;
        }
        balls2[i] = ball;
    }
     
    var dx = balls2[0].x - balls2[1].x;
    var dy = balls2[0].y - balls2[1].y;
    var radian = Math.atan2(dy, dx);
    var dist2 = Math.sqrt(dx * dx + dy * dy);
     
    balls2[0].x += dist2 * Math.cos(radian) + balls[1].x;
    balls2[0].y += dist2 * Math.sin(radian) + balls[1].y;
    balls2[1].x += dist2 * Math.cos(radian) + balls[1].x;
    balls2[1].y += dist2 * Math.sin(radian) + balls[1].y;
     
    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);
         
        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();
         
        for(var i = 1; i < numBalls; 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();
        }
         
        ctx.strokeStyle = "#000000";
        ctx.lineWidth = 1;
        ctx.beginPath();
        ctx.moveTo(balls2[0].x, balls2[0].y);
        ctx.lineTo(balls2[1].x, balls2[1].y);
        ctx.stroke();
         
        for(var i = 0; i < numBalls2; i++)
        {
            ctx.strokeStyle = "rgb(" + balls2[i].rColor + "," + balls2[i].gColor + "," + balls2[i].bColor + ")";
            ctx.lineWidth = 1;
            ctx.beginPath();
            ctx.arc(balls2[i].x, balls2[i].y, balls2[i].r, 0, Math.PI * 2, true);
            ctx.fillStyle = "rgb(" + balls2[i].rColor + "," + balls2[i].gColor + "," + balls2[i].bColor + ")";
            ctx.fill();
            ctx.stroke();
        }
        
        if(balls[1].f)
        {
            joint(balls[0], balls[1], length);
            joint(balls2[1], balls[0], 0);
            joint(balls2[0], balls2[1], length2);
        }
        else
        if(balls2[0].f)
        {
            joint(balls2[1], balls2[0], length2);
            joint(balls[0], balls2[1], 0);
            joint(balls[1], balls[0], length);
        }
        else
        if(balls2[1].f)
        {
            joint(balls2[0], balls2[1], length2);
            joint(balls[0], balls2[1], 0);
            joint(balls[1], balls[0], length);
        }
    }
     
    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;
        }
    }
     
    function mouseUpHandler()
    {
        balls[1].f = false;
        balls2[0].f = false;
        balls2[1].f = false;
    }
     
    function mouseDownHandler(e)
    {
        setMouseXY(e);
        touchBall(balls[1]);
        touchBall(balls2[0]);
        touchBall(balls2[1]);
        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(balls[1].f)
        {
            balls[1].x = mouseX;
            balls[1].y = mouseY;
        }
        else
        if(balls2[0].f)
        {
            balls2[0].x = mouseX;
            balls2[0].y = mouseY;
        }
        else
        if(balls2[1].f)
        {
            balls2[1].x = mouseX;
            balls2[1].y = mouseY;
        }
    }
     
    function joint(ballA, ballB, len)
    {
        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) * len;
        ballA.y = ballB.y - Math.sin(radian) * len;
    }
}
 
// 各ブラウザ対応
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;
}

コメントを残す

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