Skip to content

Instantly share code, notes, and snippets.

@rm-hull rm-hull/
Last active Jan 4, 2016

What would you like to do?
Demonstration of using Swannodette's OM: reactively capturing a stream of mouse move events.
(ns examples.mouse.core
(:require-macros [cljs.core.async.macros :refer [go]])
(:require [ :as events]
[cljs.core.async :as async :refer [>! <! put! chan]]
[om.core :as om :include-macros true]
[om.dom :as dom :include-macros true]
[dommy.core :refer [insert-after!]])
(:use-macros [dommy.macros :only [sel1 node]])
(:import [ EventType]))
; Adjusted slightly from the original to insert an "app" div
(sel1 :#canvas-area)
(insert-after! (node [:div#app])))
(defn listen [el type]
(let [out (chan)]
(events/listen el type #(put! out %))
{:mouse nil}
(fn [app node]
(will-mount [_]
(let [mouse-chan
(fn [e] [(.-clientX e) (.-clientY e)])
[(listen js/window EventType/MOUSEMOVE)])]
(go (while true
(om/update! app assoc :mouse (<! mouse-chan))))))
(render [_]
(dom/p nil
(when-let [pos (:mouse app)]
(pr-str (:mouse app)))))))
(.getElementById js/document "app"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.