Skip to content

Instantly share code, notes, and snippets.

@fantasywind
Last active August 29, 2015 14:01
Show Gist options
  • Save fantasywind/77985d42bb4b7f52a01f to your computer and use it in GitHub Desktop.
Save fantasywind/77985d42bb4b7f52a01f to your computer and use it in GitHub Desktop.
requestAnimationFrame service on angular
'use strict'
angular.module('testApp')
.service 'Frame', ->
refresher = []
refresherCleaner = []
_state = 'OFF'
@state =
OFF: 'OFF'
ON: 'ON'
@start = ->
_state = @state.ON
console.log 'canvas animation on'
looper = =>
# clean
for fn in refresherCleaner
fn()
refresherCleaner = []
# exec
for fn in refresher
fn()
# check state
if _state is @state.ON
requestAnimationFrame looper
requestAnimationFrame looper
@off = ->
_state = @state.OFF
console.log 'canvas animation off'
@appendAction = ()->
stopFns = []
for fn in arguments
if angular.isFunction fn
refresher.push fn
stopFns.push ->
for refreshFn, idx in refresher
if refreshFn is fn
refresherCleaner.push ->
refresher.splice idx, 1
break
return false if stopFns.length is 0
return if stopFns.length is 1 then stopFns[0] else stopFns
return @
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment