summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJules Laplace <jules@okfoc.us>2016-05-10 19:39:27 -0400
committerJules Laplace <jules@okfoc.us>2016-05-10 19:39:27 -0400
commit896da613d53eedf7dbcb433a1a2eb3553c401a71 (patch)
treeb27b5ea03905bdf9a05c48cc98e1a1eb8cfa59fa
parent7119c8164dcb2c61928b5ac86b086df535bccbf6 (diff)
basic brushes
-rw-r--r--js/color.js1
-rw-r--r--js/nopaint/index.js120
-rw-r--r--js/util.js16
-rw-r--r--nopaint.html1
4 files changed, 114 insertions, 24 deletions
diff --git a/js/color.js b/js/color.js
index a4299b1..23fb13f 100644
--- a/js/color.js
+++ b/js/color.js
@@ -32,6 +32,7 @@ function mirc_color_reverse (n) { return mod(-(n+1), 16)|0 }
function all_hue (n) { return colors[all_color_hue_order[mod(n, 16)|0]] }
function all_inv_hue (n) { return colors[all_color_inv_order[mod(n, 16)|0]] }
function hue (n) { return colors[color_hue_order[mod(n, 11)|0]] }
+function rand_hue () { return colors[color_hue_order[randint(11)]] }
function inv_hue (n) { return colors[color_inv_order[mod(n, 11)|0]] }
function gray (n) { return colors[gray_names[mod(n, 4)|0]] }
function fire (n) { return colors[fire_names[mod(n, 7)|0]] }
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
})()
diff --git a/js/util.js b/js/util.js
index 3775ba0..d0d616d 100644
--- a/js/util.js
+++ b/js/util.js
@@ -47,6 +47,13 @@ function rand(n){ return (Math.random()*n) }
function randint(n){ return rand(n)|0 }
function randrange(a,b){ return a + rand(b-a) }
function randsign(){ return random() >= 0.5 ? -1 : 1 }
+
+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 }
function rad(n){ return n*PI/180 }
@@ -74,7 +81,6 @@ function step(a, b){
// ^^ bool -> int
}
-
function julestep (a,b,n) {
return clamp(norm(n,a,b), 0.0, 1.0);
}
@@ -160,6 +166,14 @@ d=this.apply(a,arguments))===e?a:d}.bind(d):d;a.init&&a.init.apply(a,arguments)
]!==e&&(d["__"+c]=b[c]);return a(d)},f},typeof module=="object"&&(module.exports
=Model); // c-{{{-<
+function defaults (dest, src) {
+ dest = dest || {}
+ for (var i in src) {
+ dest[i] = typeof dest[i] == 'undefined' ? src[i] : dest[i]
+ }
+ return dest
+}
+
// Naive useragent detection pattern
var is_iphone = (navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))
var is_ipad = (navigator.userAgent.match(/iPad/i))
diff --git a/nopaint.html b/nopaint.html
index 504ea49..6ce29d2 100644
--- a/nopaint.html
+++ b/nopaint.html
@@ -39,6 +39,7 @@
<br>
<span id="nopaint_no_el" class="tool">no</span><br>
<span id="nopaint_paint_el" class="tool">paint</span><br>
+ <span id="nopaint_pause_el" class="tool">pause</span><br>
<br>
<span id="grid_el" class="tool">_ grid</span><br>
<span id="rotate_checkbox" class="tool">_ rotate</span><br>