import Rx, { Observable } from 'rxjs'; import Particle from './particle'; import Store from './store'; import Controls from './controls'; import { CONTROLS } from './enums'; function Animation2() { this.options = { animating: false, count: 1, maxCount: 2000, speed: 4 }; this.container = document.getElementById('animation2'); this.bounds = this.container.getBoundingClientRect(); this.particles = []; const controls = new Controls( document.getElementById('controls2'), this.options ); controls.mount().subscribe(this.subscriber.bind(this)); this.updateAnimating(this.options.animating); this.updateCount(this.options.count); // TODO X dimension modified by core UI // TODO ANIM3 Show vision grid (including touches!) // TODO ANIM3 regen vision grid is option updated // TODO ANIM3 cleanup vision grid // TODO ANIM3 Pallet avoidance // TODO ANIM4 Flocking }; Animation2.prototype.subscriber = function({ key, value }) { switch(key) { case CONTROLS.ANIMATING: this.updateAnimating(value); break; case CONTROLS.COUNT: this.updateCount(value); break; case CONTROLS.RADIUS: this.updateRadius(value); break; case CONTROLS.ROTATION: this.updateRotation(value); break; case CONTROLS.SPEED: this.updateSpeed(value); break; } } Animation2.prototype.nextFrame = function() { this.particles.forEach(p => p.nextFrame()); } Animation2.prototype.updateAnimating = function(isAnimating) { this.options.animating = isAnimating; if (isAnimating) { const fps$ = Rx.Observable.interval(1000 / 32) .takeWhile(_ => this.options.animating); fps$.subscribe(this.nextFrame.bind(this)); } } Animation2.prototype.updateCount = function(count) { while (this.particles.length >= count) { const p = this.particles.pop(); this.container.removeChild(p.node); } while (this.particles.length < count) { const p = new Particle(this.container, this.bounds, this.options); this.particles.push(p); } } Animation2.prototype.updateRadius = function(value) { this.particles.forEach(p => p.updateOptions({ randomizeRadius: value })); } Animation2.prototype.updateRotation = function(value) { this.particles.forEach(p => p.updateOptions({ randomizeRotation: value })); } Animation2.prototype.updateSpeed = function(speed) { this.particles.forEach(p => p.updateOptions({ speed })); } export default Animation2;