diff options
Diffstat (limited to 'js/nopaint/index.js')
| -rw-r--r-- | js/nopaint/index.js | 120 |
1 files changed, 97 insertions, 23 deletions
diff --git a/js/nopaint/index.js b/js/nopaint/index.js index 6270503..413306a 100644 --- a/js/nopaint/index.js +++ b/js/nopaint/index.js @@ -4,19 +4,20 @@ var nopaint = (function(){ controls.nopaint = {} - controls.no = new Checkbox (nopaint_no_el) + controls.no = new Tool (nopaint_no_el) controls.no.use = function(state){ - this.update(! nopaint.timeout) - controls.paint.update(false) undo.undo() - nopaint.play() + controls.paint.focus() } - controls.paint = new Checkbox (nopaint_paint_el) + controls.paint = new Tool (nopaint_paint_el) controls.paint.use = function(state){ - controls.no.update(false) nopaint.play() - this.update(!! nopaint.timeout) + } + + controls.nopaint_pause = new Tool (nopaint_pause_el) + controls.nopaint_pause.use = function(state){ + nopaint.pause() } // use own stepwise clock to drive tweens @@ -26,6 +27,8 @@ var nopaint = (function(){ nopaint.delay = 100 nopaint.tool = null nopaint.tools = {} + nopaint.keys = [] + nopaint.weights = [] nopaint.step = 0 nopaint.time = 0 nopaint.timeout = false @@ -53,17 +56,37 @@ var nopaint = (function(){ undo.new() last_tool = nopaint.tool last_tool && last_tool.finish() - nopaint.tool = nopaint.tools[ choice(Object.keys(nopaint.tools)) ] + nopaint.tool = nopaint.get_random_tool() nopaint.tool.start( last_tool ) - // console.log(nopaint.tool.type) + 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(){}, + init: function(opt){ + this.opt = opt || {} + }, start: function(){}, paint: function(t){}, finish: function(){}, @@ -72,8 +95,9 @@ var nopaint = (function(){ var NopaintBrush = NopaintTool.extend({ type: "brush", is_brush: true, - init: function(){ - this.p = {x: 0, y: 0} + init: function(opt){ + this.opt = opt || {} + this.p = {x: randint(canvas.w), y: randint(canvas.h)} this.fg = 0 this.bg = 1 this.char = " " @@ -82,7 +106,6 @@ var nopaint = (function(){ start: function(last_brush){ this.reset( last_brush ) - this.iterate( last_brush ) brush.load( this ) brush.generate() draw.down({}, null, [this.p.x, this.p.y]) @@ -118,11 +141,10 @@ var nopaint = (function(){ to: b, duration: b.duration, easing: b.easing, - update: function(o,dt){ - // console.log(o,dt) - }, + update: b.update, finished: function(){ - this.reorient() + this.iterate() + this.regenerate() }.bind(this) }) this.tweens.push(tween) @@ -137,29 +159,81 @@ var nopaint = (function(){ type: "solid", get_next_point: function(){ + var radius = randrange(2, this.opt.max_radius) var b = {} - b.duration = randrange(1,7) + b.duration = randrange(1, 7) b.easing = choice(easings) - b.x = this.p.x + randrange(-10, 10) - b.y = this.p.y + randrange(-10, 10) + 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() }, - reset: function(){ + + 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 ) - this.recolor( last_brush ) }, + + regenerate: function(){ + brush.load( this ) + brush.generate() + }, + update: function(t){ } }) - nopaint.add_tool( new SolidBrush ) + 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 })() |
