summaryrefslogtreecommitdiff
path: root/js/draw.js
blob: eb58a270ee61ec6f2bc79dff13e72e73d793b4ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
var draw = (function(){

  var last_point = [0,0]
  
  function down (e, lex, point) {
    erasing = (e.which == "3" || e.ctrlKey)
    changed = true
    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 set_last_point (e, point) {
    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 move_toroidal (e, lex, point) {
    var w = canvas.w, h = canvas.h
    var src_x_quantile = quantile( last_point[0], w )
    var src_y_quantile = quantile( last_point[1], h )
    var dst_x_quantile = quantile( point[0], w )
    var dst_y_quantile = quantile( point[1], h )
    var src_x_mod = mod( last_point[0], w )
    var src_y_mod = mod( last_point[1], h )
    var dst_x_mod = mod( point[0], w )
    var dst_y_mod = mod( point[1], h )
    // if we've moved across the edge of the board, draw two lines
    if (src_x_quantile != dst_x_quantile || src_y_quantile != dst_y_quantile) {
      var xa, ya
      if (src_x_quantile < dst_x_quantile) {
        xa = [
          [src_x_mod, dst_x_mod + w],
          [src_x_mod-w, dst_x_mod],
        ]
      }
      else if (src_x_quantile == dst_x_quantile) {
        xa = [
          [src_x_mod, dst_x_mod],
          [src_x_mod, dst_x_mod],
        ]
      }
      else {
        xa = [
          [src_x_mod, dst_x_mod-w],
          [src_x_mod+w, dst_x_mod],
        ]
      }

      if (src_y_quantile < dst_y_quantile) {
        ya = [
          [src_y_mod, dst_y_mod + h],
          [src_y_mod-h, dst_y_mod],
        ]
      }
      else if (src_y_quantile == dst_y_quantile) {
        ya = [
          [src_y_mod, dst_y_mod],
          [src_y_mod, dst_y_mod],
        ]
      }
      else {
        ya = [
          [src_y_mod, dst_y_mod-h],
          [src_y_mod+h, dst_y_mod],
        ]
      }
      line(lex, [ xa[0][0], ya[0][0] ], [ xa[0][1], ya[0][1] ], erasing)
      line(lex, [ xa[1][0], ya[1][0] ], [ xa[1][1], ya[1][1] ], erasing)
    }
    else {
      var last_point_mod = [], point_mod = []
      last_point_mod[0] = mod( last_point[0], w )
      last_point_mod[1] = mod( last_point[1], h )
      point_mod[0] = mod( point[0], w )
      point_mod[1] = mod( point[1], h )
      line(lex, last_point_mod, point_mod, erasing)
    }
    last_point[0] = point[0]
    last_point[1] = point[1]
    // y = point.y
  }
  
  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 stamp (canvas, brush, x, y, erasing) {
    var 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 (lex.opacity === 0 && lex.char === ' ') return;
        var aa = canvas.aa[t][s]
        undo.save_lex(s, t, aa)
        if (erasing) {
          aa.erase(lex)
        }
        else {
          aa.stamp(lex, brush)
        }
      }
    })
  }
  
  function fill (lex, x, y) {
    var q = [ [x,y] ]
    var aa = canvas.aa
    var target = aa[y][x].clone()
    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 (aa[n[1]][n[0]].ne(target)) {
        continue LOOP
      }
      w = e = n[0]
      j = n[1]
      WEST: while (w > 0) {
        if (aa[j][w-1].eq(target)) {
          w = w-1
        }
        else {
          break WEST
        }
      }
      EAST: while (e < canvas.w-1) {
        if (aa[j][e+1].eq(target)) {
          e = e+1
        }
        else {
          break EAST
        }
      }
      for (var i = w; i <= e; i++) {
        undo.save_lex(i, j, aa[j][i])
        aa[j][i].assign(lex)
        if (j > 0 && aa[j-1][i].eq(target)) {
          q.push([ i, j-1 ])
        }
        if (j < canvas.h-1 && aa[j+1][i].eq(target)) {
          q.push([ i, j+1 ])
        }
      }
    }
  }

  var draw = {}
  draw.down = down
  draw.set_last_point = set_last_point
  draw.move = move
  draw.move_toroidal = move_toroidal
  draw.stamp = stamp
  draw.line = line
  draw.point = point
  draw.fill = fill
  return draw

})()