バネの重りをドラッグする|HTML5

バネの重りをドラッグする|HTML5

バネの重りをドラッグする|HTML5(HTML5, JavaScript, CSS3) : デモ

マウスバネの重りをドラッグする|HTML5(HTML5, JavaScript, CSS3) : ZIPファイル(8kb)

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:25px;
    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 Force = 0;//力
var Kfact = 0;//バネ定数
var Mass = 0;//質量
var SpringLength = 0;//バネの長さ
var Position = 0;//位置
var Velocity = 0;//速度
var Acc = 0;//加速度
var Myu = 0;//粘性抵抗
var TimerInterval = 0;//タイマー値
var DT = 0;
var pY = 0;
var radius = 0;
var segNumbers = 0;
 
var width = 0;
var height = 0;
 
var ctx = null;
 
var offsetX = 0;
var offsetY = 0;
 
var oldX = 0;
var oldY = 0;
 
var flag = false;
 
var X1 = 0;
var Y1 = 0;
var X2 = 0;
var Y2 = 0;
 
function init()
{
    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';
    }
     
    var theCanvas = document.getElementById("canvas");
    offsetX = (theCanvas.currentStyle || document.defaultView.getComputedStyle(theCanvas,'')).width;
    offsetX = Number(offsetX.replace('px',''));
    offsetX =  offsetX / 2;
    offsetY = (theCanvas.currentStyle || document.defaultView.getComputedStyle(theCanvas,'')).height;
    offsetY = Number(offsetY.replace('px',''));
    offsetY =  offsetY / 2;
      
    ctx = theCanvas.getContext("2d");
     
    document.getElementById('canvas').addEventListener(buttonDown, mouseDownHandler, false);
      
    document.getElementById('canvas').addEventListener(buttonUp, mouseUpHandler, false);
     
    setInit();
     
    setInterval(move,TimerInterval);
}
 
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()
{
    if(flag)
    {
        Position =  mouseY/2;
    }
    flag = false;
}
  
function mouseDownHandler(e)
{
    setMouseXY(e);
    oldX = X1;
    oldY = Y1;
    var dx1 = mouseX - X1;
    var dy1 = mouseY - Y1;
    var dis = Math.sqrt(dx1 * dx1 + dy1 * dy1);
     
    if(dis <= radius)
    {
        flag = true;
    }
    document.getElementById('canvas').addEventListener(buttonMove, mouseMoveHandler, false);
}
  
function mouseMoveHandler(e)
{
    if(flag)
    {
        setMouseXY(e);
    }
}
 
function paraChange(Y)
{
    TimerInterval = 1000 / 30;//タイマー値
    SpringLength = offsetY/2;//バネの長さ
    segNumbers = 60;//スプリングの細分数
    Kfact = 40;//バネ定数
    Mass = 500;//質量
    radius = 10;//重りの半径
    Myu = 0.02;//粘性抵抗
    DT = 1;
    Position = Y;
    Velocity = 0;//速度
}
 
function setBall(X0,Y0,R)
{
    ctx.fillStyle="#000000";
    ctx.beginPath();
    //context.arc(x, y, radius, startAngle, endAngle [, anticlockwise ] ) 
    ctx.arc(X0-R/2, Y0-R/2+pY, R, 0, Math.PI*2, true);
    ctx.fill();
    ctx.stroke();
}
 
function setSegmentLine(X1,Y1,X2,Y2)
{
    var XX = X1;
    var YY = Y1;
     
    if(X2<X1)
    {
        XX = X2;
    }
     
    if(Y2<Y1)
    {
        YY = Y2;
    }
    var X = XX;
    var Y = YY+pY;
    width = Math.abs(X2-X1);
    height = Math.abs(Y2-Y1);
     
    ctx.fillStyle="#000000";
    ctx.beginPath();
    ctx.moveTo(X1,Y1+pY);
    ctx.lineTo(X2,Y2+pY);
    ctx.fill();
    ctx.stroke();
}
 
function setChangeBall(X0,Y0)
{
    var X = X0-width/2;
    var Y = (Y0-height/2)+pY;
    ctx.fillStyle="#000000";
    ctx.beginPath();
    //context.arc(x, y, radius, startAngle, endAngle [, anticlockwise ] ) 
    ctx.arc(X, Y, radius, 0, Math.PI * 2, true);
    ctx.fill();
    ctx.stroke();
}
 
function move()
{
    ctx.clearRect(0,0,275,275);
     
    Force = - Kfact * (Position - SpringLength);
    Acc = Force / Mass * DT;
    Velocity = (Velocity + Acc) * (1 - Myu);
    Position = Position + Velocity * DT;
     
    if(flag)
    {
        var dY = mouseY / segNumbers;
    }
    else
    {
        dY = (SpringLength + Position) / segNumbers;
    }
     
     
    Y1 = 0;
    X1 = offsetX;
     
    for(var i=1; i<=segNumbers; i++)
    {
        X2 = (i % 2) * radius + offsetX - radius;
        Y2 = i * dY;
         
        setSegmentLine(X1, Y1, X2, Y2);
        X1 = X2;
        Y1 = Y2;
    }
     
    if(flag)
    {
        setChangeBall(offsetX,mouseY);
    }
    else
    {
        setChangeBall(offsetX,SpringLength + Position);
    }
}
 
function setInit()
{
    paraChange(8);
    var dY = (SpringLength + Position) / segNumbers;
    var Y1 = 0;
    var X1 = offsetX;
     
    for(var i=1; i<=segNumbers; i++)
    {
        var X2 = (i % 2) * radius + offsetX - radius;
        var Y2 = i * dY;
         
        setLine(X1,Y1,X2,Y2);
         
        X1 = X2;
        Y1 = Y2;
    }
    setBall(offsetX, SpringLength + Position, radius);
}
 
function setLine(X1,Y1,X2,Y2)
{
    ctx.strokeStyle = "#000000";
    ctx.lineWidth = 1;
    ctx.beginPath();
    ctx.moveTo(X1, Y1 + pY);
    ctx.lineTo(X2, Y2 + pY);
    ctx.fill();
    ctx.stroke();
}

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

コメントを残す

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