-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscheduleAnimFrame.coffee
More file actions
48 lines (40 loc) · 1.22 KB
/
Copy pathscheduleAnimFrame.coffee
File metadata and controls
48 lines (40 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
init = (Bacon) ->
cancelRequestAnimFrame = do ->
window.cancelAnimationFrame or
window.webkitCancelRequestAnimationFrame or
window.mozCancelRequestAnimationFrame or
window.oCancelRequestAnimationFrame or
window.msCancelRequestAnimationFrame or
clearTimeout
requestAnimFrame = do ->
window.requestAnimationFrame or
window.webkitRequestAnimationFrame or
window.mozRequestAnimationFrame or
window.oRequestAnimationFrame or
window.msRequestAnimationFrame or
(cb) -> setTimeout(cb, 1000 / 60)
scheduleFrame = (cb) ->
id = -1
animLoop = (x) ->
cb(x)
id = requestAnimFrame(-> animLoop(id))
animLoop(id)
Bacon.scheduleAnimFrame = ->
Bacon.fromBinder (handler) ->
id = scheduleFrame(handler)
-> cancelRequestAnimFrame(id)
Bacon.repeatedlyOnFrame = (values, divisor = 1) ->
index = 0
Bacon.scheduleAnimFrame()
.scan(0, (x) -> x + 1)
.filter((tick) -> !(tick % divisor))
.map(-> values[index++ % values.length])
.toEventStream()
if module?
Bacon = require("baconjs")
module.exports = init(Bacon)
else
if typeof define == "function" and define.amd
define ["bacon"], init
else
init(this.Bacon)