Skip to content

Instantly share code, notes, and snippets.

@deltam
Created May 8, 2011 05:17
Show Gist options
  • Select an option

  • Save deltam/961131 to your computer and use it in GitHub Desktop.

Select an option

Save deltam/961131 to your computer and use it in GitHub Desktop.
映画『ウォッチメン』のロールシャッハ仮面を作れないか実験中
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>Rorschach Mask</title>
<script type="text/javascript" src="rorschach.js"></script>
</head>
<body>
<div align="center"><b>Rorschach Mask</b></div>
<div id="div_id" align="center" style="padding:10px;">
<canvas id="canvas_element" width="300" height="300"></canvas>
</div>
<span id="canvas_xy"></span><br/>
<!-- <img id="img_id"></img> -->
</body>
</html>
// rorschach.js:
function $(id) { return document.getElementById(id); }
function Point(x, y) { return {'x':x, 'y':y}; }
function rand(max) { return Math.round(Math.random()*max); }
function format_rgb(r,g,b) { return 'rgb(' + r + ',' + g + ',' + b + ')'; }
function make_cells(max_x, max_y) {
var cells = [];
for (var i=0; i<max_x; i++) {
cells[i] = [];
for (var j=0; j<max_y; j++) {
cells[i][j] = rand(256);
}
}
return cells;
}
function draw_cells(cells) {
var canvas = $("canvas_element");
if ( !canvas || !canvas.getContext ) {
return false;
}
var ctx = canvas.getContext('2d');
ctx.beginPath();
for (var i=0; i<cells.length; i++) {
for (var j=0; j<cells[i].length; j++) {
var color = format_rgb(cells[i][j], cells[i][j], cells[i][j]);
/* var color;
if (cells[i][j] > 127)
color = format_rgb(255,255,255);
else
color = format_rgb(0,0,0);*/
ctx.strokeStyle = color;
ctx.strokeRect(i, j, 1, 1);
ctx.strokeRect(300 - i, j, 1, 1);
}
}
ctx.closePath();
ctx.stroke();
}
// cell automaton rules
var old_cells = make_cells(150, 300);
function neighborhood_average(cells, cx, cy) {
var xm = (cx-1 + cells.length) % cells.length;
var xp = (cx+1) % cells.length;
var ym = (cy-1 + cells[cx].length) % cells[cx].length;
var yp = (cy+1) % cells[cx].length;
var temp = cells[xm][ym] + cells[cx][ym] + cells[xp][ym] +
cells[xm][cy] + cells[xp][cy] +
cells[xm][yp] + cells[cx][yp] + cells[xp][yp];
return Math.round(temp / 8.0);
}
function step(cells) {
var next = [];
for (var i=0; i<cells.length; i++) {
next[i] = [];
for (var j=0; j<cells[i].length; j++) {
var avg = neighborhood_average(cells, i, j);
next[i][j] = (Math.round((avg + (cells[i][j] - old_cells[i][j]) * 1.6)));
}
}
old_cells = cells;
return next;
}
// onload handler
onload = function() {
var cells = make_cells(150,300);
draw_cells(cells);
setInterval(function() {
cells = step(cells);
draw_cells(cells);
}, 500);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment