summaryrefslogtreecommitdiff
path: root/js/blit.js
blob: b434742ea0be327cd11c681b77aa7a3042aea214 (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
var blit = (function(){
  var blit = {}
  blit.and = blit.atop = function(A, B, x, y){
    x = x || 0 ; y = y || 0
    B.forEach(function(lex, u, v){
      var cell = A.getCell(u+x, v+y)
      if (cell && lex.opacity > 0) {
        cell.assign(lex)
      }
    })
  }
  blit.or = blit.under = function(A, B, x, y){
    x = x || 0 ; y = y || 0
    B.forEach(function(lex, u, v){
      var cell = A.getCell(u+x, v+y)
      if (cell && cell.opacity == 0) {
        cell.assign(lex)
      }
    })
  }
  // copy the region of A beginning at x,y into B
  blit.copy_from = function(A, B, x, y){
    x = x || 0 ; y = y || 0
    B.forEach(function(lex, u, v){
      var cell = A.getCell(u+x, v+y)
      if (cell) {
        lex.assign(cell)
      }
    })
  }
  blit.copy_to = function(A, B, x, y){
    x = x || 0 ; y = y || 0
    B.forEach(function(lex, u, v){
      var cell = A.getCell(u+x, v+y)
      if (cell) {
        cell.assign(lex)
      }
    })
  }
  blit.invert = function(A, B, x, y){
    x = x || 0 ; y = y || 0
    B.forEach(function(lex, u, v){
      var cell = A.getCell(u+x, v+y)
      if (cell && lex.opacity > 0) {
        cell.fg = get_inverse(cell.fg)
        cell.bg = get_inverse(cell.bg)
      }
    })
  }
  var distance_rect = function(x, y, ratio){
    return Math.sqrt((Math.pow(y * ratio, 2)) + Math.pow(x, 2))
  }
  var distance_square = function(x, y, ratio){
    return Math.sqrt((Math.pow(y * ratio, 2)) + Math.pow(x * ratio, 2))
  }
  blit.circle = function(A, lex){
    var hw = brush.w/2, hh = brush.h/2
    var ratio, distance
    
    if (brush.w === brush.h){
      distance = distance_square
      ratio = hw / hh * (brush.w === 3 || brush.w === 5 ? 1.2 : 1.05)
    } else {
      distance = distance_rect
      ratio = hw / hh
    }

    A.forEach(function(lex,x,y) {
      if (distance(x - hw + 0.5, y - hh + 0.5, ratio) > hw){
        lex.clear()
      }
    })
  }
  blit.cross = function(A, lex){
    A.forEach(function(lex,x,y) {
      if ((x+y)%2) {
        lex.clear()
      }
    })
  }
  blit.inverted_cross = function(A, lex){
    // 1x1 brush should still draw something
    if (A.w == 1 && A.h == 1) {
      return
    }
    A.forEach(function(lex,x,y) {
      if (!((x+y)%2)) {
        lex.clear()
      }
    })
  }
  blit.square = function(A, lex){
  	// i.e. no transparency
  }
  return blit
})()