From 6222c87ecd77773e72a269adb05d6733ea8a1e65 Mon Sep 17 00:00:00 2001 From: Gareth Whittaker Date: Sun, 5 Feb 2017 22:20:53 +0000 Subject: [PATCH] initial commit --- .gitignore | 1 + animation-loop.js | 34 ++++++++++++++++++++++++++++++++++ package.json | 24 ++++++++++++++++++++++++ 3 files changed, 59 insertions(+) create mode 100644 .gitignore create mode 100644 animation-loop.js create mode 100644 package.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c2658d7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules/ diff --git a/animation-loop.js b/animation-loop.js new file mode 100644 index 0000000..8e25e69 --- /dev/null +++ b/animation-loop.js @@ -0,0 +1,34 @@ +import { Observable } from 'rxjs/Observable' +import { Subject } from 'rxjs/Subject' +import { animationFrame } from 'rxjs/scheduler/animationFrame' +import 'rxjs/add/observable/of' +import 'rxjs/add/observable/never' +import 'rxjs/add/operator/repeat' +import 'rxjs/add/operator/switchMap' + +const animationLoop = () => { + const frame = Observable + .of(null, animationFrame) + .repeat() + + const noop = Observable.never() + + const loop = new Subject() + .switchMap((active) => active ? frame : noop) + + return { + start() { + loop.next(true) + }, + + stop() { + loop.next(false) + }, + + subscribe(observer) { + return loop.subscribe(observer) + } + } +} + +export { animationLoop } diff --git a/package.json b/package.json new file mode 100644 index 0000000..baa2664 --- /dev/null +++ b/package.json @@ -0,0 +1,24 @@ +{ + "name": "rxjs-animation-loop", + "version": "0.1.0", + "description": "This module provides a game / animation loop based upon requestAnimationFrame.", + "author": "Gareth Whittaker ", + "main": "animation-loop.js", + "repository": { + "type": "git", + "url": "https://github.com/garethwhittaker/rxjs-animation-loop" + }, + "bugs": { + "url": "https://github.com/garethwhittaker/rxjs-animation-loop/issues" + }, + "keywords": [ + "animation loop", + "game loop", + "animationframe", + "requestanimationframe" + ], + "dependencies": { + "rxjs": "5.1.0" + }, + "license": "MIT" +}