-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7eba24f
commit e59366f
Showing
3 changed files
with
102 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
(ns bardo.examples.om | ||
(:require-macros [cljs.core.async.macros :refer [go]]) | ||
(:require [bardo.ease :refer [wrap ease shift clamp]] | ||
[bardo.interpolate :refer [interpolate into-lazy-seq mix blend chain pipeline]] | ||
[bardo.transition :refer [transition]] | ||
[om.core :as om :include-macros true] | ||
[sablono.core :as html :refer-macros [html]] | ||
[cljs.core.async :as async :refer [<! timeout]] | ||
|
||
[adzerk.boot-reload.client :as reload])) | ||
|
||
(enable-console-print!) | ||
|
||
(defonce app-state {:pos {:x 0 | ||
:y 0}}) | ||
|
||
(defn now [] | ||
(.getTime (js/Date.))) | ||
|
||
(defn mover | ||
[{:keys [x y]} owner {:keys [speed] | ||
:or {speed 2000}}] | ||
(reify | ||
om/IInitState | ||
(init-state [_] | ||
{:intrpl (interpolate {:x x :y y} {:x x :y y}) | ||
:start (now)}) | ||
om/IWillReceiveProps | ||
(will-receive-props [_ {:keys [x y]}] | ||
(let [{:keys [intrpl start]} (om/get-state owner) | ||
t (/ (- (now) start) speed) | ||
[px py] ((juxt :x :y) (om/get-props owner)) | ||
intrpl (if (< t 1) | ||
(-> intrpl | ||
(shift 0 1 t 1) | ||
(mix (interpolate (intrpl t) {:x x :y y}))) | ||
(interpolate {:x px :y py} | ||
{:x x :y y}))] | ||
(om/set-state! owner :intrpl intrpl) | ||
(om/set-state! owner :start (now)))) | ||
om/IRenderState | ||
(render-state [_ {:keys [x y intrpl start]}] | ||
(let [t (/ (- (now) start) speed)] | ||
(when (< t 1) | ||
(let [{:keys [x y]} (intrpl t)] | ||
(go | ||
(om/set-state! owner :x x) | ||
(om/set-state! owner :y y))))) | ||
(html | ||
[:div | ||
{:style {:position "fixed" | ||
:top y | ||
:left x | ||
:border "1px solid #ddd" | ||
:padding "5px"}} | ||
[:div | ||
"Location"] | ||
[:div | ||
(str "x: " (int x))] | ||
[:div | ||
(str "y: " (int y))]])))) | ||
|
||
(defn app-view | ||
[data owner] | ||
(reify | ||
om/IRender | ||
(render [this] | ||
(html | ||
[:div | ||
{:on-click (fn [e] | ||
(om/update! data :pos {:x (.-pageX e) | ||
:y (.-pageY e)})) | ||
:style {:position "fixed" | ||
:width "100%" | ||
:height "100%"}} | ||
"Click anywhere, click repeated in different corners to curve" | ||
(om/build mover (:pos data))])))) | ||
|
||
(defn mount [] | ||
(om/root app-view app-state | ||
{:target (. js/document (getElementById "root"))})) | ||
|
||
(mount) | ||
(reload/connect "ws://localhost:3449" {:on-jsload mount}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,15 @@ | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<style> | ||
body {margin: 0px} | ||
</style> | ||
</head> | ||
<body> | ||
<div id="root"></div> | ||
<script src="http://fb.me/react-0.11.2.js"></script> | ||
<script src="out/goog/base.js" type="text/javascript"></script> | ||
<script type="text/javascript" src="main.js"></script> | ||
<script type="text/javascript">goog.require("bardo.examples.om");</script> | ||
</body> | ||
</html> |