50% increase in frame rate by drawing level once on a background canvas, instead of redrawing each frame.
(defpage "/tempest/:level" {:keys [level]}
(common/site-layout
(include-js "/tempest.js")
- [:canvas#canv1 {:width "900" :height "900"}]
+ [:canvas#canv-bg {:width "900" :height "900"
+ :style "position: absolute; z-index: 0;"}]
+ [:canvas#canv-fg {:width "900" :height "900"
+ :style "position: absolute; z-index: 1;"}]
[:p#fps "FPS 0.0"]
(javascript-tag (str "tempest.canvasDraw(" (pr-str level) ");"))
))
[context dims level]
(doseq []
(.clearRect context 0 0 (:width dims) (:height dims))
- (draw-board context dims level)
+ ;;(draw-board context dims level)
(draw-player context dims level (deref *player*))
(draw-entities context dims level @*enemy-list*)
(draw-entities context dims level @*projectile-list*)
(let [document (dom/getDocument)
timer (goog.Timer. 20)
level (get levels/*levels* (- (js/parseInt level) 1))
- canvas (dom/getElement "canv1")
+ canvas (dom/getElement "canv-fg")
context (.getContext canvas "2d")
+ bgcanvas (dom/getElement "canv-bg")
+ bgcontext (.getContext bgcanvas "2d")
handler (goog.events.KeyHandler. document)
dims {:width (.-width canvas) :height (.-height canvas)}]
+ (draw-board bgcontext dims level)
+
(def *frame-count* (atom 0))
(def *frame-time* (atom (goog.now)))