Skip to content

Instantly share code, notes, and snippets.

@jackrusher
Last active December 20, 2015 07:49
Show Gist options
  • Save jackrusher/6095965 to your computer and use it in GitHub Desktop.
Save jackrusher/6095965 to your computer and use it in GitHub Desktop.
Interacting with Leap Motion using Clojure from within emacs.
(ns blue-ball
(:use [seesaw core font graphics])
(:require [clojure-leap.core :as leap]
[clojure-leap.screen :as l-screen]))
;; these atoms contain the current x/y state from the Leap
(def x (atom 10))
(def y (atom 10))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Build the window/canvas
;; native look & feel, please
(native!)
;; build and display frame (window)
(def f (-> (frame :title "Animation test")
pack!
show!))
;; what do on re-paint
(defn paint-ball [c g]
(let [ball-color (seesaw.color/color "#000066")]
(anti-alias g)
(draw g
(circle @x @y 20)
(style :foreground ball-color
:background ball-color))))
;; set frame content to our canvas
(config! f :content (border-panel
:center (canvas :id :animatron
:background "#eeeeee"
:paint paint-ball)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Update x/y from leap motion
(defn process-frame [frame screens]
(let [fingers (leap/fingers frame)]
(when-let [pointable (and (leap/pointables? frame)
(first (leap/pointables frame)))]
(let [position-map (l-screen/intersect-position screens pointable)]
(reset! x (int (:x position-map)))
(reset! y (int (:y position-map)))
(repaint! f)))))
(def listener
(leap/listener :frame #(process-frame (:frame %) (:screens %))
:default #(println "listener:" (:listener %) ", state: " (:state %))))
(def controller
(first (leap/controller listener)))
;; use this to de-activate the Leap event stream
;;(leap/remove-listener! controller listener)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment