プログラミングとイラストレーション » ライフゲーム|HTML5
プログラミングとイラストレーション > HTML5 > ライフゲーム|HTML5

ライフゲーム|HTML5

ライフゲーム|HTML5

ライフゲーム(HTML5, JavaScript, CSS3) : デモ

ライフゲーム(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>
        <canvas id="canvas" width="275" height="275"></canvas>
    </body>
</html>

2.CSS

@charset "utf-8";
 
body
{
    margin: 0;
    padding: 0;
    background-color: #fff;
}
 
#canvas
{
    position: absolute;
    top: 0;
    left: 0;
    width: 275px;
    height: 275px;
}

3.JavaScript

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

base.js

window.addEventListener("load", init, false);
 
function init()
{
    var offsetX = 0,
    offsetY = 0,
   	ctx,
   	theCanvas,
   	buttonDown,
    buttonMove,
    buttonUp,
    num_x = 0,
    num_y = 0,
    cell_size = 5,
    center_x = 0,
    center_y = 0,
    cells = [],
    colums_x = 27,
	colums_y = 27,
    _RADIUS = 5,
   	theContents;
   	
    theContents = document.getElementById("canvas");
    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', ''));
    
    //ボタンタイプ
    if(g_mobiledevice === true)
    {
        buttonDown = 'touchstart';
        buttonMove = 'touchmove';
        buttonUp = 'touchend';
    }
    else
    {
        buttonDown = 'mousedown';
        buttonMove = 'mousemove';
        buttonUp = 'mouseup';
    }
    
    document.getElementById('canvas').addEventListener('touchstart', function() 
    {
        event.preventDefault();
    });
    document.getElementById('canvas').addEventListener('touchmove', function() 
    {
        event.preventDefault();
    });
    document.getElementById('canvas').addEventListener('touchend', function() 
    {
        event.preventDefault();
    });
     
    document.getElementById('canvas').addEventListener(buttonDown, buttonDownHandler, false);
    
   	ctx = document.getElementById('canvas').getContext("2d");
   	
   	
    
    var cell_count = colums_x * colums_y;
    
    function reStart()
    {
    	for(var i = 0 ; i < cell_count; i++)
		{
	        var x = Math.random() * 2;
	        if(x < 1)
	        {
	        	 cells[i] = false;
	        }
	        else
	        {
	        	 cells[i] = true;
	        }
	        //console.log(_cells[i]);
	    }
    }
    
    reStart();
    inter();
    //animation();
    
    function buttonDownHandler(e)
    {
    	clearInterval(inter);
    	reStart();
    	inter();
    }
    
    function inter()
    {
    	draw(true);
    }
    setInterval(inter, 130);
    
    function animation()
	{
		draw(true);
		requestAnimationFrame(animation);
	}
    
    function draw(check)
	{
		ctx.clearRect(0, 0, 300, 300);
		
		//枠を描く
		ctx.strokeStyle ="rgba(" + 0 + "," + 0 + "," + 0 + "," + 1 + ")";
		ctx.lineWidth = 1;
        ctx.beginPath();
        ctx.rect(0, 0, offsetX, offsetY);
        ctx.fillStyle = "rgba(" + 255 + "," + 255 + "," + 255 + "," + 1 + ")";
        ctx.fill();
        ctx.stroke();
        
        var old_cells = [];
		old_cells = old_cells.concat(cells);
		
		for(var y = 0 ; y < colums_y; y++)
		{
            for(var x = 0; x < colums_x ; x++)
			{   
                var ix = y * colums_y + x;
				
                if(check === true)
				{
                    var alive_cnt = 0;
                    var count = 0;
                    // 周りの8セルをチェック
                    for(var yy = y - 1 ; yy <= y + 1; yy++)
					{
                        for(var xx = x - 1 ; xx <= x + 1; xx++)
						{
							var check_ix = yy * colums_y + xx;
							count++;
                            //console.log(count);
                            //console.log(yy);
                            if(!(xx == x && yy == y) &&  // not myself
                                 yy >= 0 && yy < colums_y &&
                                 xx >= 0 && xx < colums_x)
							{
                                if(old_cells[check_ix] === true)
								{
                                    alive_cnt++;
                                    //console.log(alive_cnt);
                                }
                            }
                        }
                    }
					
                    if(cells[ix] === true)
					{
                        if(alive_cnt === 2 || alive_cnt === 3)
						{
                            cells[ix] = true;
                        }
                        else
                        {
                        	cells[ix] = false;
                        }
                    }
					else
					{
                        if(alive_cnt === 3)
						{
                            cells[ix] = true;
                        }
                    }
                }
               
			  	if(cells[ix] === true)
				{
					var c = 0;
				}
				else
				{
					c = 255;
				}
				var pi = Math.PI * 2;
				ctx.strokeStyle ="rgba(" + 0 + "," + 0 + "," + 0 + "," + 1 + ")";
				ctx.lineWidth = 0;
		        ctx.beginPath();
		        ctx.arc(x * cell_size * 2 + cell_size + 2, y * cell_size * 2 + cell_size + 2, cell_size, 0, pi, true);
		        //ctx.rect(x * _RADIUS * 2, y*_RADIUS * 2, _RADIUS, _RADIUS);
		        ctx.fillStyle = "rgba(" + c + "," + c + "," + c + "," + 1 + ")";
		        ctx.fill();
		        ctx.stroke();
            }
        }
    }

    
}

// 各ブラウザ対応
window.requestAnimationFrame = (function()
{
	return window.requestAnimationFrame ||
	window.webkitRequestAnimationFrame ||
	window.mozRequestAnimationFrame ||
	window.oRequestAnimationFrame ||
	window.msRequestAnimationFrame ||
	function(callback)
	{
		window.setTimeout(callback, 1000/60);
	};
}());

コメントを残す

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

コメントフィード

トラックバック URL : http://www.htmlcode.jp/%e3%83%a9%e3%82%a4%e3%83%95%e3%82%b2%e3%83%bc%e3%83%a0%ef%bd%9chtml5/trackback/