summaryrefslogtreecommitdiff
path: root/js/ui/canvas.js
blob: 67250b82f5d0e42034a7e45c9c73a5363a5a317a (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
var canvas = current_canvas = (function(){

  var cols = 100
  var rows = 24

  var exports = new Matrix (cols, rows, function(x,y){
    var lex = new Lex (x,y)
    lex.build()
    return lex
  })

  exports.bind = function(){

    exports.forEach(function(lex, x, y){

      if (lex.bound) return
      lex.bound = true
      var point = [x,y]
      lex.span.addEventListener('contextmenu', function(e){
        e.preventDefault()
      })
      lex.span.addEventListener('mousedown', function(e){
        e.preventDefault()
        dragging = true
        current_canvas = canvas
        if (drawing) {
          draw.down(e, lex, point)
        }
        else if (selecting) {
          selection.down(e, lex, point)
        }
        else if (filling) {
          draw.fill(brush, x, y)
        }
        lex.focus()
      })
      lex.span.addEventListener("mousemove", function(e){
        if (! dragging) return
        if (drawing) {
          draw.move(e, lex, point)
        }
        else if (selecting) {
          selection.move(e, lex, point)
        }
        lex.focus()
      })

    })
  }

  return exports

})()