 
     (.putImageData context idata 0 0)
 
-    (.log js/console "Done in: " (int (* 1000 (/ (* height width)  (- (.getTime (js/Date.)) start)))) "px/s")))
+    (.log js/console "Done in: " (int (* 1000 (/ (* screen-height screen-width)
+                                                 (- (.getTime (js/Date.)) start)))) "px/s")))
 
 (defn re-render!
   "Adds a green semi-transparent overlay to the screen, then

      :fill (do (aset context "fillStyle" color)
                (.fillRect context x0 y0 (- x1 x0) (- y1 y0))))))

(defn render-mandelbrot!
  "Iterate over every pixel in the canvas, rendering the mandelbrot
  set at the specified scale. Uses a javascript function (in resources/public/js/fractal-maths.js)
  for the hard maths, because clojurescript just wasn't cutting it speed wise (you really want local
  variables for this kind of thing - I got it to be only ~10 times slower using the >> and << macros)"
  [{:keys [canvas overlay-canvas escape-radius]
    {:keys [scale x0 y0] :as render-state} :rendering-data}]

  (set-canvas-dimensions! canvas)
  (set-canvas-dimensions! overlay-canvas)
  (clear-canvas! overlay-canvas)

  (let [context               (.getContext canvas "2d")
        max-iterations        (int (* 10 (Math/log scale))) ; Sensible?
        start                 (.getTime (js/Date.))

        width                 (.-width canvas)
        height                (.-height canvas)
        idata                 (.createImageData context width height)
        data                  (.-data idata)

        escape-radius-squared (* escape-radius escape-radius)]

    (forloop
     [(x 0) (< x width)  (inc x)]
     (forloop
      [(y 0) (< y height) (inc y)]

      (let [real       (+ (/ x scale) x0)
            imaginary  (- (/ y scale) y0)
            iterations (js/mandelbrot_smoothed_iteration_count escape-radius-squared max-iterations real imaginary)
            intensity  (* 255 (/ iterations max-iterations))
            data-index (* 4 (+ x (* y width)))]

        (aset data data-index intensity)
        (aset data (+ data-index 1) intensity)

  (.setTimeout js/window #(render-mandelbrot! new-state) 50))


(defn handle-mouseup
  "On mouseup, we zoom the mandelbrot to the specified box and remove the
   :mousedown-event key from the app-state"
  [e]
  (swap!
   app-state
   (fn [{:as old-state :keys [mousedown-event canvas rendered-rectangle]}]

     (let [{:keys [x0 y0 scale]}        rendered-rectangle

           x*1                           (+ x0 (/ (aget e "pageX") scale))
           y*1                           (- y0 (/ (aget e "pageY") scale))

           x*0                           (+ x0 (/ (aget mousedown-event "pageX") scale))
           y*0                           (- y0 (/ (aget mousedown-event "pageY") scale))]

       (-> old-state
           (dissoc :mousedown-event)
           (assoc :rendering-data {:x0     x*0
                                   :y0     y*0
                                   :width  (- x*1 x*0)
                                   :height (- y*0 y*1)}))))))

(defn handle-state-change!
  [_ _ old-state new-state]
