- Author:
- Stefan Franke
- Twitter:
- @
- GitHub:
- Facebook:
- Google+:
- +
- Reddit:
- /r/
- Pouet:
- Website:
- franke.ms
- Compo:
- classic
- Demo link:
- https://js1k.com/2010-first/demo/191
- Shortlink:
- https://js1k.com/191
- Blog post:
- please update here!
- Bytes:
- 944
- Chars:
- 944
- Submission
C=document.getElementById('c');
c=C.getContext('2d');
w=C.width=640;v=C.height=480;W=w*4;X=w*v*4;
c.fillStyle="black";
c.fillRect(0,0,w,v);
function l(){
d=c.getImageData(0,0,w,v);
p=d.data;
for(x=0;x<X;x+=4)p[x]=Math.random()>0.8?255:0;
c.putImageData(d,0,0)
}
function m(e){
if(!e)e=window.event;
if(e.which==32)R=!R;
if(e.which==65)l();
if(e.type=="click"){
d=c.getImageData(0,0,w,v);
i=(e.clientY-C.offsetTop+pageYOffset)*w+(e.clientX-C.offsetLeft+pageXOffset);
d.data[4*i]=e.which==1?255:0;
c.putImageData(d,0,0)
}}
function f(){
if(R){
d=c.getImageData(0,0,w,v);
p=d.data;
for(x=0;x<X;x+=4){
s=p[(x-4+X)%X]+p[(x+4)%X]+p[(x+W)%X]+p[(x+W-4)%X]+p[(x+W+4)%X]+p[(x-W+X)%X]+p[(x-W-4+X)%X]+p[(x-W+4+X)%X];
p[x+1]=s==510?p[x]:s==765?255:0;
}
for(x=0;x<X;x+=4)p[x]=p[x+2]=p[x+1];
c.putImageData(d,0,0)
}
setTimeout(f,500);
}
C.onclick=m;
window.onkeydown=m;
l();
R=1;
f();
- Description
- Conway's Game of Life
http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life
Starts with random cells and performs endless cycles.
SPACE toggle start/stop
'a' add rAndom celss
Left mouse button click: add a cell (marked red)
- Base64 encoded
Qz1kb2N1bWVudC5nZXRFbGVtZW50QnlJZCgnYycpOw0KYz1DLmdldENvbnRleHQoJzJkJyk7DQp3PUMud2lkdGg9NjQwO3Y9Qy5oZWlnaHQ9NDgwO1c9dyo0O1g9dyp2KjQ7DQpjLmZp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Rvdy5vbmtleWRvd249bTsNCmwoKTsNClI9MTsNCmYoKTs=
- Original source
// get the canvas 2d object
C=document.getElementById('c');
c=C.getContext('2d');
// initialize size and size depending values
w=C.width=640;v=C.height=480;W=w*4;X=w*v*4;
// clear the canvas with black
c.fillStyle="black";
c.fillRect(0,0,w,v);
// define a function to add random cells
function l(){
d=c.getImageData(0,0,w,v);
p=d.data;
for(x=0;x<X;x+=4) {
p[x]=Math.random()>0.8?255:0; // red value 255 marks a cell
}
c.putImageData(d,0,0)
}
// the event handler function
function m(e){
if(!e)e=window.event;
// toggle on space
if(e.which==32)R=!R;
// add cells on 'a'
if(e.which==65)l();
// add a cell with left mouse button clicks
if(e.type=="click"){
d=c.getImageData(0,0,w,v);
i=(e.clientY-C.offsetTop+pageYOffset)*w+(e.clientX-C.offsetLeft+pageXOffset);
d.data[4*i]=e.which==1?255:0;
c.putImageData(d,0,0)
}}
// this is the main loop, hooked with setTimeout
function f(){
if(R){ // if not running skip
d=c.getImageData(0,0,w,v);
p=d.data;
// pass 1: calculate next cycle
for(x=0;x<X;x+=4){
// use the red values to evaluate the neighbour count
s=p[(x-4+X)%X]+p[(x+4)%X]+p[(x+W)%X]+p[(x+W-4)%X]+p[(x+W+4)%X]+p[(x-W+X)%X]+p[(x-W-4+X)%X]+p[(x-W+4+X)%X];
// set the green value with the result:
// 2 neighbours -> keep the cell
// 3 neighbours -> create a cell
// otherwise -> remove the cell
p[x+1]=s==510?p[x]:s==765?255:0;
}
// pass 2: set RGB values
for(x=0;x<X;x+=4)p[x]=p[x+2]=p[x+1];
c.putImageData(d,0,0)
}
// meet again in 500ms
setTimeout(f,500);
}
// register the event handler
C.onclick=m;
window.onkeydown=m;
// initial load
l();
// running
R=1;
// start it
f();