ライフゲーム 黒のエリア|HTML5

ライフゲーム 黒のエリア|HTML5

ライフゲーム 黒のエリア(HTML5, JavaScript, CSS3) : デモ

ライフゲーム 黒のエリア(HTML5, JavaScript, CSS3) : ZIPファイル(4kb)

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 = 2,
    center_x = 0,
    center_y = 0,
    cells = [],
    cells_x = 68,
	cells_y = 68,
   	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 = cells_x * cells_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 < cells_y; y++)
		{
            for(var x = 0; x < cells_x ; x++)
			{   
                var ix = y * cells_y + x;
				
                if(check === true)
				{
                    var alive_cnt = 0;
                    // 周りの8セルをチェック
                    for(var yy = y - 1 ; yy <= y + 1; yy++)
					{
                        for(var xx = x - 1 ; xx <= x + 1; xx++)
						{
							/*var yyy = yy;
							var xxx = xx;
							if(x == 0)
							{
								xxx = cells_x - 1;
							}
							if(x == cells_x - 1)
							{
								xxx = 0;
							}
							if(y == 0)
							{
								yyy = cells_y - 1;
							}
							if(y == cells_y - 1)
							{
								yyy = 0;
							}
							var check_ix = yyy * cells_y + xxx;*/
							/*if(xxx == x && yyy == y)//myself
                            {
                            	if(old_cells[check_ix] === true)
								{
                                    alive_cnt++;
                                }
                            }*/
                            var check_ix = yy * cells_y + xx;
                            if(xx == x && yy == y)//myself
                            {
                            	if(old_cells[check_ix] === true)
								{
                                    alive_cnt++;
                                }
                            }
                            /*if(!(xxx == x && yyy == y) && yyy >= 0 && yyy < cells_y &&
                                 xxx >= 0 && xxx < cells_x)
							{*/
                            if(!(xx == x && yy == y) && yy >= 0 && yy < cells_y &&
                                 xx >= 0 && xx < cells_x)
							{
                                if(old_cells[check_ix] === true)
								{
                                    alive_cnt++;
                                }
                            }
                        }
                    }
					
                    if(alive_cnt <= 4)
					{
                        cells[ix] = false;
                    }
                    else
                    if(alive_cnt > 4)
                    {
                    	cells[ix] = true;
                    }
                    
                    if(alive_cnt === 4 || alive_cnt === 5)
					{
                        cells[ix] = !cells[ix];
                    }
                    
                }
               
			  	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);
	};
}());

コメントを残す

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