Skip to content

Commit f35b668

Browse files
committed
Add time stamps to requestAnimationFrame
requestAnimationFrame provides its callback with a DOMHighResTimeStamp, measuring a monotonic clock time for each frame. I've extended the bindings such that waitForAnimationFrame and inAnimationFrame provide this value to the caller.
1 parent fc6fdea commit f35b668

File tree

2 files changed

+18
-16
lines changed

2 files changed

+18
-16
lines changed

JavaScript/Web/AnimationFrame.hs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,24 @@ newtype AnimationFrameHandle = AnimationFrameHandle JSRef
3434
{- |
3535
Wait for an animation frame callback to continue running the current
3636
thread. Use 'GHCJS.Concurrent.synchronously' if the thread should
37-
not be preempted.
37+
not be preempted. This will return the high-performance clock time
38+
stamp once an animation frame is reached.
3839
-}
39-
waitForAnimationFrame :: IO ()
40+
waitForAnimationFrame :: IO Double
4041
waitForAnimationFrame = do
4142
h <- js_makeAnimationFrameHandle
4243
js_waitForAnimationFrame h `onException` js_cancelAnimationFrame h
4344

4445
{- |
4546
Run the action in an animationframe callback. The action runs in a
46-
synchronous thread.
47+
synchronous thread, and is passed the high-performance clock time
48+
stamp for that frame.
4749
-}
48-
inAnimationFrame :: OnBlocked -- ^ what to do when encountering a blocking call
49-
-> IO () -- ^ the action to run
50+
inAnimationFrame :: OnBlocked -- ^ what to do when encountering a blocking call
51+
-> (Double -> IO ()) -- ^ the action to run
5052
-> IO AnimationFrameHandle
5153
inAnimationFrame onBlocked x = do
52-
cb <- syncCallback onBlocked x
54+
cb <- syncCallback1 onBlocked (x . pFromJSRef)
5355
h <- js_makeAnimationFrameHandleCallback (jsref cb)
5456
js_requestAnimationFrame h
5557
return h
@@ -68,6 +70,6 @@ foreign import javascript unsafe "h$animationFrameCancel"
6870
js_cancelAnimationFrame :: AnimationFrameHandle -> IO ()
6971
foreign import javascript interruptible
7072
"$1.handle = window.requestAnimationFrame($c);"
71-
js_waitForAnimationFrame :: AnimationFrameHandle -> IO ()
73+
js_waitForAnimationFrame :: AnimationFrameHandle -> IO Double
7274
foreign import javascript unsafe "h$animationFrameRequest"
7375
js_requestAnimationFrame :: AnimationFrameHandle -> IO ()

jsbits/animationFrame.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
function h$animationFrameCancel(h) {
22
if(h.handle) window.cancelAnimationFrame(h.handle);
33
if(h.callback) {
4-
h$release(h.callback)
5-
h.callback = null;
4+
h$release(h.callback)
5+
h.callback = null;
66
}
77
}
88

99
function h$animationFrameRequest(h) {
10-
h.handle = window.requestAnimationFrame(function() {
11-
var cb = h.callback;
12-
if(cb) {
13-
h$release(cb);
14-
h.callback = null;
15-
cb();
16-
}
10+
h.handle = window.requestAnimationFrame(function(ts) {
11+
var cb = h.callback;
12+
if(cb) {
13+
h$release(cb);
14+
h.callback = null;
15+
cb(ts);
16+
}
1717
});
1818
}

0 commit comments

Comments
 (0)