summaryrefslogtreecommitdiff
path: root/js/ui
diff options
context:
space:
mode:
Diffstat (limited to 'js/ui')
-rw-r--r--js/ui/evolver.js199
-rw-r--r--js/ui/nopaint.js17
2 files changed, 214 insertions, 2 deletions
diff --git a/js/ui/evolver.js b/js/ui/evolver.js
new file mode 100644
index 0000000..e65df20
--- /dev/null
+++ b/js/ui/evolver.js
@@ -0,0 +1,199 @@
+var evolver = (function(){
+
+ setTimeout(init)
+
+ var hash = window.location.hash
+ var opt = {
+ src: "img/sistene-chapel.jpg",
+ w: 40,
+ h: 16,
+ }
+ if (hash.length) {
+ hash.split("#").forEach(function(s){
+ var kv = s.split("=")
+ if (opt.hasOwnProperty(kv[0])) {
+ opt[kv[0]] = kv[1]
+ }
+ })
+ }
+
+ var target, last_score = 0
+ var clones = [], clone_count = 10, strokes_per_iteration = 1
+ var clone_index = 0
+ var main_canvas
+
+ var compare_w = 64, compare_h = 64
+
+ var target = document.createElement('canvas')
+ target.width = compare_w
+ target.height = compare_h
+ var t_ctx = target.getContext('2d')
+
+ var compare = document.createElement('canvas')
+ compare.width = compare_w
+ compare.height = compare_h
+ var c_ctx = compare.getContext('2d')
+
+
+ brush.rebuild = function(){
+ this.initialize()
+ }
+
+ function init () {
+ nopaint.debug = false
+
+ main_canvas = canvas
+ canvas.resize(opt.w, opt.h)
+// canvas.forEach(function(lex,x,y){
+// lex.bg = randint(16)
+// })
+ for (var i = 0; i < clone_count; i++) {
+ clones[i] = {
+ el: document.createElement("div"),
+ canvas: canvas.clone(),
+ score: 0,
+ hash: -1,
+ }
+// clones[i].canvas.append(clones[i].el)
+// clones[i].el.className = "rapper"
+// document.getElementById("clones").appendChild( clones[i].el )
+ clones[i].canvas.forEach(function(lex){
+ lex.build = noop
+ })
+ }
+ load(opt.src, go)
+ }
+ function load(src, fn){
+ var img = new Image ()
+ img.onload = function(){
+ target = drawImage(t_ctx, img)
+ last_score = 0
+ go()
+ }
+ if (opt.src.match(/^http/)) {
+ opt.src = "http://asdf.us/cgi-bin/proxy?" + opt.src
+ }
+ img.src = src
+ }
+ function drawImage (ctx, img) {
+ ctx.drawImage(img, 0, 0, compare_w, compare_h)
+ return { width: compare_w, height: compare_h, data: ctx.getImageData(0,0,compare_w,compare_h).data, channels: 4 }
+ }
+ function go () {
+ if (evolver.paused) return
+ clone_index = 0
+ paint_next()
+ }
+
+ function paint_next () {
+ canvas = clones[clone_index].canvas
+ for (var i = 0; i < strokes_per_iteration; i++) {
+ nopaint.paint()
+ }
+ render(canvas, function(c){
+ compare = drawImage(c_ctx, c)
+
+ clones[clone_index].score = ImageSSIM.compare(target, compare).ssim
+ if (++clone_index == clone_count) {
+ fitness()
+ changed = false
+ requestAnimationFrame(go)
+ }
+ else {
+ paint_next()
+ }
+ })
+ }
+ function fitness () {
+ clones.sort(function(a,b){ return b.score - a.score })
+
+ var best_clone, next_best_clone, third_best_clone
+ var clones_to_keep
+
+ var max_score = clones[0].score
+
+// console.log(clones.filter(function(c,i){ return i < 10 }).map(function(c){ return c.score.toFixed(2) }).join(" "))
+
+ if (max_score < last_score) {
+ console.log("no improvement [%s] [%s]", max_score.toFixed(3), last_score.toFixed(3))
+ clones_to_keep = 2
+ best_clone = main_canvas
+ next_best_clone = clones[0].canvas
+ third_best_clone = clones[1].canvas
+ }
+ else {
+ last_score = max_score
+
+ clones_to_keep = 3
+ best_clone = clones[0].canvas
+ next_best_clone = clones[1].canvas
+ third_best_clone = clones[2].canvas
+
+ console.log("top clone [%s]", max_score.toFixed(5))
+ main_canvas.forEach(function(lex,x,y){
+ lex.assign( best_clone.getCell(x,y) )
+ })
+ }
+
+ var best_clone = clones[0].canvas
+ clones.forEach(function(clone, i){
+ if (i < clones_to_keep) return
+ var clone_to_copy
+ if (i < clones.length / 4) {
+ clone_to_copy = third_best_clone
+ }
+ else if (i < clones.length / 2) {
+ clone_to_copy = next_best_clone
+ }
+ else {
+ clone_to_copy = best_clone
+ }
+ clone.canvas.forEach(function(lex,x,y) {
+ lex.assign( clone_to_copy.getCell(x,y) )
+ })
+ })
+
+ }
+
+/*
+ function check () {
+ clipboard.export_canvas(function(canvas){
+ var hash = simi.hash(canvas)
+ var score = simi.compare(hash, target)
+ if (score > last_score) {
+ last_score = score
+ console.log(score.toFixed(3), "PAINT")
+ nopaint.paint()
+ }
+ else {
+ console.log(score.toFixed(3), "NO")
+ nopaint.no()
+ }
+ requestAnimationFrame(check)
+ })
+ }
+*/
+
+ var buffer_canvas = document.createElement('canvas')
+
+ function render (canvas, done_fn) {
+ var opts = {
+ palette: 'mirc',
+ font: 'fixedsys_8x15',
+ fg: 0,
+ bg: 1,
+ canvas: buffer_canvas
+ }
+ opts.done = function(c){
+ done_fn(c)
+ }
+ colorcode.to_canvas(canvas.mirc(), opts)
+ }
+
+ return {
+ paused: false,
+ pause: function(){
+ evolver.paused = ! evolver.paused
+ }
+ }
+})() \ No newline at end of file
diff --git a/js/ui/nopaint.js b/js/ui/nopaint.js
index bf94bdc..d3741a9 100644
--- a/js/ui/nopaint.js
+++ b/js/ui/nopaint.js
@@ -33,6 +33,7 @@ var nopaint = (function(){
oktween.raf = function(){}
var nopaint = {}
+ nopaint.debug = true
nopaint.delay = nopaint.normal_delay = 100
nopaint.turbo_delay = 0
nopaint.tool = null
@@ -83,7 +84,7 @@ var nopaint = (function(){
last_tool && last_tool.finish()
nopaint.tool = nopaint.get_random_tool( last_tool )
nopaint.tool.start( last_tool )
- console.log("> %s", nopaint.tool.type)
+ nopaint.debug && console.log("> %s", nopaint.tool.type)
}
nopaint.add_tool = function(fn){
nopaint.tools[fn.type] = fn
@@ -143,6 +144,9 @@ var nopaint = (function(){
upload_interval: 100,
step: 0,
timeout: null,
+ delay: function(){
+ return nopaint.is_turbo ? randrange(150, 300) : randrange(400, 800)
+ },
reset: function(){
this.no_count = 0
this.paint_count = 0
@@ -153,7 +157,7 @@ var nopaint = (function(){
},
play: function(){
clearTimeout(this.timeout)
- var delay = nopaint.is_turbo ? randrange(150, 300) : randrange(400, 800)
+ var delay = this.delay()
this.timeout = setTimeout(this.play.bind(this), delay)
this.check_fitness()
this.step += 1
@@ -451,6 +455,14 @@ var nopaint = (function(){
},
})
+ var GrayBrush = SolidBrush.extend({
+ type: "hue",
+ recolor: function(){
+ this.fg = this.bg = rand_gray()
+ this.char = " "
+ },
+ })
+
var LetterBrush = SolidBrush.extend({
type: "letter",
recolor: function(){
@@ -742,6 +754,7 @@ var nopaint = (function(){
nopaint.add_tool( new EraseBrush({ weight: 5 }) )
nopaint.add_tool( new RandomBrush({ weight: 4 }) )
nopaint.add_tool( new HueBrush({ weight: 5 }) )
+ nopaint.add_tool( new GrayBrush({ weight: 5 }) )
nopaint.add_tool( new LetterBrush({ weight: 2 }) )
nopaint.add_tool( new RandomLetterBrush({ weight: 12 }) )
nopaint.add_tool( new CloneBrush({ weight: 8 }) )