diff --git a/r_place_backend/src/r_place_backend/core.clj b/r_place_backend/src/r_place_backend/core.clj index 1a54f1c..46a40d4 100644 --- a/r_place_backend/src/r_place_backend/core.clj +++ b/r_place_backend/src/r_place_backend/core.clj @@ -5,8 +5,8 @@ [compojure.core :refer [defroutes GET POST DELETE ANY context]])) (def sample-data {:column 4 - :row 4 - :color "green"}) + :row 4 + :color "green"}) (def colors ["#8967B3" "#4F75FF" "#8FD14F" "#FF6500" "#73EC8B" "#243642"]) diff --git a/r_place_ui/src/place/core.cljs b/r_place_ui/src/place/core.cljs index 41777da..a1f45d5 100644 --- a/r_place_ui/src/place/core.cljs +++ b/r_place_ui/src/place/core.cljs @@ -7,21 +7,22 @@ (def canvas (dom/getElement "paintCanvas")) (def ctx (.getContext canvas "2d")) +(def socket* (atom nil)) + (def tools {:brush (dom/getElement "brush") :eraser (dom/getElement "eraser") :color-picker (dom/getElement "colorPicker") :demo (dom/getElement "demo")}) -(defonce app-state (atom {})) -;; Set canvas size - -(defn set-zoom [new-scale] - (swap! app-state assoc :scale new-scale) - (style/setStyle canvas "transform" (str "scale(" new-scale ")")) - (style/setStyle canvas "transformOrigin" "top left") - (draw-grid)) +;; Grid settings +(def grid-size 20) +(def grid-color "#808080") +(defonce app-state (atom {:current-tool :brush + :current-color "#000000" + :scale 0.1})) +;; Set canvas size (defn draw-grid [] (set! (.-strokeStyle ctx) grid-color) (set! (.-lineWidth ctx) 0.5) @@ -34,6 +35,14 @@ (.lineTo ctx (.-width canvas) y)) (.stroke ctx)) +(defn set-zoom [new-scale] + (swap! app-state assoc :scale new-scale) + (style/setStyle canvas "transform" (str "scale(" new-scale ")")) + (style/setStyle canvas "transformOrigin" "top left") + (draw-grid)) + + + (defn draw-grid-pixel [grid-idx-x grid-idx-y color] (let [grid-x (* grid-idx-x grid-size) grid-y (* grid-idx-y grid-size)] @@ -54,9 +63,9 @@ (defn send-pixel! [x y color] - (.send socket* (pr-str {:column x - :row y - :color color}))) + (.send @socket* (pr-str {:column x + :row y + :color color}))) (defn handle-canvas-click [e] (let [rect (.getBoundingClientRect canvas) @@ -68,7 +77,7 @@ (:current-color @app-state))] (draw-pixel x y color) (send-pixel! (Math/floor (/ x grid-size)) - Math/floor + (Math/floor (/ y grid-size)) color))) (events/listen canvas "click" handle-canvas-click) @@ -82,13 +91,6 @@ (set! (.-width canvas) 500) (set! (.-height canvas) 500) - ;; Grid settings - (def grid-size 20) - (def grid-color "#808080") - - (def app-state (atom {:current-tool :brush - :current-color "#000000" - :scale 0.1})) ;; Set initial zoom (set-zoom 1) @@ -119,12 +121,13 @@ on-message-fn) socket)) -(do (def socket* (setup-ws "ws://localhost:8080/ws" - (fn [] - (js/console.log "Socket connected")) - (fn [e] - (js/console.log e.data) - (websocket-message-handler e.data))))) +(reset! socket* + (setup-ws "ws://localhost:8080/ws" + (fn [] + (js/console.log "Socket connected")) + (fn [e] + (js/console.log e.data) + (websocket-message-handler e.data)))) (def sample-data {:column 0