import Rx, { Observable } from 'rxjs'; import Particle from './particle'; import Store from './store'; import Controls from './controls'; import { CONTROLS } from './enums'; function Animation1a() { this.options = { count: 1, maxCount: 25, randomize: true, showMovementCircle: true, speed: 4 }; this.container = document.getElementById('animation1a'); this.bounds = this.container.getBoundingClientRect(); this.movementCircleCtrl = createMovementCircleControl(); this.particles = []; const controls = new Controls( document.getElementById('controls1a'), this.options, [this.movementCircleCtrl] ); const circle$ = Rx.Observable.fromEvent(this.movementCircleCtrl, 'change') .map(evt => ({ key: CONTROLS.MOVEMENT_CIRCLE, value: evt.target.checked })); const eventStack$ = controls.mount().merge(circle$); eventStack$.subscribe(this.subscriber.bind(this)); this.updateCount(this.options.count); this.updateMovementCircle(this.options.showMovementCircle); }; function createMovementCircleControl() { const label = document.createElement('label'); label.className = 'controls-checkbox'; const text = document.createElement('span'); text.innerHTML = 'Show movement circle'; text.className = 'controls-checkbox-text'; const checkbox = document.createElement('input'); checkbox.type = 'checkbox'; checkbox.className = 'controls-checkbox-input'; label.appendChild(checkbox); label.appendChild(text); return label; } Animation1a.prototype.subscriber = function({ key, value }) { switch(key) { case CONTROLS.ANIMATING: this.updateAnimating(value); break; case CONTROLS.COUNT: this.updateCount(value); break; case CONTROLS.MOVEMENT_CIRCLE: this.updateMovementCircle(value); break; case CONTROLS.RANDOMIZE: this.updateRandomize(value); break; case CONTROLS.SPEED: this.updateSpeed(value); break; } } Animation1a.prototype.nextFrame = function() { this.particles.forEach(p => p.nextFrame()); } Animation1a.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)); } } Animation1a.prototype.updateCount = function(count) { while (this.particles.length > count) { delete this.particles.pop().remove(); } while (this.particles.length < count) { const p = new Particle(this.container, this.bounds, this.options); this.particles.push(p); } } Animation1a.prototype.updateMovementCircle = function(value) { this.options.showMovementCircle = value; this.movementCircleCtrl.querySelector('[type=checkbox]').checked = value; this.particles.forEach(p => p.updateConfig({ showMovementCircle: value })); } Animation1a.prototype.updateRandomize = function(value) { this.options.randomize = value; this.particles.forEach(p => p.updateConfig({ randomize: value })); } Animation1a.prototype.updateSpeed = function(value) { this.options.speed = value; this.particles.forEach(p => p.updateConfig({ speed: value })); } export default Animation1a;