You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

87 lines
2.5 KiB

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: 3000,
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 "shaky" / "stuck" bug
// 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.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.updateMovementCircle = function(showMovementCircle) {
this.options.showMovementCircle = showMovementCircle;
this.movementCircleCtrl.querySelector('[type=checkbox]').checked = showMovementCircle;
this.particles.forEach(p => p.updateOptions({ showMovementCircle }));
}
Animation2.prototype.updateSpeed = function(speed) {
this.options.speed = speed;
this.particles.forEach(p => p.updateOptions({ speed }));
}
export default Animation2;