Skip to content

Instantly share code, notes, and snippets.

@okaq
Created November 24, 2013 16:36
Show Gist options
  • Save okaq/7629106 to your computer and use it in GitHub Desktop.
Save okaq/7629106 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head id="zeta">
<title>okaq.com</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="author" content="[email protected]" />
<link rel="shortcut icon" href="" />
<style type="text/css">
html,body{width:100%;height:100%;background-color:rgba(255,255,255,1.0);}
#beta{position:absolute;top:16px;left:16px;}
</style>
<script type="text/javascript">
// ok
console.log("ok jan!");
// load
(function() {
var async_load = function() {
console.log("async load");
g.init();
}
window.addEventListener("load", async_load, false);
})();
// game
var g = {
"init": function() {
console.log("g init...");
g.can = document.getElementById("beta");
g.con = g.can.getContext("2d");
g.w = g.can.width;
g.h = g.can.height;
g.bg = c.rand();
g.fg = c.rand();
g.clear();
b.init();
g.id = window.setInterval(g.mc, 100);
},
"clear": function() {
g.con.fillStyle = g.bg;
g.con.fillRect(0,0,g.w,g.h);
},
"mc": function() {
// monte carlo sampling
var x0 = (Math.random() * g.w) >>> 0;
var y0 = (Math.random() * g.h) >>> 0;
var s0 = (x0 + y0 * b.w) >>> 0;
var s1 = (s0 * 4) >>> 0;
// console.log("x0: " + x0 + " y0: " + y0 + " s0: " + s0 + " b.image.data[s1]: " + b.image.data[s1]);
if (b.image.data[s1] == 0) {
g.con.fillStyle = g.fg;
g.con.fillRect(x0, y0, 4, 4);
}
}
}
// search
var s = {
"search": function() {
// pick point at random, add it to visited hash
// instead of random, initialize sampled/unsampled pixel sets
// if empty, pick another random point
// else fill pixel, add to filled and visited hash
// then explore 8 neighboring pixels, candidates
// finished when all pixels visited, len(visited) == len(image.data)
// one hash to rule them all: {"x-y": {visit:true,fill:true}}
// visited set, candidates local set
},
"init": function() {
}
}
// offscreen buffer
var b = {
"init": function() {
console.log("buffer creation init");
b.can = document.createElement("canvas");
b.can.width = g.w;
b.can.height = g.h;
b.con = b.can.getContext("2d");
b.w = b.can.width;
b.h = b.can.height;
b.bg = "rgba(255,255,255,1.0)";
b.fg = "rgba(0,0,0,1.0)";
// debug.iter(b.can);
b.clear();
b.font();
b.draw();
b.data();
},
"clear": function() {
b.con.fillStyle = b.bg;
b.con.fillRect(0,0,b.w,b.h);
},
"font": function() {
b.con.font = "400px Ubuntu, Arial, Helvetica";
b.con.textBaseline = "top";
b.text = u.rand(0x3040, 0x30ff);
console.log("b.text: " + b.text);
},
"draw": function() {
b.con.fillStyle = b.fg;
b.con.fillText(b.text, 56, 56);
},
"data": function() {
b.image = b.con.getImageData(0,0,b.w,b.h)
}
}
// unicode
var u = {
"rand": function(lo, hi) {
// return random unicode escape string within range
// Han characters start at '\u4e00'
var d0 = (hi - lo) >>> 0;
var d1 = ((Math.random() * d0) + lo) >>> 0;
return String.fromCharCode(d1);
}
}
// color
var c = {
"rand": function() {
var c0 = [c.rb(), c.rb(), c.rb(), Math.random()];
var s0 = c0.join(",");
var s1 = "rgba(" + s0 + ")";
return s1;
},
"rb": function() {
return ((Math.random() * 255) >>> 0)
}
}
// debug
var debug = {
"iter": function(o) {
for (key in o) {
console.log(key + ": " + o[key]);
}
}
}
</script>
</head>
<body id="alpha">
<canvas id="beta" width="512" height="512"></canvas>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment