円軌道を辿りながら螺旋を描く|HTML5

円軌道を辿りながら螺旋を描く|HTML5

円軌道を辿りながら螺旋を描く(HTML5, JavaScript, CSS3) : デモ

円軌道を辿りながら螺旋を描く|HTML5(HTML5, JavaScript, CSS3) : ZIPファイル(3kb)

画面をクリックすると、再度描画します。

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:#fff;
}
  
#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()
{ 
    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)
    {
        var buttonDown = 'touchstart',
        buttonMove = 'touchmove',
        buttonUp = 'touchend';
    }
    else
    {
        buttonDown = 'mousedown';
        buttonMove = 'mousemove';
        buttonUp = 'mouseup';
    }
     
    var theCanvas,
    offsetX,
    offsetY;
     
    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;
      
    var ctx = theCanvas.getContext("2d"),
    radian = 0,
    radian2 = 0,
    xpos = 0,
    ypos = 0,
    xpos2 = 0,
    ypos2 = 0,
    d = 0,
    distance = 100,
    distance2 = 20,
    degree = 0;
     
    document.getElementById('canvas').addEventListener(buttonUp, mouseUpHandler, false);
    draw();
    var _animation = setInterval(animation, 1000/60);
     
    function mouseUpHandler()
    {
        ctx.clearRect(0, 0, 275, 275);
        distance = 100;
        distance2 = 20;
        radian = 0;
        radian2 = 0;
        xpos = 0;
        ypos = 0;
        xpos2 = 0;
        ypos2 = 0;
        degree = 0;
        draw();
        _animation = setInterval(animation, 1000/60);
    }
      
    function animation()
    {
        if(degree > 360)
        {
            clearInterval(_animation);
        }
        ctx.clearRect(0, 0, 275, 275);
        degree++;
        setRadian(degree);
        setXY();
        ctx.lineTo(xpos2, ypos2);
        ctx.stroke();
    }
     
    function draw()
    {
        setRadian(0);
        setXY();
        ctx.strokeStyle = "#000000";
        ctx.lineWidth = 0.5;
        ctx.beginPath();
        ctx.moveTo(xpos2, ypos2);
    }
     
    function setXY()
    {
        xpos = offsetX + Math.cos(radian) * distance;
        ypos = offsetY + Math.sin(radian) * distance;
        xpos2 = xpos + Math.cos(radian2) * distance2;
        ypos2 = ypos + Math.sin(radian2) * distance2;
    }
     
    function setRadian(degree)
    {
        radian = degree * Math.PI / 180;
        radian2 = radian * 10;
    }
}

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

コメントを残す

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