プログラミングとイラストレーション » インバース・キネマティクス・ふたつのセグメントとリーチ|HTML5
プログラミングとイラストレーション > HTML5 > インバース・キネマティクス・ふたつのセグメントとリーチ|HTML5

インバース・キネマティクス・ふたつのセグメントとリーチ|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 = 60,
    length2 = 60,
    numBalls = 2,
    numBalls2 = 2,
    dist = 0,
    dist2 = 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';
    }
     
    for(var i = 0; i < numBalls; i++)
    {
        var ball = new Object();
        ball.r = 10;
        ball.f = false;
        ball.rColor = 265;
        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;
    }
     
    //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 radian = Math.atan2(dy, dx);
    dist = Math.sqrt(dx * dx + dy * dy);
     
    for(var i = 0; i < numBalls2; i++)
    {
        var ball = new Object();
        ball.r = 10;
        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;
        }
        balls2[i] = ball;
    }
     
    //center fixed
    balls2[0].x += offsetX / 2;
    balls2[0].y += offsetY / 2;
    balls2[1].x += offsetX / 2;
    balls2[1].y += offsetY / 2;
     
    var dx = balls2[0].x - balls2[1].x;
    var dy = balls2[0].y - balls2[1].y;
    angle = Math.atan2(dy, dx);
    dist2 = Math.sqrt(dx * dx + dy * dy);
     
    document.getElementById('canvas').addEventListener(buttonMove, mouseMoveHandler, 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 = 0; 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();
        }
        
        //OneSegment//head--start
        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 radian2 = radian;
         
        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;
         
        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 +dist * Math.cos(radian);
        balls[1].y = balls[0].y +dist * Math.sin(radian);
        //OneSegment//head--end
         
        //TwoSegment//base--start
        dx = balls[1].x - balls2[1].x;
        dy = balls[1].y - balls2[1].y;
        rad = Math.atan2(dy, dx);
         
        dx = (balls2[0].x - balls2[1].x);
        dy = (balls2[0].y - balls2[1].y);
        rad2 = Math.atan2(dy, dx);
         
        radian = (rad - rad2) ;
         
        cos = Math.cos(radian);
        sin = Math.sin(radian);
         
        x1 = balls2[0].x - balls2[1].x;
        y1 = balls2[0].y - balls2[1].y;
        x2 = cos * x1 - sin * y1;
        y2 = cos * y1 + sin * x1;
        balls2[0].x = balls2[1].x + x2;
        balls2[0].y = balls2[1].y + y2;
         
        //offset
        /*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 + dist2 * Math.cos(radian);
        balls2[0].y = balls2[1].y + dist2 * Math.sin(radian);*/
        //TwoSegment//base--end
         
        //offset--start
        dx = balls[1].x - balls2[0].x;
        dy = balls[1].y - balls2[0].y;
         
        balls[1].x -= dx;
        balls[1].y -= dy;
         
        balls[0].x -= dx;
        balls[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;
}

コメントを残す

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

コメントフィード

トラックバック URL : http://www.htmlcode.jp/%e3%82%a4%e3%83%b3%e3%83%90%e3%83%bc%e3%82%b9%e3%83%bb%e3%82%ad%e3%83%8d%e3%83%9e%e3%83%86%e3%82%a3%e3%82%af%e3%82%b9%e3%83%bb%e3%81%b5%e3%81%9f%e3%81%a4%e3%81%ae%e3%82%bb%e3%82%b0%e3%83%a1%e3%83%b3-2/trackback/