     @timer = null
     @callback()
 
+GENERIC_TF_TEMPLATE="""//Generic transistion function, coded in JS
+{
+  //number of states
+  'states': 2,
+
+  //Neighbors sum calculation. By default - sum of all.
+  //'plus': function(s,x){ return s+x; },
+  //'plusInitial': 0,
+
+  //Transition function. Takes current state and sum, returns new state.
+  'next': function(x, s){
+    if (s==2) return x;
+    if (s==3) return 1;
+    return 0;
+  }
+}
+"""
+
+binaryTransitionFunc2GenericCode = (binTf) ->
+  row2condition = (row) -> ("s==#{sum}" for nextValue, sum in row when nextValue).join(" || ")
+  
+  conditionBorn = row2condition binTf.table[0]
+  conditionStay = row2condition binTf.table[1]
+  
+  code = ["""//Automatically generated code for binary rule #{binTf}
+{
+    //number of states
+    'states': 2,
+
+    //Neighbors sum calculation is default. Code for reference.
+    //'plus': function(s,x){ return s+x; },
+    //'plusInitial': 0,
+    
+    //Transition function. Takes current state and sum, returns new state.
+    'next': function(x, s){
+        if (x==1 && (#{conditionStay})) return 1;
+        if (x==0 && (#{conditionBorn})) return 1;
+        return 0;
+     }
+}"""]
+
 # ============ Bind Events =================
 E("btn-reset").addEventListener "click", doReset
 E("btn-step").addEventListener "click", doStep
 #E("canvas").addEventListener "click", doCanvasClick
-E("canvas").addEventListener "mousedown", doCanvasMouseDown, true
+E("canvas").addEventListener "mousedown", doCanvasMouseDown
 E("canvas").addEventListener "mouseup", doCanvasMouseUp
-E("canvas").addEventListener "mousemove", doCanvasMouseMove, true
-E("canvas").addEventListener "mousedrag", doCanvasMouseMove, true
+E("canvas").addEventListener "mousemove", doCanvasMouseMove
+E("canvas").addEventListener "mousedrag", doCanvasMouseMove
 E("btn-set-rule").addEventListener "click", doSetRule
 E("btn-set-rule-generic").addEventListener "click", (e)->
   doSetRuleGeneric()

E('btn-disable-generic-rule').addEventListener 'click', doDisableGeneric
E('btn-export-close').addEventListener 'click', doExportClose
#initialize
GENERIC_TF_TEMPLATE="""//Generic transistion function, coded in JS
{
  //number of states
  'states': 2,

  //Neighbors sum calculation. By default - sum of all.
  //'plus': function(s,x){ return s+x; },
  //'plusInitial': 0,

  //Transition function. Takes current state and sum, returns new state.
  'next': function(x, s){
    if (s==2) return x;
    if (s==3) return 1;
    return 0;
  }
}
"""

binaryTransitionFunc2GenericCode = (binTf) ->
  row2condition = (row) -> ("s==#{sum}" for nextValue, sum in row when nextValue).join(" || ")
  
  conditionBorn = row2condition binTf.table[0]
  conditionStay = row2condition binTf.table[1]
  
  code = ["""//Automatically generated code for binary rule #{binTf}
{
    //number of states
    'states': 2,

    //Neighbors sum calculation is default. Code for reference.
    //'plus': function(s,x){ return s+x; },
    //'plusInitial': 0,
    
    //Transition function. Takes current state and sum, returns new state.
    'next': function(x, s){
        if (x==1 && (#{conditionStay})) return 1;
        if (x==0 && (#{conditionBorn})) return 1;
        return 0;
     }
}"""]


if not E('generic-tf-code').value
  E('generic-tf-code').value = GENERIC_TF_TEMPLATE


  updatePopulation()    
  
doCanvasMouseDown = (e) ->
  canvas.setCapture true
  e.preventDefault()
  [x,y] = getCanvasCursorPosition e, canvas
  unless (e.button is 0) and not e.shiftKey
