         y (quot mouse-y pixel-size)]
     [(* x pixel-size) (* y pixel-size)]))
 
-(defn visit-pixels-for-rect-tool [doc-x doc-y]
+(defn visit-pixels-for-rect-tool [doc-x doc-y doc-width]
   (let [[orig-x orig-y] (get-in @guistate/transient-state [:mouse-down-pos])
         [x y nx ny] (geometry/normalize-rect orig-x orig-y doc-x doc-y)]
     (doseq [dx (range x nx)]
       (doseq [dy (range y ny)]
-        (visit-pixel (geometry/flatten-to-index dx dy 64))))))
+        (visit-pixel (geometry/flatten-to-index dx dy doc-width))))))
 
 (defn unpack-event [event]
   [(.-offsetX event) (.-offsetY event)])

                      (reset! guistate/transient-state
                              (assoc @guistate/transient-state :last-mouse-pos [])))
                    (when (= paint-tool :box)
                      (visit-pixels-for-rect-tool doc-x doc-y))
                    (when (= paint-tool :line)
                      (let [last-x ((get-in @guistate/transient-state [:mouse-down-pos]) 0)
                            last-y ((get-in @guistate/transient-state [:mouse-down-pos]) 1)]

        (.fillRect context x-with-offset y-with-offset pixel-size pixel-size)))))


(defn clip-sub-image [image rect doc-width]
  (let [[x1 y1 x2 y2] rect
        width (geometry/rect-width rect)
        height (geometry/rect-height rect)
        sub-image-indices(for [y (range y1 y2)
                               x (range x1 x2)
                               :let [i (geometry/flatten-to-index x y doc-width)]]
                               i)
        sub-image-data (vec (map #(nth image %) sub-image-indices))]
      {:width width
