             canvas-context (.getContext canvas "2d")
             mouse-move-chan (listen js/document (.-MOUSEMOVE EventType))
             mouse-up-chan (listen js/document (.-MOUSEUP EventType))]
-        ;;;
-        ;;; On any mouse up on the document, set mouse-down to false.
         (go
           (while true
-            (let [e (<! mouse-move-chan)
-                  x (.-clientX e)
-                  {:keys [mouse-down x-offset mouse-down-pos]} (om/get-state owner)
-                  [old-x old-y] mouse-down-pos
-                  new-x (+ x-offset (- x old-x))
-                  clamp-x (.clamp goog.math new-x 0 (- 400 canvas-width))]
-              (when mouse-down
-                (om/update-state! owner #(assoc % :x-offset clamp-x :mouse-down-pos [x old-y]))))))
+            (alt!
+              mouse-move-chan
+              ([v] (when-let [[clamp-x _ old-y] (clamped-rel-mouse-pos v 0 (- 400 canvas-width) (om/get-state owner))]
+                     (om/update-state! owner #(assoc % :x-offset clamp-x :mouse-down-pos [(.-clientX v) old-y]))))
+              mouse-up-chan
+              ([_] (om/set-state! owner :mouse-down false)))))
         (om/update-state! owner #(assoc % :canvas-context canvas-context :canvas canvas))))
 
     om/IDidUpdate

    om/IRender
    (render [_]
      (dom/div nil
               (dom/button #js {:className "toggle-recording"
                                :onClick (fn [e]
                                           (if is-recording
                                             (do
                                               (save-recording! data audio-recorder audio-context analyser-node)
                                               (om/transact! data :is-recording not))
                                             (do
                                               (.record audio-recorder)
                                               (om/transact! data :is-recording not))))}
                           (if is-recording "Stop" "Record"))))))

(defn wave-selector-view [data owner]
  (reify

             (- x1 x0))
          (- y1 y0)))))

(defn superlative-of [arr compar]
  (loop [i 0 min-val (aget arr 0)]
    (if (= i (.-length arr))
      min-val
      (let [cur-val (aget arr i)]
        (if (apply compar [cur-val min-val])
          (recur (inc i) cur-val)
          (recur (inc i) min-val))))))

(defn min-arr-val [arr]
  (superlative-of arr <))

(defn max-arr-val [arr]
  (superlative-of arr >))

(defn draw-select-rect! [canvas-width canvas-height canvas-context mouse-down? mouse-over?]
  (.clearRect canvas-context 0 0 canvas-width canvas-height)
