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

  var cols = 100
  var rows = 30

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

  canvas.bind = function(){

    canvas.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){
        if (is_mobile) return
        e.preventDefault()
        dragging = true
        current_canvas = canvas
        if (e.altKey) {
          if (e.shiftKey) {
            blit.copy_from(canvas, brush, floor(x-brush.w/2), floor(y-brush.h/2))
            brush.mask(brush)
            draw.set_last_point(e, point)
          }
          else {
            brush.load(lex)
            brush.generate()
            dragging = false
          }
          return
        }
        else if (drawing) {
          undo.new()
          draw.down(e, lex, point)
        }
        else if (selecting) {
          selection.down(e, lex, point)
        }
        else if (transforming) {
          transform.down(e, lex, point)
        }
        else if (filling) {
          undo.new()
          draw.fill(brush, x, y)
        }
        canvas.focus(x, y)
      })

      lex.span.addEventListener("mousemove", function(e){
        mouse.x = x
        mouse.y = y
        if (is_mobile) return
        if (! dragging) return
        if (drawing) {
          draw.move(e, lex, point)
        }
        else if (selecting) {
          selection.move(e, lex, point)
        }
        else if (transforming) {
          transform.move(e, lex, point)
        }
        canvas.focus(x, y)
      })

    })

    if (is_mobile) {
      canvas.rapper.addEventListener('touchstart', function(e){
        e.preventDefault()
        var x, y, point, lex
        x = (e.touches[0].pageX - canvas.rapper.offsetTop) / canvas.aa[0][0].span.offsetWidth
        y = (e.touches[0].pageY - canvas.rapper.offsetTop) / canvas.aa[0][0].span.offsetHeight
        x = ~~clamp(x, 0, canvas.aa[0].length-1)
        y = ~~clamp(y, 0, canvas.aa.length-1)
        point = [x,y]
        lex = canvas.aa[y][x]
        dragging = true
        if (drawing) {
          undo.new()
          draw.down(e, lex, point)
        }
        else if (filling) {
          undo.new()
          draw.fill(brush, x, y)
        }
        canvas.focus(x, y)
      })
      canvas.rapper.addEventListener("touchmove", function(e){
        e.preventDefault()
        var x, y, point, lex
        x = (e.touches[0].pageX - canvas.rapper.offsetTop) / canvas.aa[0][0].span.offsetWidth
        y = (e.touches[0].pageY - canvas.rapper.offsetTop) / canvas.aa[0][0].span.offsetHeight
        x = ~~clamp(x, 0, canvas.aa[0].length-1)
        y = ~~clamp(y, 0, canvas.aa.length-1)
        point = [x,y]
        lex = canvas.aa[y][x]
        if (! dragging) return
        shader_el.innerHTML = point.join(",")
        if (drawing) {
          draw.move(e, lex, point)
        }
        canvas.focus(x, y)
      })
    }

  }

  canvas.min = 1
  canvas.max = 999

  // canvas.resize(1, 1, true) // wont create undo state
  canvas.resize = function(w, h, no_undo){
    var old_w = this.w, old_h = this.h
    w = this.w = clamp(w, this.min, this.max)
    h = this.h = clamp(h, this.min, this.max)
    if (old_w === w && old_h === h) return;

    if (!no_undo){
      undo.new()
      undo.save_resize(w, h, old_w, old_h)
     } 

    canvas.__proto__.resize.call(canvas, w, h)
    controls.canvas_w.char = "" + w
    controls.canvas_w.build()
    controls.canvas_h.char = "" + h
    controls.canvas_h.build()
  }
  canvas.size_add = function(w, h){
    canvas.resize(canvas.w + w, canvas.h + h)
  }

  return canvas

})()