diff options
| author | Jules Laplace <jules@okfoc.us> | 2016-05-11 14:42:27 -0400 |
|---|---|---|
| committer | Jules Laplace <jules@okfoc.us> | 2016-05-11 14:42:27 -0400 |
| commit | 3c5556c0610c4b28e78e012cd21812ff2b484fd8 (patch) | |
| tree | e0ab031e08576b8ef178dd14362a32657539b536 | |
| parent | 93151c4788106e56a53bfd4dfd2f089cadb00c63 (diff) | |
fix undo bug, add rotate scale shader nopaint featurez
| -rw-r--r-- | js/ui/nopaint.js | 152 | ||||
| -rw-r--r-- | js/util.js | 1 |
2 files changed, 133 insertions, 20 deletions
diff --git a/js/ui/nopaint.js b/js/ui/nopaint.js index bb6641c..fc2b5a3 100644 --- a/js/ui/nopaint.js +++ b/js/ui/nopaint.js @@ -4,7 +4,7 @@ var nopaint = (function(){ controls.no = new Tool (nopaint_no_el) controls.no.use = function(state){ - nopaint.undo() + undo.undo() controls.paint.focus() } @@ -41,14 +41,14 @@ var nopaint = (function(){ document.body.classList.toggle("nopaint", state) return state } - nopaint.undo = function(){ + nopaint.no = function(){ undo.undo() nopaint.play() } controls.paint.lex.raw_key = nopaint.raw_key = keys.left_right_key(function(n){ if (! nopaint.timeout) return if (n > 0) nopaint.play() - else if (n < 0) nopaint.undo() + else if (n < 0) nopaint.no() else nopaint.pause() }) nopaint.pause = nopaint.blur = function(){ @@ -75,6 +75,7 @@ var nopaint = (function(){ last_tool && last_tool.finish() nopaint.tool = nopaint.get_random_tool() nopaint.tool.start( last_tool ) + console.log("> %s", nopaint.tool.type) } nopaint.add_tool = function(fn){ nopaint.tools[fn.type] = fn @@ -372,44 +373,154 @@ var nopaint = (function(){ }, }) - /* Slide Tools */ + /* Shader Tools */ - var SlideTool = NopaintTool.extend({ - type: "slide", + var ShaderTool = NopaintTool.extend({ + type: "shader", dx: 0, dy: 0, speed: 5, + is_recursive: false, start: function(){ undo.save_rect(0, 0, canvas.w, canvas.h) this.speed = floor(randrange(1, 3)) this.canvas = canvas.clone() - this.dx = randint(3)-1 - this.dy = randint(3)-1 - if (! this.dx && ! this.dy) { - this.dx = 1 - this.dy = 0 - } }, paint: function(t){ if ((t % this.speed) == 0) { var w = canvas.w var h = canvas.h - var i = this.dx - var j = this.dy - this.canvas.assign(canvas) + var lex + if (this.is_recursive) { + this.canvas.assign(canvas) + } + this.before_shade() for (var x = 0; x < w; x++) { for (var y = 0; y < h; y++) { - var lex = this.canvas.get(x+i, y+j) - canvas.get(x, y).assign(lex) + lex = canvas.get(x, y) + if (! this.shade( this.canvas, canvas, lex, x, y, w, h )) { + lex.build() + } } } } }, + before_shade: function(){}, + shade: function(src, dest, lex, x, y, w, h){}, finish: function(){ this.canvas.demolish() } }) + + var TranslateTool = ShaderTool.extend({ + type: "translate", + dx: 0, + dy: 0, + speed: 5, + start: function(){ + this.__start() + this.dx = randint(3)-1 + this.dy = randint(3)-1 + this.x = this.y = 0 + if (! this.dx && ! this.dy) { + this.dx = 1 + this.dy = 0 + } + }, + before_shade: function(){ + this.x += this.dx + this.y += this.dy + }, + shade: function(src, dest, lex, x, y, w, h){ + var copy = src.get(x+this.x, y+this.y) + lex.assign(copy) + return true + }, + }) + var ScaleTool = ShaderTool.extend({ + type: "scale", + scale: 1, + dscale: 0, + speed: 5, + start: function(){ + this.__start() + var sign = randsign() + this.x_scale = 1 + this.y_scale = 1 + this.dx_scale = randsign() * randrange(0.0005, 0.01) + var r = Math.random() + if (r < 0.333) { + this.dy_scale = this.dx_scale * randrange(0.85, 1.25) + } + else if (r < 0.666) { + this.dy_scale = this.dx_scale + } + else { + this.dy_scale = randsign() * randrange(0.0005, 0.01) + } + }, + before_shade: function(){ + this.x_scale += this.dx_scale + this.y_scale += this.dy_scale + }, + shade: function(src, dest, lex, x, y, w, h){ + x = (x/w) * 2 - 1 + y = (y/h) * 2 - 1 + x *= this.x_scale + y *= this.y_scale + x = (x + 1) / 2 * w + y = (y + 1) / 2 * h + var copy = src.get(x, y) + lex.assign(copy) + return true + }, + }) + + var RotateTool = ShaderTool.extend({ + type: "rotate", + theta: 0, + d_theta: 0, + start: function(){ + this.__start() + var sign = randsign() + this.theta = 0 + this.d_theta = randsign() * randrange(0.001, 0.05) + console.log(this.d_theta) + }, + before_shade: function(){ + this.theta += this.d_theta + }, + shade: function(src, dest, lex, x, y, w, h){ + x = (x/w) * 2 - 1 + y = (y/h) * 2 - 1 + var ca = cos(this.theta) + var sa = sin(this.theta) + var a = x * ca - y * sa + var b = x * sa + y * ca + x = (a + 1) / 2 * w + y = (b + 1) / 2 * h + var copy = src.get(x, y) + lex.assign(copy) + return true + }, + }) + + var CycleTool = ShaderTool.extend({ + type: "cycle", + n: 0, + speed: 30, + is_recursive: true, + start: function(){ + this.__start() + this.n = randsign() + }, + shade: function(src, dest, lex, x, y){ + lex.bg += this.n + return false + }, + }) + nopaint.add_tool( new SolidBrush({ weight: 4 }) ) nopaint.add_tool( new EraseBrush({ weight: 6 }) ) nopaint.add_tool( new RandomBrush({ weight: 4 }) ) @@ -419,8 +530,11 @@ var nopaint = (function(){ nopaint.add_tool( new CloneBrush({ weight: 10 }) ) nopaint.add_tool( new FillTool({ weight: 4 }) ) nopaint.add_tool( new FillLetterTool({ weight: 6 }) ) - nopaint.add_tool( new StarsTool({ weight: 6 }) ) - nopaint.add_tool( new SlideTool({ weight: 5 }) ) + nopaint.add_tool( new StarsTool({ weight: 3 }) ) + nopaint.add_tool( new TranslateTool({ weight: 5 }) ) + nopaint.add_tool( new CycleTool({ weight: 2 }) ) + nopaint.add_tool( new ScaleTool({ weight: 4 }) ) + nopaint.add_tool( new RotateTool({ weight: 4 }) ) nopaint.regenerate_weights() nopaint.toggle(true) @@ -52,7 +52,6 @@ function xrandom(exp){ return Math.pow(Math.random(), exp) } function xrand(exp,n){ return (xrandom(exp)*n) } function xrandint(exp,n){ return rand(exp,n)|0 } function xrandrange(exp,a,b){ return a + xrand(exp,b-a) } -function xrandsign(){ return xrandom() >= 0.5 ? -1 : 1 } function choice(a){ return a[randint(a.length)] } function deg(n){ return n*180/PI } |
