diff options
Diffstat (limited to 'js/draw.js')
| -rw-r--r-- | js/draw.js | 128 |
1 files changed, 101 insertions, 27 deletions
@@ -1,32 +1,106 @@ +var draw = (function(){ -function draw (lex, x, y, erasing) { - stamp (canvas, brush, x, y, erasing) -} + var last_point = [0,0] + + function down (e, lex, point) { + erasing = (e.which == "3" || e.ctrlKey) + if (e.shiftKey) { + line (lex, last_point, point, erasing) + } + else { + stamp (canvas, brush, point[0], point[1], erasing) + } + last_point[0] = point[0] + last_point[1] = point[1] + } + + function move (e, lex, point) { + line(lex, last_point, point, erasing) + last_point[0] = point[0] + last_point[1] = point[1] + } + + function point (lex, x, y, erasing) { + stamp (canvas, brush, x, y, erasing) + } -function line (lex, a, b, erasing) { - var len = dist(a[0], a[1], b[0], b[1]) - var bw = 1 - var x, y, i; - for (var i = 0; i < len; i += bw) { - x = lerp(i / len, a[0], b[0]) - y = lerp(i / len, a[1], b[1]) - stamp (canvas, brush, x, y, erasing) - } -} + function line (lex, a, b, erasing) { + var len = dist(a[0], a[1], b[0], b[1]) + var bw = 1 + var x, y, i; + for (var i = 0; i <= len; i += bw) { + x = lerp(i / len, a[0], b[0]) + y = lerp(i / len, a[1], b[1]) + stamp (canvas, brush, x, y, erasing) + } + } -function stamp (canvas, brush, x, y, erasing) { - hh = brush.w/2|0 - brush.forEach(function(lex, s, t){ - s = round( s + x-hh ) - t = round( t + y-hh ) - if (s >= 0 && s < canvas.w && t >= 0 && t < canvas.h) { - if (erasing) { - canvas.aa[t][s].erase(lex) - } - else { - canvas.aa[t][s].clone(lex) - } + function stamp (canvas, brush, x, y, erasing) { + hh = brush.w/2|0 + brush.forEach(function(lex, s, t){ + s = round( s + x-hh ) + t = round( t + y-hh ) + if (lex.opacity > 0 && s >= 0 && s < canvas.w && t >= 0 && t < canvas.h) { + if (erasing) { + canvas.aa[t][s].erase(lex) + } + else { + canvas.aa[t][s].clone(lex) + } + } + }) + } + + function fill (lex, x, y) { + var q = [ [x,y] ] + var target = canvas.aa[y][x].copy() + var n, w = 0, e = 0, j = 0 + var kk = 0 + // gets into a weird infinite loop if we don't break here.. :\ + if (target.eq(lex)) { return } + LOOP: while (q.length) { + n = q.shift() + if (canvas.aa[n[1]][n[0]].ne(target)) { + continue LOOP + } + w = e = n[0] + j = n[1] + WEST: while (w > 0) { + if (canvas.aa[j][w-1].eq(target)) { + w = w-1 + } + else { + break WEST + } + } + EAST: while (e < canvas.w-1) { + if (canvas.aa[j][e+1].eq(target)) { + e = e+1 + } + else { + break EAST + } + } + for (var i = w; i <= e; i++) { + canvas.aa[j][i].clone(lex) + if (j > 0 && canvas.aa[j-1][i].eq(target)) { + q.push([ i, j-1 ]) + } + if (j < canvas.h-1 && canvas.aa[j+1][i].eq(target)) { + q.push([ i, j+1 ]) + } + } } - }) -} + } + + var draw = {} + draw.down = down + draw.move = move + draw.stamp = stamp + draw.line = line + draw.point = point + draw.fill = fill + return draw + +})() |
