   mousedown: (pos) ->
     for n in @nodes
       if n.isHit(pos)
-        @selected =
-          node: n
-          ox: n.x # original location
-          oy: n.y
-          mx: pos.x # mousedown location
-          my: pos.y
-          t: Date.now()
+        if @merging?
+          if n isnt @merging.node
+            @mergeNodes(@merging.node, n)
+            @merging = null
+        else
+          @selected =
+            node: n
+            ox: n.x # original location
+            oy: n.y
+            mx: pos.x # mousedown location
+            my: pos.y
+            t: Date.now()
         true
+
+    if @merging?
+      @merging = null
+
     false
 
   mousemove: (pos) ->

node_style = stroke: 'white', width: 1.5, fill: 'black'
node_emph_style = stroke: 'white', width: 5.5, fill: 'black'
link_style = stroke: 'white', width: 1.5
node_text_style = fill: 'white', font: '16px monospace'

setStyle = (ctx, s) ->

        mdx = pos.x - @selected.mx
        mdy = pos.y - @selected.my
        if mdx*mdx+mdy*mdy < 10*10
          # call it a click

          @merging =
            node: @selected.node
            x: pos.x
            y: pos.y

    @selected = null

  mergeNodes: (node0, node1) ->
    # filter both out of the list
    @nodes = ( n for n in @nodes when n isnt node0 and n isnt node1 )
    newnode = new Inner(node0.value + node1.value, node0, node1)
    newnode.x = (node0.x + node1.x) / 2
    newnode.y = (node0.y + node1.y) / 2 - Math.abs(node0.x - node1.x)
    @nodes.push(newnode)

# render loop
canvas = document.getElementById('cnv')
