clojurescript:触摸事件和Domina [英] clojurescript: touch events and Domina
问题描述
我无法将 touch或 changedTouches列表从Domina的touchstart事件中删除。
I'm having trouble getting the 'touch' or 'changedTouches' list out of the touchstart event in Domina.
这是我的:require东西:
Here's my :require stuff:
(ns myproj
(:require-macros [hiccups.core :as h])
(:require [domina :as dom]
[hiccups.runtime :as hiccupsrt]
[domina.events :as ev]
[cljs.reader :refer [read-string]]
[wsosc :as wo]
[clojure.browser.repl :as repl]
))
这是我的touchstart事件处理程序:
And here's my touchstart event handler:
(defn touchstart [evt]
; store event in an atom for repl access
(swap! de (fn [x] evt))
; print something to html to show a result (no console on the phone)
(dom/set-text! (dom/by-id "result") (str "blah" evt))
; hopefully someday extract touch coordinates here.
(let [rct (.getBoundingClientRect (dom/by-id "osccanvas"))
;touchlist1 (get evt "changedTouches")
;touchlist2 (.changedTouches evt)
;touchlist3 (.-changedTouches evt)
;kies (keys evt)]
wat (:type evt) ; this works
;wat (ev/raw-event evt) ; this works
;touchlist (.-changedTouches evt)]
;touch (.item touchlist 1)]
]
(dom/set-text! (dom/by-id "result") (str "touchstart touch:" wat))))
'de'是一个我试图用于调试的原子。我可以从事件中获取:type,但仅此而已。除了ev / raw-event以外,几乎没有其他注释内容起作用。至少对于我来说,raw-event返回了一个相对于repl而言相当难以理解的对象。如果我调换!带有原始事件的de看起来像这样:
'de' is an atom that I'm trying to use for debug. I'm able to get the :type from the event but that's about it. Pretty much none of the other commented things work, except for ev/raw-event. raw-event returns an object that is fairly incrutable from the repl, at least for me. If I swap! de with the raw-event it looks like this:
ClojureScript:myproj>@de
#<[object Object]>
我不知道如何从中提取信息,似乎对(key x)之类的东西反应迟钝或(.keys x)等。
I have no idea how extract information from this, it seems pretty unresponsive to things like (keys x) or (.keys x), etc.
另一个奇怪的是,我可以在上述函数中调用(:type evt),但是如果我将evt分配给de我不能在repl上使用'de'原子做同样的事情,即(:type @de)。
What is also strange is that I can call (:type evt) in the above function, but if I assign evt to de I can't do the same thing with the 'de' atom at the repl, ie (:type @de).
推荐答案
好吧,经过很多挫折之后,我终于有了工作。事实证明,有许多工作层我还没有真正意识到(并且不想知道!)。最主要的是,在domina touch事件对象中没有触摸信息-即使在domina获得事件之前,该信息也会被剥离。像这样:
Ok after much frustration I finally got things to work. It turns out there are a number of layers at work which I was not really aware of (and didn't WANT to be aware of!). The main thing is that there was no touch information in the domina touch event object - that gets stripped out even before domina gets the event. Its like this:
原始浏览器事件->谷歌关闭库-> domina库->我的代码
original browser event -> google closure library -> domina library -> my code
Google闭包(而不是clojure,不是它的javascript)库实际上剥离了触摸信息,因此在我得到的事件对象中不可用。谢谢,谷歌。但是,原始事件仍然可以访问,仅向下两层。代码如下所示:
And the google closure (not clojure, its javascript) library actually strips out the touch information, so its not available in the event object that I get. Thanks, google. However, the original event is still accessible, its just two layers down. The code looks like this:
(defn touchstart [evt]
(let [wat (ev/raw-event evt)
touches (.-changedTouches (.getBrowserEvent wat))
touch (.item touches 0)
]
(domousedown (.-clientX touch) (.-clientY touch))
))
所以我使用Domina的raw-event函数来获取Google闭包事件的版本( wat)。但这也没有触摸信息。我必须使用getBrowserEvent再上一层,然后可以按此处记录的那样调用changedTouches方法:
So I use Domina's raw-event function to get the google closure version of the event ('wat'). But that doesn't have the touch info either. I have to go one more level with getBrowserEvent, and then I can call the changedTouches method as documented here:
https://developer.mozilla.org/en-US/docs/DOM/TouchEvent
最后一个难题是检测是否首先存在触摸屏,因此我可以为此设置正确的事件功能。此非clojure hack可以完成此工作:
And the last piece of the puzzle was detecting whether a touchscreen is present in the first place, so I can set up the right event functions for that. This non-clojure hack does the job:
(if (js* "'ontouchstart' in window")
<set up touch events>
<set up non-touch events>)
我在这里尝试了clojure语法的各种排列,但似乎没有任何效果。欢迎对此提出建议。
I tried various permutations of clojure syntax here but nothing seemed to work for me. Open to suggestions on that.
这篇关于clojurescript:触摸事件和Domina的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!