import Rx, { Observable } from 'rxjs'; import Particle from './particle'; import Store from './store'; import Controls from './controls'; import { CONTROLS } from './enums'; function Animation1() { this.options = { animating: false, count: 1, maxCount: 25, randomizeRadius: true, randomizeRotation: true, showMovementCircle: false, // showVisionGrid: false, speed: 4 }; this.container = document.getElementById('animation1'); this.bounds = this.container.getBoundingClientRect(); this.movementCircleCtrl = createMovementCircleControl(); this.particles = []; const controls = new Controls( document.getElementById('controls1'), this.options, [this.movementCircleCtrl] ); const eventStack = controls.mount() .merge(Rx.Observable.fromEvent(this.movementCircleCtrl, 'change') .map(evt => ({ key: CONTROLS.MOVEMENT_CIRCLE, value: evt.target.checked })) ); eventStack.subscribe(this.subscriber.bind(this)); this.updateAnimating(this.options.animating); this.updateCount(this.options.count); this.updateMovementCircle(this.options.showMovementCircle); // TODO particle starts offscreen sometimes // TODO ANIM2 Max out to 1000 or 2000 particles // TODO ANIM3 Show vision grid (including touches!) // TODO ANIM3 regen vision grid is option updated // TODO ANIM3 cleanup vision grid // TODO ANIM4 Pallet avoidance // TODO ANIM5 Flocking }; 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; } Animation1.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.RADIUS: this.updateRadius(value); break; case CONTROLS.ROTATION: this.updateRotation(value); break; case CONTROLS.SPEED: this.updateSpeed(value); break; } } Animation1.prototype.nextFrame = function() { this.particles.forEach(p => p.nextFrame()); } Animation1.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)); } } Animation1.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); } } Animation1.prototype.updateMovementCircle = function(showMovementCircle) { this.options.showMovementCircle = showMovementCircle; this.movementCircleCtrl.querySelector('[type=checkbox]').checked = showMovementCircle; this.particles.forEach(p => p.updateOptions({ showMovementCircle })); } Animation1.prototype.updateRadius = function(randomizeRadius) { this.options.randomizeRadius = randomizeRadius; this.particles.forEach(p => p.updateOptions({ randomizeRadius })); } Animation1.prototype.updateRotation = function(randomizeRotation) { this.options.randomizeRotation = randomizeRotation; this.particles.forEach(p => p.updateOptions({ randomizeRotation })); } Animation1.prototype.updateSpeed = function(speed) { this.options.speed = speed; this.particles.forEach(p => p.updateOptions({ speed })); } export default Animation1;