diff options
| author | Jules Laplace <jules@okfoc.us> | 2016-05-10 18:37:50 -0400 |
|---|---|---|
| committer | Jules Laplace <jules@okfoc.us> | 2016-05-10 18:37:50 -0400 |
| commit | 7119c8164dcb2c61928b5ac86b086df535bccbf6 (patch) | |
| tree | 5e399a8bdb0f22c40c6d0598dae58801ae0ea4ba /js/nopaint/index.js | |
| parent | 17f2644e3d885c8aef170023c84933e5bd888402 (diff) | |
nopaint v1..
Diffstat (limited to 'js/nopaint/index.js')
| -rw-r--r-- | js/nopaint/index.js | 165 |
1 files changed, 165 insertions, 0 deletions
diff --git a/js/nopaint/index.js b/js/nopaint/index.js new file mode 100644 index 0000000..6270503 --- /dev/null +++ b/js/nopaint/index.js @@ -0,0 +1,165 @@ +var nopaint = (function(){ + + var is_paint = false + + controls.nopaint = {} + + controls.no = new Checkbox (nopaint_no_el) + controls.no.use = function(state){ + this.update(! nopaint.timeout) + controls.paint.update(false) + undo.undo() + nopaint.play() + } + + controls.paint = new Checkbox (nopaint_paint_el) + controls.paint.use = function(state){ + controls.no.update(false) + nopaint.play() + this.update(!! nopaint.timeout) + } + + // use own stepwise clock to drive tweens + oktween.raf = function(){} + + var nopaint = {} + nopaint.delay = 100 + nopaint.tool = null + nopaint.tools = {} + 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.tools[ choice(Object.keys(nopaint.tools)) ] + nopaint.tool.start( last_tool ) + // console.log(nopaint.tool.type) + } + nopaint.add_tool = function(fn){ + nopaint.tools[fn.type] = fn + } + + var NopaintTool = Model({ + type: "none", + init: function(){}, + start: function(){}, + paint: function(t){}, + finish: function(){}, + }) + + var NopaintBrush = NopaintTool.extend({ + type: "brush", + is_brush: true, + init: function(){ + this.p = {x: 0, y: 0} + this.fg = 0 + this.bg = 1 + this.char = " " + this.tweens = [] + }, + + 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]) + }, + + 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: function(o,dt){ + // console.log(o,dt) + }, + finished: function(){ + this.reorient() + }.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 b = {} + 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) + return b + }, + + recolor: function(){ + this.fg = this.bg = randint(16) + this.char = " " + }, + reset: function(){ + brush.resize(1,1) + }, + iterate: function( last_brush ){ + this.reorient( last_brush ) + this.recolor( last_brush ) + }, + update: function(t){ + } + }) + + nopaint.add_tool( new SolidBrush ) + +})() |
