 ;;
 ;; Any variable called 'g' is a CanvasRenderingContext2D object.
 
-(def optimize-drawing false)
+(def optimize-drawing true)
+
+;; To debug the optimized drawing:
+;; * set snergly.image/optimize-drawing to true
+;; * set snergly.core/sync-by-frame to false
+;; * in snergly.core/sync-chan, change the parameter of async/timeout
+;;   from 0 to 1000.
+;;
+;; When I do this, I can tell that the cell background starts too far up
+;; and to the left, so that redrawing the background makes the top and left
+;; borders half-width.  And the right and left borders, if they are drawn,
+;; become progressively thicker; I think this is just because of the canvas
+;; drawing strokes with translucency for some reason, even
 
 (defn fill-rect [g style x y w h]
   (aset g "fillStyle" style)

        ]
    (aset g "imageSmoothingEnabled" false)
    (aset g "fillStyle" background)
    (aset g "lineWidth" 1)
    (when (g/new? grid) (fill-rect g background 0 0 img-width img-height))
    (draw-cells g grid cell-size background wall (remove nil? [dist2 dist1]) path)
    )

  {:distances g/Distances
   :color-family s/Keyword})

(def strokewidth 1.0)

(s/defn draw-cell-backgrounds [g
                               grid :- g/Grid
                               cell-size :- g/NonNegativeInt
                               background :- s/Str
                               distance-maps :- [DistanceMap]
                               changed-cells]
  (doseq [coord changed-cells]
    (let [{:keys [distances color-family max-distance] :as distance-map} (first (filter #(contains? (:distances %) coord) distance-maps))
          [y x] (map #(* % cell-size) coord)
          cell (g/grid-cell grid coord)
          x (if (g/linked? cell (:west cell)) (- x 0.5) x)
          y (if (g/linked? cell (:north cell)) (- y 0.5) y)
          w (if (g/linked? cell (:east cell)) (+ cell-size 0.5) cell-size)
          h (if (g/linked? cell (:south cell)) (+ cell-size 0.5) cell-size)
          color (if distance-map
                  (util/make-color max-distance (distances coord) color-family)
                  background)]
