プログラミングとイラストレーション » 螺旋描画|HTML5
プログラミングとイラストレーション > HTML5 > 螺旋描画|HTML5

螺旋描画|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>螺旋を描く|Draw Spiral</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()
{ 
    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,
    xpos = 0,
    ypos = 0,
    distance = 0,
    d = 0,
    distance = 100;
     
    document.getElementById('canvas').addEventListener(buttonUp, mouseUpHandler, false);
    draw();
     
    function mouseUpHandler()
    {
        ctx.clearRect(0, 0, 300, 300);
        distance = 100;
        radian = 0;
        xpos = 0;
        ypos = 0;
        d = 0;
        draw();
        _animation = setInterval(animation, 1000/60);
    }
      
    function animation()
    {
        if(distance < 0)
        {
            clearInterval(_animation);
            return;
        }
        ctx.clearRect(0, 0, 300, 300);
        d++;
        distance += -0.01;
        setRadian(d);
        xpos = offsetX + Math.cos(radian) * distance;
        ypos = offsetY + Math.sin(radian) * distance;
        ctx.lineTo(xpos, ypos);
        ctx.stroke();
    }
    var _animation = setInterval(animation, 1000/60);
      
    function draw()
    {
        setRadian(0);
        xpos = offsetX + Math.cos(radian) * distance;
        ypos = offsetY + Math.sin(radian) * distance;
        ctx.strokeStyle = "#000000";
        ctx.lineWidth = 0.5;
        ctx.beginPath();
        ctx.moveTo(xpos, ypos);
    }
     
    function setRadian(degree)
    {
        radian = degree * Math.PI / 180;
    }
}

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/%e8%9e%ba%e6%97%8b%e6%8f%8f%e7%94%bb%ef%bd%9chtml5/trackback/