1
votes

I have the following ClojureScript code and am trying to detect the coordinates of a a click. So far, I can't even get Javascript alerts to recognize a click, let alone give me the coords.

I know I will have to write a function to have it give me the exact cells being clicked, but as a start, need to know how to get the coordinates of any area clicked on a page.

Thanks!

(defn header [color text]
  [:h1
   {:style
    {:color color
     :background-color "blue"}}
   text])

(defn Cell []
  [:div
    {:style
     {:width "40px"
      :height "40px"
      :float "right"
      :margin-bottom "1px"
      :margin-right "1px"
      :background-color "grey"
      :border "1px" "solid" "white"}}])

(defn home-page []
  [:div
   [header "red" "Minesweeper"]
   [:div
    {:style
     {:width "440px"
      :height "440px"}}
    (repeat 100 [Cell])]])
1

1 Answers

0
votes

Put an :onClick key at the same map indentation level as the :style. Its value should be a function, which will get an event e. Then:

(let [coords (.getClientPosition e)
      coords' {:x (aget coords "x")
               :y (aget coords "y")}])

Here's an example of a hashmap that has an :onClick event and its function:

{ :href "#"
  :onClick (fn [e]
               (.preventDefault e)
               (swap! counter inc))} 

The only thing that matters in the above is getting the e and using it. That was taken from the flappybird example program, which is how I got started.