sliva/src/sliva/pages/gibanica.cljs

82 lines
2.5 KiB
Plaintext
Raw Normal View History

2018-08-10 18:29:18 +02:00
(ns sliva.pages.gibanica
(:require [reagent.core :as reagent]
[sliva.data :refer [appstate]]
[sliva.pages.navigation :refer [navigation]]
2018-08-11 03:00:13 +02:00
[sliva.socket :refer [send-message]]
[sliva.gfx :refer [get-param]]))
2018-08-10 18:29:18 +02:00
(defn motion-track [event]
(let [pospesek (.-acceleration event)
gibX (.-x pospesek)
gibY (.-y pospesek)
gibZ (.-z pospesek)
cas (-> (js/Date.)
(.valueOf))]
2018-08-10 21:45:24 +02:00
(send-message "motion" cas gibX gibY gibZ)))
2018-08-10 18:29:18 +02:00
(defn start-motion-track []
(.addEventListener js/window "devicemotion" motion-track))
(defn stop-motion-track []
(.removeEventListener js/window "devicemotion" motion-track))
2018-08-11 01:36:47 +02:00
(def rotation (reagent/atom nil))
2018-08-10 21:45:24 +02:00
(defn rotation-track [event]
(let [alpha (.-alpha event)
beta (.-beta event)
gamma (.-gamma event)
cas (-> (js/Date.)
(.valueOf))]
2018-08-11 01:36:47 +02:00
(console.log "rotation:" @rotation)
(if @rotation
(let [da (- (:a @rotation) alpha)
db (- (:b @rotation) beta)
dg (- (:g @rotation) gamma)]
(send-message
"rotation"
cas
da
db
dg))
(swap! rotation merge {:a alpha
:b beta
:g gamma}))))
2018-08-10 21:45:24 +02:00
(defn start-rotation-track []
(.addEventListener js/window "deviceorientation" rotation-track))
(defn stop-rotation-track []
(.removeEventListener js/window "deviceorientation" rotation-track))
2018-08-10 18:29:18 +02:00
(defn gibanica []
(reagent/create-class
{:display-name "gibanica"
2018-08-10 21:45:24 +02:00
:component-did-mount (fn []
(start-motion-track)
(start-rotation-track))
:component-will-unmount (fn []
(stop-motion-track)
(stop-rotation-track))
2018-08-11 01:36:47 +02:00
:should-component-update #(true)
2018-08-10 21:45:24 +02:00
:reagent-render
(fn []
[:div
[:h3 "gibaj me!"]
2018-08-11 03:00:13 +02:00
;; [:div
;; [:h4 "Rotacija"]
;; [:div "Alfa:" (:a @rotation)]
;; [:div "Beta:" (:b @rotation)]
;; [:div "Gama:" (:g @rotation)]]
2018-08-11 01:36:47 +02:00
[:div
2018-08-11 03:00:13 +02:00
"Barva"
[:input {:type "range"
:min 0.01
:max 0.333
:step 0.001
:value (get-param :zacetna-barva)
:on-change (fn [event]
(send-message "vizual-update" "zacetna-barva" (aget event "target" "value")))}]]
[:button {:on-click #(send-message "vizual-reset")} "Resetiraj"]
2018-08-10 21:45:24 +02:00
[navigation]])}))
2018-08-10 18:29:18 +02:00