我正在尝试在ClojureScript中覆盖onload
和document
的{{1}}函数。我认为Image
应该可以做到,但我没有取得任何成功。相关代码如下:
set!
(defn load-image [img-path]
(let [img (js/Image.)]
(do (set! (.-src img) img-path)
img)))
(defn add-img-canvas [img-path width height]
(let [img (load-image img-path)]
(set! (.-onload img)
(fn [] ;; This function is never called.
(let [canvas (get-scaled-canvas img width height)]
(do (pr-str canvas)
(swap! game-state :canvas canvas)))))))
(defn hello-world []
(let [count (atom 1)]
(fn []
[:div
[:h1 (:text @game-state)]
[:div (do (swap! count inc) (str "count is " @count))]
[:canvas (:canvas @game-state)]])))
(reagent/render-component [hello-world]
(. js/document (getElementById "app")))
(set! (.-onload js/document)
(fn [] ;; This function is also never called.
(add-img-canvas (:img-src game-state) 100 130)))
;;(. js/document onload)
中的匿名函数未被调用。我做错了什么?