var nopaint = (function(){ var is_paint = false controls.nopaint = {} controls.no = new Tool (nopaint_no_el) controls.no.use = function(state){ undo.undo() controls.paint.focus() } controls.paint = new Tool (nopaint_paint_el) controls.paint.use = function(state){ nopaint.play() } controls.nopaint_pause = new Tool (nopaint_pause_el) controls.nopaint_pause.use = function(state){ nopaint.pause() } // use own stepwise clock to drive tweens oktween.raf = function(){} var nopaint = {} nopaint.delay = 100 nopaint.tool = null nopaint.tools = {} nopaint.keys = [] nopaint.weights = [] nopaint.step = 0 nopaint.time = 0 nopaint.timeout = false nopaint.undo = function(){ undo.undo() } nopaint.pause = function(){ clearTimeout(nopaint.timeout) nopaint.timeout = 0 nopaint.step = 0 } nopaint.play = function(){ nopaint.pause() nopaint.switch_tool() nopaint.go() } nopaint.go = function(){ nopaint.timeout = setTimeout(nopaint.go, nopaint.delay) oktween.update(nopaint.time) nopaint.tool.paint( nopaint.step ) nopaint.time += 1 nopaint.step += 1 } nopaint.switch_tool = function(){ undo.new() last_tool = nopaint.tool last_tool && last_tool.finish() nopaint.tool = nopaint.get_random_tool() nopaint.tool.start( last_tool ) console.log(nopaint.tool.type) } nopaint.add_tool = function(fn){ nopaint.tools[fn.type] = fn } nopaint.get_random_tool = function(){ var n = rand( nopaint.sum ) for (var i = 0, _len = nopaint.weights.length; i < _len; i++) { if (n < nopaint.weights[i]) { return nopaint.tools[ nopaint.keys[i] ] } } return nopaint.tools[ choice(nopaint.keys) ] } nopaint.regenerate_weights = function(){ nopaint.sum = 0 nopaint.weights = [] nopaint.keys = Object.keys( nopaint.tools ).sort(function(a,b){ return b-a }) nopaint.keys.forEach(function(key){ nopaint.sum += nopaint.tools[key].opt.weight nopaint.weights.push( nopaint.sum ) }) } var NopaintTool = Model({ type: "none", init: function(opt){ this.opt = opt || {} }, start: function(){}, paint: function(t){}, finish: function(){}, }) var NopaintBrush = NopaintTool.extend({ type: "brush", is_brush: true, init: function(opt){ this.opt = opt || {} this.p = {x: randint(canvas.w), y: randint(canvas.h)} this.fg = 0 this.bg = 1 this.char = " " this.tweens = [] }, start: function(last_brush){ this.reset( last_brush ) brush.load( this ) brush.generate() draw.down({}, null, [this.p.x, this.p.y]) }, paint: function(t){ this.update( t ) draw.move_toroidal({}, null, [this.p.x, this.p.y]) }, finish: function(){ this.tweens.forEach(function(t){ t.cancel() }) this.tweens = [] }, reorient: function(last_brush){ var a = {}, b if (last_brush) { this.p.x = a.x = randint(canvas.w) this.p.y = a.y = randint(canvas.h) } else { a.x = this.p.x a.y = this.p.y } b = this.get_next_point() var tween = oktween.add({ obj: this.p, from: a, to: b, duration: b.duration, easing: b.easing, update: b.update, finished: function(){ this.iterate() this.regenerate() }.bind(this) }) this.tweens.push(tween) }, }) var easings = "linear circ_out circ_in circ_in_out quad_in quad_out quad_in_out".split(" ") var SolidBrush = NopaintBrush.extend({ type: "solid", get_next_point: function(){ var radius = randrange(2, this.opt.max_radius) var b = {} b.duration = randrange(1, 7) b.easing = choice(easings) b.x = this.p.x + randrange(-radius, radius) b.y = this.p.y + randrange(-radius, radius) return b }, recolor: function(){ this.fg = this.bg = randint(16) this.char = " " this.regenerate() }, randomize: function(){ this.opt.max_radius = randrange(3,10) var bw = xrandrange(5, 1, 3) brush.resize( round(bw * randrange(0.9, 1.8)), round(bw) ) }, reset: function( last_brush ){ brush.resize(1,1) this.randomize() this.reorient( last_brush ) this.recolor( last_brush ) }, iterate: function( last_brush ){ this.randomize() this.reorient( last_brush ) }, regenerate: function(){ brush.load( this ) brush.generate() }, update: function(t){ } }) var RandomBrush = SolidBrush.extend({ type: "random", iterate: function( last_brush ){ this.reorient( last_brush ) this.recolor( last_brush ) }, }) var HueBrush = SolidBrush.extend({ type: "hue", recolor: function(){ this.fg = this.bg = rand_hue() this.char = " " this.regenerate() }, }) var LetterBrush = SolidBrush.extend({ type: "letter", chars: unicode.block('Basic Latin', 32), recolor: function(){ this.fg = rand_hue() this.bg = rand_hue() this.char = choice(this.chars) this.regenerate() }, }) nopaint.add_tool( new SolidBrush({ weight: 1 }) ) nopaint.add_tool( new RandomBrush({ weight: 2 }) ) nopaint.add_tool( new HueBrush({ weight: 4 }) ) nopaint.add_tool( new LetterBrush({ weight: 4 }) ) nopaint.regenerate_weights() return nopaint })()