プログラミングとイラストレーション » 2つのオブジェクトにバネを割り当てる|HTML5
プログラミングとイラストレーション > HTML5 > 2つのオブジェクトにバネを割り当てる|HTML5

2つのオブジェクトにバネを割り当てる|HTML5

2つのオブジェクトにバネを割り当てる|HTML5

2つのオブジェクトにバネを割り当てる|HTML5(HTML5, JavaScript, CSS3) : デモ

2つのオブジェクトにバネを割り当てる|HTML5(HTML5, JavaScript, CSS3) : ZIPファイル(4kb)

オブジェクトをドラッグ。

1.HTML

<!DOCTYPE html>
<html lang="jp">
    <head>
        <meta charset="UTF-8" />
        <title>2つのオブジェクトにバネを割り当てる</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;
    font-family:Helvetica, HiraKakuProN-W3, sans-serif; 
    font-size:12px;
    color:#000;
}
 
#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 mouseX = 0;
    var mouseY = 0;
    var offsetX = 0;
    var offsetY = 0;
    var ctx = null;
    var springLength = 100;
    var friction = 0.91;
    var spring = 0.1;
     
    var buttonDown = null;
    var buttonMove = null;
    var buttonUp = null;
    var theCanvas = null;
     
    var ball0 = new Object();
    ball0.f=false;
    ball0.x = 0;
    ball0.y = 0;
    ball0.vx = 0;
    ball0.vy = 0;
    ball0.r = 10;
    ball0.d = 0;
     
    var ball1 = new Object();
    ball1.f=false;
    ball1.x = 0;
    ball1.y = 0;
    ball1.vx = 0;
    ball1.vy = 0;
    ball1.r = 10;
    ball1.d = 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',''));
    //offsetX =  offsetX / 2;
    offsetY = (theContents.currentStyle || document.defaultView.getComputedStyle(theContents,'')).height;
    offsetY = Number(offsetY.replace('px',''));
    //offsetY =  offsetY / 2;
     
    //ボタンタイプ
    if(g_mobiledevice==true)
    {
        buttonDown = 'touchstart';
        buttonMove = 'touchmove';
        buttonUp = 'touchend';
    }
    else
    {
        buttonDown = 'mousedown';
        buttonMove = 'mousemove';
        buttonUp = 'mouseup';
    }
     
    ball0.x = Math.random() * offsetX;
    ball0.y = Math.random() * offsetY;
     
    ball1.x = Math.random() * offsetX;
    ball1.y = Math.random() * offsetY;
     
    document.getElementById('canvas').addEventListener(buttonDown, mouseDownHandler, false);
     
    document.getElementById('canvas').addEventListener(buttonUp, mouseUpHandler, false);
    
    setBallAnimation();
     
    function setBallAnimation()
    {
        ctx = document.getElementById('canvas').getContext("2d");
        setInterval(animation, 1000/60);
    }
     
    function animation()
    {
        ctx.clearRect(0,0,300,300);
        ctx.strokeStyle = "#000000";
        ctx.lineWidth = 1;
         
        ctx.beginPath();
        ctx.arc(ball0.x, ball0.y, ball0.r,0,Math.PI*2,true);
        ctx.fill();
        ctx.stroke();
         
        ctx.beginPath();
        ctx.arc(ball1.x, ball1.y, ball1.r,0,Math.PI*2,true);
        ctx.fill();
        ctx.stroke();
         
        if(!ball0.f)
        {
            springTo(ball0, ball1);
        }
        if(!ball1.f)
        {
            springTo(ball1, ball0);
        }
        ctx.strokeStyle = "#000000";
        ctx.lineWidth = 1;
        ctx.beginPath();
        ctx.moveTo(ball0.x, ball0.y);
        ctx.lineTo(ball1.x, ball1.y);
        ctx.stroke();
        //bezier();
    }
     
    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()
    {
        ball0.f = false;
        ball1.f = false;
    }
     
    function mouseDownHandler(e)
    {
        setMouseXY(e);
        var dx0 = mouseX - ball0.x;
        var dy0 = mouseY - ball0.y;
        ball0.d = Math.sqrt(dx0 * dx0 + dy0 * dy0);
         
        if(ball0.d <= ball0.r)
        {
            ball0.f = true;
        }
         
        var dx1 = mouseX - ball1.x;
        var dy1 = mouseY - ball1.y;
        ball1.d = Math.sqrt(dx1 * dx1 + dy1 * dy1);
         
        if(ball1.d <= ball1.r)
        {
            ball1.f = true;
        }
         
        document.getElementById('canvas').addEventListener(buttonMove, mouseMoveHandler, false);
    }
     
    function mouseMoveHandler(e)
    {
        if(ball0.f)
        {
            setMouseXY(e);
            ball0.x = mouseX;
            ball0.y = mouseY;
        }
        else
        if(ball1.f)
        {
            setMouseXY(e);
            ball1.x = mouseX;
            ball1.y = mouseY;
        }
    }
     
    function springTo(ballA, ballB)
    {
        var dx = ballB.x - ballA.x;
        var dy = ballB.y - ballA.y;
        var angle = Math.atan2(dy, dx);
        var targetX = ballB.x - (Math.cos(angle) * springLength);
        var targetY = ballB.y - (Math.sin(angle) * springLength);
        ballA.vx += (targetX - ballA.x) * spring;
        ballA.vy += (targetY - ballA.y) * spring;
        ballA.vx *= friction;
        ballA.vy *= friction;
        ballA.x += ballA.vx;
        ballA.y += ballA.vy;
    }
}

devicecheck.js

var g_mobiledevice = false; //iPhone、iPad、Androidのときtrue
var g_browsername = 'unknown';
var g_browserver = -1;
 
//navigator.userAgent エージェントの取得
//window.alert(navigator.userAgent);
if(checkUserAgent() == false)
{
    window.alert('This browser does not support.');
}
 
function checkUserAgent()
{
    // iPhone
    if ( navigator.userAgent.indexOf('iPhone') >= 0)
    {
        g_mobiledevice = true;
        g_browsername = 'iPhone';
        return true;
    // iPad
    } 
    else
    if( navigator.userAgent.indexOf('iPad') >= 0)
    {
        g_mobiledevice = true;
        g_browsername = 'iPad';
        return true;
    // iPod
    } 
    else
    if( navigator.userAgent.indexOf('iPod') >= 0)
    {
        g_mobiledevice = true;
        g_browsername = 'iPod';
        return true;
    // Android
    }
    else
    if( navigator.userAgent.indexOf('Android') >= 0)
    {
        var str = navigator.userAgent.match(/Android \d+\.\d+/);
        var ver = str[0].match(/\d+/);
        g_browserver = Number(ver);
        g_mobiledevice = true;
        if(g_browserver > 2.1) 
        {
            g_browsername = 'Android';
            return true;
        }
    }
     
    if(navigator.userAgent.indexOf('Chrome') >= 0)
    {
        var str = navigator.userAgent.match(/Chrome\/\d+/);
        var ver = str[0].match(/\d+/);
        g_browserver = Number(ver);
        if(g_browserver >= 9) 
        {
            g_browsername = 'Chrome';
            return true;
        }
    }
    else
    if(navigator.userAgent.indexOf('MSIE') >= 0)
    {
        str = navigator.userAgent.match(/MSIE \d+/);
        ver = str[0].match(/\d+/);
        g_browserver = Number(ver);
        if(g_browserver >= 9) 
        {
            g_browsername = 'MSIE';
            return true;
        }
    }
    else
    if(navigator.userAgent.indexOf('Opera') >= 0)
    {
        str = navigator.userAgent.match(/Version\/\d+/);
        ver = str[0].match(/\d+/);
        g_browserver = Number(ver);
        if(g_browserver >= 11) 
        {
            g_browsername = 'Opera';
            return true;
        }
    }
    else
    if(navigator.userAgent.indexOf('Firefox') >= 0)
    {
        str = navigator.userAgent.match(/Firefox\/\d+/);
        ver = str[0].match(/\d+/);
        g_browserver = Number(ver);
        if(g_browserver >= 4) 
        {
            g_browsername = 'Firefox';
            return true;
        }
    }
    else
    if(navigator.userAgent.indexOf('Safari') >= 0)
    {
        str = navigator.userAgent.match(/Version\/\d+/);
        ver = str[0].match(/\d+/);
        g_browserver = Number(ver);
        if(g_browserver >= 5) 
        {
            g_browsername = 'Safari';
            return true;
        }
    }
     
    return false;
}

コメントを残す

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

コメントフィード

トラックバック URL : http://www.htmlcode.jp/%ef%bc%92%e3%81%a4%e3%81%ae%e3%82%aa%e3%83%96%e3%82%b8%e3%82%a7%e3%82%af%e3%83%88%e3%81%ab%e3%83%90%e3%83%8d%e3%82%92%e5%89%b2%e3%82%8a%e5%bd%93%e3%81%a6%e3%82%8b%ef%bd%9chtml5/trackback/