セルオートマトンのパターン展開|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/base.js"></script>
    </head>
    <body>
        <div id="contents">
            <canvas id="canvas" width="300" height="300">
            </canvas>
        </div>
    </body>
</html>

2.CSS

@charset &quot;utf-8&quot;;
 
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: 300px;
    height:300px;
    border: 1px solid #000;
    overflow:hidden;
}
 
#canvas
{
    position: absolute;
    top: 0;
    left: 0;
    width: 300px;
    height:300px;
}

3.JavaScript

base.js

window.addEventListener("load", init, false);
 
function init()
{
    var theCanvas = document.getElementById("canvas");
    var centerX = (theCanvas.currentStyle || document.defaultView.getComputedStyle(theCanvas,'')).width;
    centerX = Number(centerX.replace('px',''));
    centerX =  centerX / 2;
    var centerY = (theCanvas.currentStyle || document.defaultView.getComputedStyle(theCanvas,'')).height;
    centerY = Number(centerY.replace('px',''));
    centerY =  centerY / 2;
    var ctx = theCanvas.getContext("2d");
    
	sx = 0;
    sy = 0;
	n=0;
    
    w = 5;
    h = 5;
    sx = 0;
    sy = 0;
    ar = new Array();
	colors = new Array();
    n = 51;
	md = 4;
	colors.push('#000000');
	colors.push('#ff0000');
	colors.push('#0000ff');
	colors.push('#ffff00');
	ab = new Array();
    centerX = centerX - (n * w / 2);
    centerY = centerY - (n * h / 2);
    
   	for (var i = 1; i <= n * n; i++)
    {
		c = '#000000';
        a = 0;
		if (i % n == 0 || i % n == 1 || i >= 1 && i <= n || i >= n * n - n + 1 && i <= n * n)
		{
			c = '#ffffff';
		}
		
		if (i == ((n + 1) * (n + 1)) / 2 - n)
		{
			c = '#ff0000';
			a = 1;
		}
		
        drawRectangle(ctx, w, h, sx, sy, c, a, i);
        sx += w;
        if (i % n == 0)
        {
            sx = 0;
            sy += h;
        }
    }
    
    setInterval(draw, 1000);
    
    function draw()
	{
		sx = 0;
		sy = 0;
		for (var i = 0; i < ar.length; i++)
		{
			ab[i] = ar[i];
		}

		ctx.clearRect(0,0,300,300);

		for (i = 1; i <=  ab.length; i++)
		{
			c = '#ffffff';
			a = 0;
			if (i % n != 0 && i % n != 1 && i >= 53 && i <= n * n - n+1)
			{
				//var t = (ab[i - 1 - 1] + ab[i + 1 - 1] + ab[i - n - 1] + ab[i + n - 1]) % md;
				var t = (ab[i - 1 - 1] + ab[i + 1 - 1] + ab[i - n - 1] + ab[i + n - 1]+(2*ab[i - 1])) % md;
				c = colors[(t * 4 / md)];	
				a = t;
				
			}
			
			drawRectangle(ctx, w, h, sx, sy, c, a, i);
			 
			sx += w;
			if (i % n == 0)
			{
				sx = 0;
				sy += h;
			}
		}
	}
    
    function drawRectangle(ctx, w, h, sx, sy, c, a, i)
    {
		ar[i - 1] = a;
	    ctx.fillStyle = c;
		ctx.beginPath();
        ctx.moveTo(centerX + sx, centerY + sy);
        ctx.lineTo(centerX + sx + w, centerY + sy);
        ctx.lineTo(centerX + sx + w, centerY + sy + h);
        ctx.lineTo(centerX + sx, centerY + sy + h);
        ctx.fill();
	    ctx.closePath();
    }
}

コメントを残す

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