複数の目標を持つバネ|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>
    </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);
 
var mouseX = 0;
var mouseY = 0;
var ctx = null;
 
var buttonDown = null;
var buttonMove = null;
var buttonUp = null;
var theCanvas = null;
 
var balls = new Array();
var offsetX = 0;
var offsetY = 0;
 
var ball = new Object();
ball.x = 0;
ball.y = 0;
ball.vx = 0;
ball.vy = 0;
ball.r = 10;
 
var gravity = 5;
var spring = 0.1;
var friction = 0.8;
 
function init()
{
    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',''));
     
    document.getElementById('canvas').addEventListener('touchstart', function() 
    {
        event.preventDefault();
    });
    document.getElementById('canvas').addEventListener('touchmove', function() 
    {
        event.preventDefault();
    });
    document.getElementById('canvas').addEventListener('touchend', function() 
    {
        event.preventDefault();
    });
     
    //ボタンタイプ
    if(g_mobiledevice==true)
    {
        buttonDown = 'touchstart';
        buttonMove = 'touchmove';
        buttonUp = 'touchend';
    }
    else
    {
        buttonDown = 'mousedown';
        buttonMove = 'mousemove';
        buttonUp = 'mouseup';
    }
     
    for(var i=0; i<3; i++)
    {
        balls[i] = new Object();
        balls[i].x = Math.random() * offsetX;
        balls[i].y = Math.random() * offsetY;
        balls[i].vx = 0;
        balls[i].vy = 0;
        balls[i].r = 10;
    }
     
    mouseX = offsetX/2;
    mouseY = offsetY/2;
     
    document.getElementById('canvas').addEventListener(buttonDown, mouseDownHandler, false);
     
    //document.getElementById('canvas').addEventListener(buttonMove, mouseMoveHandler, false);
     
    setBallAnimation();
 
}
 
function setRandomXY()
{
    for(var i=0; i<balls.length;i++)
    {
        balls[i].x = Math.random() * offsetX;
        balls[i].y = Math.random() * offsetY;
    }
}
 
function setBallAnimation()
{
    ctx = document.getElementById('canvas').getContext("2d");
    setInterval(animation, 1000/60);
    setInterval(setRandomXY, 2000);
}
 
function animation()
{
    ctx.clearRect(0,0,300,300);
     
    for(i=1; i<balls.length;i++)
    {
        var handle = balls[i];
        var dx = handle.x - ball.x;
        var dy = handle.y - ball.y;
        ball.vx += dx * spring;
        ball.vy += dy * spring;
    }
     
    ball.vx *= friction;
    ball.vy *= friction;
    ball.x += ball.vx;
    ball.y += ball.vy;
     
    for(i=0; i<3;i++)
    {
        ctx.fillStroke="#000000";
        ctx.lineWidth=1;
        ctx.beginPath();
        ctx.moveTo(ball.x, ball.y);
        ctx.lineTo(balls[i].x,balls[i].y);
        ctx.stroke();
    }
     
    for(var i=0; i<balls.length;i++)
    {
        ctx.strokeStyle = "#000000";
        ctx.fillStyle = "#000000";
        ctx.lineWidth = 1;
        ctx.beginPath();
        ctx.arc(balls[i].x, balls[i].y, balls[i].r, 0, Math.PI*2, true);
        ctx.fill();
        ctx.stroke();
    }
     
    ctx.strokeStyle = "#000000";
    ctx.fillStyle = "#ffffff";
    ctx.lineWidth = 1;
    ctx.beginPath();
    ctx.arc(ball.x, ball.y, ball.r, 0, Math.PI*2, true);
    ctx.fill();
    ctx.stroke();
}
 
function moveBall(ball,targetX,targetY)
{
    ball.vx += (targetX-ball.x)*spring;
    ball.vy += (targetY-ball.y)*spring;
    ball.vy += gravity;
    ball.vx *= friction;
    ball.vy *= friction;
    ball.x += ball.vx;
    ball.y += ball.vy;
}
 
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 mouseDownHandler(e)
{
    for(var i=0; i<balls.length;i++)
    {
        balls[i].x = Math.random() * offsetX;
        balls[i].y = Math.random() * offsetY;
    }
}
 
function mouseMoveHandler(e)
{
    setMouseXY(e);
}

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;
}

コメントを残す

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