プログラミングとイラストレーション » 振り子運動のシミュレーション|HTML5
プログラミングとイラストレーション > HTML5 > 振り子運動のシミュレーション|HTML5

振り子運動のシミュレーション|HTML5

振り子運動のシミュレーション|HTML5

振り子運動のシミュレーション|HTML5(HTML5, JavaScript, CSS3) : デモ

振り子運動のシミュレーション|HTML5(HTML5, JavaScript, CSS3) : ZIPファイル(5kb)

画面をクリックすると、振り子運動を繰り返します。

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;
}
 
#txt
{
    position: absolute;
    top: 10px;
    left: 10px;
    width: 200px;
    height:50px;
}

3.JavaScript

base.js

window.addEventListener("load", init, false);
 
 
var Velocity = 0;//速度
 
var TimerInterval = 0;//タイマー値
 
var pY = 0;
var radius = 0;
 
var width = 0;
var height = 0;
 
var ctx = null;
 
var offsetX = 0;
var offsetY = 0;
 
var mouseX = 0;
var mouseY = 0;
 
//
var theta = 0;
var Gravity = 0;
var LenMat = 0;
var dt = 0;
var u = 0;
 
var X = 0;
var Y = 0;
var sinTH = 0;
var cosTH = 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);
     
    setInit();
    setInterval(run,TimerInterval);
}
 
function change()
{
    u = 0.001;
    Gravity = 0.98;
    dt = 1;
    LenMat = 150;
    Velocity = 0;
    theta = 45 * Math.PI / 180;
}
 
function run()
{
    ctx.clearRect(0,0,300,300);
    theta = theta + Velocity * dt;
    var sinTH = Math.sin(theta);
    Velocity = (Velocity - Gravity * sinTH * dt / LenMat) * (1 - u);
    draw();
}
 
function draw()
{
    var sinTH = Math.sin(theta);
    var cosTH = Math.cos(theta);
    X = LenMat * sinTH;
    Y = LenMat * cosTH;
    setSegmentLine(offsetX,0,X+offsetX,Y);
    setChangeBall(X+offsetX,Y);
}
 
function setInit()
{
    change();
    setLine(offsetX, 0, X+offsetX, Y);
    setBall(X+offsetX, Y, 10);
}
 
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;
    var Y = Y0+pY;
    ctx.fillStyle="#000000";
    ctx.beginPath();
    //context.arc(x, y, radius, startAngle, endAngle [, anticlockwise ] ) 
    ctx.arc(X, Y, 10, 0, Math.PI * 2, true);
    ctx.fill();
    ctx.stroke();
}
 
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();
}
 
function mouseDownHandler(e)
{
    //setMouseXY(e);
    Velocity = 0;
    theta = 45 * Math.PI / 180;
}
 
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;
    }
}

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.');
}
/*else
{
    //window.alert(navigator.userAgent);
    window.alert(g_mobiledevice+', '+g_browsername+', '+g_browserver);
}*/
 
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/%e6%8c%af%e3%82%8a%e5%ad%90%e9%81%8b%e5%8b%95%e3%81%ae%e3%82%b7%e3%83%9f%e3%83%a5%e3%83%ac%e3%83%bc%e3%82%b7%e3%83%a7%e3%83%b3%ef%bd%9chtml5/trackback/