asciiblaster

- draw irc art in your web browser
git clone git://git.acid.vegas/asciiblaster.git
Log | Files | Refs | Archive | README

blit.js (2556B)

      1 var blit = (function(){
      2   var blit = {}
      3   blit.and = blit.atop = function(A, B, x, y){
      4     x = x || 0 ; y = y || 0
      5     B.forEach(function(lex, u, v){
      6       var cell = A.getCell(u+x, v+y)
      7       if (cell && lex.opacity > 0) {
      8         cell.assign(lex)
      9       }
     10     })
     11   }
     12   blit.or = blit.under = function(A, B, x, y){
     13     x = x || 0 ; y = y || 0
     14     B.forEach(function(lex, u, v){
     15       var cell = A.getCell(u+x, v+y)
     16       if (cell && cell.opacity == 0) {
     17         cell.assign(lex)
     18       }
     19     })
     20   }
     21   // copy the region of A beginning at x,y into B
     22   blit.copy_from = function(A, B, x, y){
     23     x = x || 0 ; y = y || 0
     24     B.forEach(function(lex, u, v){
     25       var cell = A.getCell(u+x, v+y)
     26       if (cell) {
     27         lex.assign(cell)
     28       }
     29     })
     30   }
     31   blit.copy_toroidal_from = function(A, B, x, y){
     32     x = x || 0 ; y = y || 0
     33     B.forEach(function(lex, u, v){
     34       var cell = A.get(u+x, v+y)
     35       if (cell) {
     36         lex.assign(cell)
     37       }
     38     })
     39   }
     40   blit.copy_to = function(A, B, x, y){
     41     x = x || 0 ; y = y || 0
     42     B.forEach(function(lex, u, v){
     43       var cell = A.getCell(u+x, v+y)
     44       if (cell) {
     45         cell.assign(lex)
     46       }
     47     })
     48   }
     49   blit.invert = function(A, B, x, y){
     50     x = x || 0 ; y = y || 0
     51     B.forEach(function(lex, u, v){
     52       var cell = A.getCell(u+x, v+y)
     53       if (cell && lex.opacity > 0) {
     54         cell.fg = get_inverse(cell.fg)
     55         cell.bg = get_inverse(cell.bg)
     56       }
     57     })
     58   }
     59   var distance_rect = function(x, y, ratio){
     60     return Math.sqrt((Math.pow(y * ratio, 2)) + Math.pow(x, 2))
     61   }
     62   var distance_square = function(x, y, ratio){
     63     return Math.sqrt((Math.pow(y * ratio, 2)) + Math.pow(x * ratio, 2))
     64   }
     65   blit.circle = function(A, lex){
     66     var hw = brush.w/2, hh = brush.h/2
     67     var ratio, distance
     68     
     69     if (brush.w === brush.h){
     70       distance = distance_square
     71       ratio = hw / hh * (brush.w === 3 || brush.w === 5 ? 1.2 : 1.05)
     72     } else {
     73       distance = distance_rect
     74       ratio = hw / hh
     75     }
     76 
     77     A.forEach(function(lex,x,y) {
     78       if (distance(x - hw + 0.5, y - hh + 0.5, ratio) > hw){
     79         lex.clear()
     80       }
     81     })
     82   }
     83   blit.cross = function(A, lex){
     84     A.forEach(function(lex,x,y) {
     85       if ((x+y)%2) {
     86         lex.clear()
     87       }
     88     })
     89   }
     90   blit.inverted_cross = function(A, lex){
     91     // 1x1 brush should still draw something
     92     if (A.w == 1 && A.h == 1) {
     93       return
     94     }
     95     A.forEach(function(lex,x,y) {
     96       if (!((x+y)%2)) {
     97         lex.clear()
     98       }
     99     })
    100   }
    101   blit.square = function(A, lex){
    102   	// i.e. no transparency
    103   }
    104   return blit
    105 })()