   (aset overlay-canvas "height"(.-innerHeight js/window)))
 
 (defn render-mandelbrot!
-  [{:keys [scale x0 y0 escape-radius] :as render-state}]
+  [{escape-radius                          :escape-radius
+    {:keys [scale x0 y0] :as render-state} :rendering-data}]
 
   (println render-state)
 

                                          :color "green"))))



(set! (.-onmouseup overlay-canvas) (fn [e]
                                     (swap! app-state
                                            (fn [{:keys [mousedown-event rendering-data] :as old-state}]
                                              (let [scale            (:scale rendering-data)

                                                    old-x0           (:x0 rendering-data)
                                                    old-y0           (:y0 rendering-data)

                                                    start-x          (aget mousedown-event "pageX")
                                                    start-y          (aget mousedown-event "pageY")
                                                    finish-x         (aget e "pageX")
                                                    finish-y         (aget e "pageY")

                                                    width            (aget canvas "width")
                                                    height           (aget canvas "height")

                                                    portrait?        (< (- finish-x start-x) (- finish-y start-y))

                                                    new-scale        (if portrait?
                                                                       (/ height (/ (- finish-y start-y) scale))
                                                                       (/ width (/ (- finish-x start-x) scale)))

                                                    new-aspect-ratio (/ (- finish-x start-x)
                                                                        (- finish-y start-y))

                                                    padding          (* 0.5
                                                                        (if portrait?
                                                                          (- width (* height new-aspect-ratio))
                                                                          (- height (/ width new-aspect-ratio))))

                                                    new-x0           (if portrait?
                                                                       (- (+ old-x0 (/ start-x scale))
                                                                          (/ padding new-scale))

                                                                       (+ old-x0 (/ start-x scale)))

                                                    new-y0           (if portrait?
                                                                       (- old-y0 (/ start-y scale))

                                                                       (+ (- old-y0 (/ start-y scale))
                                                                          (/ padding new-scale)))]
                                                (-> old-state
                                                    (assoc-in [:rendering-data :x0] new-x0)
                                                    (assoc-in [:rendering-data :y0] new-y0)
                                                    (assoc-in [:rendering-data :scale] new-scale)

                                                    (dissoc :mousedown-event)))))))


(render-mandelbrot! (:rendering-data @app-state))

(defn on-js-reload
  "A figwheel thing"


    (.log js/console "Done in: " (int (* 1000 (/ (* height width)  (- (.getTime (js/Date.)) start)))) "px/s")))

(set! (.-onresize js/window) (fn [] (render-mandelbrot! @app-state)))

(add-watch app-state :state-changed
           (fn [_ _ old-state new-state]
             (when-not (= (:rendering-data old-state)
                          (:rendering-data new-state))
               (render-mandelbrot! new-state))))

(defn add-overlay-rectangle!
  "Adds a rectangle to the overlay canvas (and optionally clears all other rectangles)"
