import Rx, { Observable } from 'rxjs'; import Grid from './grid'; import Particle from './particle'; import Controls from './controls'; import { CONTROLS } from './enums'; function Animation1b() { this.options = { count: 1, maxCount: 1000, speed: 8 }; this.container = document.getElementById('animation1b'); this.particles = []; this.grid = new Grid(); const controls = new Controls( document.getElementById('controls1b'), this.options ); controls.mount().subscribe(this.subscriber.bind(this)); this.updateCount(this.options.count); }; Animation1b.prototype.subscriber = function({ key, value }) { switch(key) { case CONTROLS.ANIMATING: this.updateAnimating(value); break; case CONTROLS.COUNT: this.updateCount(value); break; case CONTROLS.SPEED: this.updateSpeed(value); break; } } Animation1b.prototype.nextFrame = function() { this.particles.forEach(p => p.nextFrame()); } Animation1b.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)); } } Animation1b.prototype.updateCount = function(count) { const bounds = this.container.getBoundingClientRect(); while (this.particles.length > count) { delete this.particles.pop().remove(); } while (this.particles.length < count) { const p = new Particle(this.container, bounds, this.options, this.grid); this.particles.push(p); } } Animation1b.prototype.updateSpeed = function(value) { this.options.speed = value; this.particles.forEach(p => p.updateConfig({ speed: value })); } export default Animation1b;