Created
November 6, 2010 13:49
-
-
Save aont/665434 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> | |
| <HTML> | |
| <HEAD> | |
| <META name="GENERATOR" content="IBM WebSphere Homepage Builder Version 6.0.2.1 for Windows"> | |
| <META http-equiv="Content-Type" content="text/html; charset=Shift_JIS"> | |
| <META http-equiv="Content-Style-Type" content="text/css"> | |
| <TITLE></TITLE> | |
| </HEAD> | |
| <BODY> | |
| <DIV id="cv"></DIV> | |
| <A href="javascript:;" onclick="cv.advance();">advance</A> | |
| <A href="javascript:;" onclick="cv.timer($('time').value)">auto</A> | |
| <FORM><SELECT id="time"> | |
| <OPTION value="500" selected>0.5秒</OPTION> | |
| <OPTION value="1000" selected>1秒</OPTION> | |
| <OPTION value="5000">5秒</OPTION> | |
| <OPTION value="10000">11秒</OPTION> | |
| </SELECT></FORM> | |
| <SCRIPT language="javascript"> | |
| <!-- | |
| function rand(n) { | |
| return Math.floor(Math.random() * n); | |
| } | |
| function $(id) { | |
| return document.getElementById(id); | |
| } | |
| function Canvas(canvas, width, height, size) { | |
| var canvas = canvas; | |
| with(canvas) { | |
| style.background = "black"; | |
| style.width = 2 + width * size; | |
| style.height = 2 + height * size; | |
| style.position = "relative"; | |
| } | |
| var map = new Array(width); | |
| var divobjs = new Array(width); | |
| for (var x = 0; x < width; x++) { | |
| map[x] = new Array(height); | |
| divobjs[x] = new Array(height); | |
| } | |
| var divobj = document.createElement("div"); | |
| with(divobj) { | |
| style.position = "absolute"; | |
| style.width = size; | |
| style.height = size; | |
| style.overflow = "hidden"; | |
| } | |
| for (y = 0; y < height; y++) { | |
| for (x = 0; x < width; x++) { | |
| r = rand(2); | |
| divobjs[x][y] = canvas.appendChild(divobj.cloneNode(true)); | |
| with(divobjs[x][y]) { | |
| style.background = ["white", "black"][r] | |
| style.top = 1 + size * y; | |
| style.left = 1 + size * x; | |
| } | |
| map[x][y] = r; | |
| } | |
| } | |
| divobj = null; | |
| var advance = this.advance = function(p) { | |
| nextmap = new Array(width) | |
| for (var x = 0; x < width; x++) { | |
| nextmap[x] = new Array(height); | |
| } | |
| for (var y = 0; y < height; y++) { | |
| for (var x = 0; x < width; x++) { | |
| alive = 0; | |
| if (y > 0) { | |
| if (x > 0) { | |
| alive += map[x - 1][y - 1]; | |
| } | |
| alive += map[x][y - 1]; | |
| if (x < width - 1) { | |
| alive += map[x + 1][y - 1]; | |
| } | |
| } | |
| if (x > 0) { | |
| alive += map[x - 1][y]; | |
| } | |
| if (x < width - 1) { | |
| alive += map[x + 1][y]; | |
| } | |
| if (y < height - 1) { | |
| if (x > 0) { | |
| alive += map[x - 1][y + 1]; | |
| } | |
| alive += map[x][y + 1]; | |
| if (x < width - 1) { | |
| alive += map[x + 1][y + 1]; | |
| } | |
| } | |
| if (alive == 3) { | |
| nextmap[x][y] = 1; | |
| } else if (alive != 2) { | |
| nextmap[x][y] = 0; | |
| } else { | |
| nextmap[x][y] = map[x][y] | |
| } | |
| divobjs[x][y].style.background = ["white", "black"][nextmap[x][y]]; | |
| } | |
| } | |
| map = nextmap; | |
| nextmap = null; | |
| return true; | |
| } | |
| var invert = this.invert = function(x, y) { | |
| if (x < 0 || x > width - 1 || y < 0 || y > height - 1) return false; | |
| map[x][y] = 1 - map[x][y]; | |
| divobjs[x][y].style.background = ["white", "black"][map[x][y]]; | |
| return true; | |
| } | |
| var md = false; | |
| var x = 0; | |
| var y = 0; | |
| canvas.onmousedown = function(evt) { | |
| if (window.createPopup) { | |
| mouseX = event.x + document.body.scrollLeft; | |
| mouseY = event.y + document.body.scrollTop; | |
| } else { | |
| mouseX = evt.pageX; | |
| mouseY = evt.pageY; | |
| } | |
| x = Math.floor((mouseX - 1) / size); | |
| y = Math.floor((mouseY - 1) / size); | |
| invert(x, y); | |
| md = true; | |
| return true; | |
| } | |
| canvas.onmousemove = function(evt) { | |
| if (!md) return; | |
| if (window.createPopup) { | |
| mouseX = event.x + document.body.scrollLeft; | |
| mouseY = event.y + document.body.scrollTop; | |
| } else { | |
| mouseX = evt.pageX; | |
| mouseY = evt.pageY; | |
| } | |
| ox = x; | |
| oy = y; | |
| x = Math.floor((mouseX - 2) / size); | |
| y = Math.floor((mouseY - 2) / size); | |
| if (x != ox || y != oy) { | |
| invert(x, y); | |
| } | |
| ox = oy = null; | |
| return true; | |
| } | |
| canvas.onmouseup = function(evt) { | |
| md = false | |
| return true; | |
| } | |
| var timer = null; | |
| this.timer = function(t) { | |
| if (timer == null) | |
| return timer = setInterval(advance, t); | |
| else | |
| clearInterval(timer); | |
| timer = null; | |
| } | |
| return this; | |
| } | |
| var cv = new Canvas($("cv"), 64, 64, 8); | |
| //--> | |
| </SCRIPT> | |
| </BODY> | |
| </HTML> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment