parent
5e74dc90af
commit
7bf8ec67a2
11 changed files with 1490 additions and 1041 deletions
@ -0,0 +1,38 @@ |
||||
.controls-label { |
||||
cursor: pointer; |
||||
font-size: 13px; |
||||
margin-bottom: 5px; |
||||
padding: 10px; |
||||
|
||||
&:hover { |
||||
background: #fff; |
||||
} |
||||
} |
||||
|
||||
.controls-label-checkbox { |
||||
margin-right: 5px; |
||||
} |
||||
|
||||
.controls-label-text { |
||||
|
||||
} |
||||
|
||||
.controls-slider { |
||||
cursor: pointer; |
||||
width: 100%; |
||||
} |
||||
|
||||
.controls-button { |
||||
border: 0; |
||||
cursor: pointer; |
||||
font-size: 18px; |
||||
padding: 10px; |
||||
|
||||
&:hover { |
||||
background: #fff; |
||||
} |
||||
} |
||||
|
||||
.controls-bottom { |
||||
margin-top: auto; |
||||
} |
@ -1,58 +1,71 @@ |
||||
import Rx, { Observable } from 'rxjs'; |
||||
import Particle from './particle'; |
||||
import Store from './store'; |
||||
import Controls from './controls'; |
||||
|
||||
function Animation1(node) { |
||||
this.container = node; |
||||
this.bounds = node.getBoundingClientRect(); |
||||
} |
||||
function Animation1() { |
||||
this.options = { |
||||
count: 1 |
||||
}; |
||||
|
||||
// const grid = {};
|
||||
// for (let x = 0; x <= 600; x += 5) {
|
||||
// grid[x] = {};
|
||||
// for (let y = 0; y <= 600; y += 5) {
|
||||
// grid[x][y] = { type: null };
|
||||
//
|
||||
// if (x === 0 || y === 0 || x === 600 || y === 600) {
|
||||
// grid[x][y] = { type: 'wall' };
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
this.container = document.getElementById('animation1'); |
||||
this.bounds = this.container.getBoundingClientRect(); |
||||
this.controls = new Controls(document.getElementById('controls1'), this.options); |
||||
|
||||
Animation1.prototype.nextFrame = function() { |
||||
this.particles.forEach(p => p.nextFrame()); |
||||
} |
||||
const eventStack = this.controls.mount(); |
||||
|
||||
Animation1.prototype.reset = () => { |
||||
// while (DOM.container.childNodes.length) {
|
||||
// DOM.container.removeChild(DOM.container.firstChild);
|
||||
// }
|
||||
}; |
||||
|
||||
Animation1.prototype.init = function() { |
||||
this.particles = Array(1).fill().map(_ => new Particle(this.container, this.bounds)); |
||||
eventStack.subscribe(this.subscriber); |
||||
|
||||
const stop$ = Rx.Observable.fromEvent(this.container, 'stop'); |
||||
this.particles = []; |
||||
|
||||
// Change animation speed
|
||||
// Change animal pic
|
||||
// Enable random radius changes
|
||||
// Enable random rotation changes
|
||||
// Show movement circle
|
||||
// Show vision grid (including touches!)
|
||||
// Start / stop
|
||||
this.updateCount(); |
||||
|
||||
console.error("Click container to stop."); |
||||
const fps$ = Rx.Observable.interval(1000 / 32) |
||||
.takeUntil(stop$) |
||||
.finally(() => { console.error("Stopped."); }) |
||||
// this.particles = Array(5).fill().map(_ => new Particle(this.container, this.bounds));
|
||||
//
|
||||
// const stop$ = Rx.Observable.fromEvent(this.container, 'stop');
|
||||
//
|
||||
// // Change animation speed
|
||||
// // Change animal pic
|
||||
// // Enable random radius changes
|
||||
// // Enable random rotation changes
|
||||
// // Show movement circle
|
||||
// // Show vision grid (including touches!)
|
||||
// // Start / stop
|
||||
// // TODO add core UI
|
||||
//
|
||||
// console.error("Click container to stop.");
|
||||
// const fps$ = Rx.Observable.interval(1000 / 32)
|
||||
// .takeUntil(stop$)
|
||||
// .finally(() => { console.error("Stopped."); })
|
||||
//
|
||||
// const click$ = Rx.Observable.fromEvent(this.container, 'click');
|
||||
// click$.subscribe(() => {
|
||||
// this.container.dispatchEvent(new CustomEvent('stop'));
|
||||
// });
|
||||
//
|
||||
// fps$.subscribe(this.nextFrame.bind(this));
|
||||
}; |
||||
|
||||
const click$ = Rx.Observable.fromEvent(this.container, 'click'); |
||||
click$.subscribe(() => { |
||||
this.container.dispatchEvent(new CustomEvent('stop')); |
||||
}); |
||||
Animation1.prototype.subscriber = function({ key, value }) { |
||||
switch(key) { |
||||
case 'count': this.updateCount(); break; |
||||
} |
||||
} |
||||
|
||||
fps$.subscribe(this.nextFrame.bind(this)); |
||||
Animation1.prototype.nextFrame = function() { |
||||
this.particles.forEach(p => p.nextFrame()); |
||||
} |
||||
|
||||
Animation1.prototype.updateCount = function() { |
||||
while (this.particles.length >= this.options.count) { |
||||
const p = this.particles.pop(); |
||||
this.container.removeChild(p.node); |
||||
} |
||||
|
||||
while (this.particles.length < this.options.count) { |
||||
const p = new Particle(this.container, this.bounds); |
||||
this.particles.push(p); |
||||
} |
||||
}; |
||||
|
||||
export default Animation1; |
||||
|
File diff suppressed because it is too large
Load Diff
@ -1,23 +1,120 @@ |
||||
function Controls(node, animation) { |
||||
import Rx, { Observable } from 'rxjs'; |
||||
|
||||
function Controls(node, { speed, otherstuff }) { |
||||
this.node = node; |
||||
this.animation = new animation(node); |
||||
} |
||||
|
||||
Controls.prototype.mount = function(customNodes) { |
||||
const countCtrl = renderCountControl(); |
||||
const speedCtrl = renderSpeedControl(); |
||||
const radiusCtrl = renderRadiusControl(); |
||||
const rotationCtrl = renderRotationControl(); |
||||
const startStopCtrl = renderStartStopControl(); |
||||
|
||||
this.node.appendChild(countCtrl); |
||||
this.node.appendChild(speedCtrl); |
||||
this.node.appendChild(radiusCtrl); |
||||
this.node.appendChild(rotationCtrl); |
||||
this.node.appendChild(startStopCtrl); |
||||
|
||||
return Rx.Observable.merge( |
||||
Rx.Observable.fromEvent(countCtrl, 'change') |
||||
.map(evt => ({ key: 'count', value: evt.target.value * 1 })), |
||||
Rx.Observable.fromEvent(speedCtrl, 'change') |
||||
.map(evt => ({ key: 'speed', value: evt.target.value * 1 })), |
||||
Rx.Observable.fromEvent(radiusCtrl, 'change') |
||||
.map(evt => ({ key: 'radius', value: evt.target.checked })), |
||||
Rx.Observable.fromEvent(rotationCtrl, 'change') |
||||
.map(evt => ({ key: 'rotation', value: evt.target.checked })), |
||||
Rx.Observable.fromEvent(startStopCtrl, 'click') |
||||
.map(evt => ({ key: 'startstop', value: null })), |
||||
); |
||||
} |
||||
|
||||
const renderCountControl = function() { |
||||
const label = document.createElement('label'); |
||||
label.className = 'controls-label'; |
||||
|
||||
const text = document.createElement('span'); |
||||
text.innerHTML = 'Number of particles'; |
||||
text.className = 'controls-label-text'; |
||||
|
||||
const slider = document.createElement('input'); |
||||
slider.type = 'range'; |
||||
slider.min = 1; |
||||
slider.max = 10; |
||||
slider.className = 'controls-slider'; |
||||
|
||||
label.appendChild(text); |
||||
label.appendChild(slider); |
||||
|
||||
return label; |
||||
} |
||||
|
||||
const renderSpeedControl = function() { |
||||
const label = document.createElement('label'); |
||||
label.className = 'controls-label'; |
||||
|
||||
const text = document.createElement('span'); |
||||
text.innerHTML = 'Animation speed'; |
||||
text.className = 'controls-label-text'; |
||||
|
||||
if (this.animation.init === undefined) { |
||||
console.error("Animation passed to Control doesn't have an init() method."); |
||||
} |
||||
const slider = document.createElement('input'); |
||||
slider.type = 'range'; |
||||
slider.min = 1; |
||||
slider.max = 10; |
||||
slider.className = 'controls-slider'; |
||||
|
||||
if (this.animation.reset === undefined) { |
||||
console.error("Animation passed to Control doesn't have a reset() method."); |
||||
} |
||||
label.appendChild(text); |
||||
label.appendChild(slider); |
||||
|
||||
this.animation.init(); |
||||
return label; |
||||
} |
||||
|
||||
Controls.prototype.mount = function() { |
||||
// this.node.style.border = '10px solid purple'; WORKING
|
||||
const renderRadiusControl = function() { |
||||
const label = document.createElement('label'); |
||||
label.className = 'controls-label'; |
||||
|
||||
const text = document.createElement('span'); |
||||
text.innerHTML = 'Random radii'; |
||||
text.className = 'controls-label-text'; |
||||
|
||||
const checkbox = document.createElement('input'); |
||||
checkbox.type = 'checkbox'; |
||||
checkbox.className = 'controls-label-checkbox'; |
||||
|
||||
label.appendChild(checkbox); |
||||
label.appendChild(text); |
||||
|
||||
return label; |
||||
} |
||||
|
||||
const renderRotationControl = function() { |
||||
const label = document.createElement('label'); |
||||
label.className = 'controls-label'; |
||||
|
||||
const text = document.createElement('span'); |
||||
text.innerHTML = 'Random rotation'; |
||||
text.className = 'controls-label-text'; |
||||
|
||||
const checkbox = document.createElement('input'); |
||||
checkbox.type = 'checkbox'; |
||||
checkbox.className = 'controls-label-checkbox'; |
||||
|
||||
label.appendChild(checkbox); |
||||
label.appendChild(text); |
||||
|
||||
return label; |
||||
} |
||||
|
||||
const renderStartStopControl = function() { |
||||
const button = document.createElement('button'); |
||||
button.className = 'controls-button controls-bottom'; |
||||
button.innerHTML = '▶ Start'; |
||||
|
||||
// button.innerHTML = '◼ Stop';
|
||||
|
||||
// right aligned panel, pass in extra custom controls array of nodes
|
||||
// set of prescribed styles
|
||||
return button; |
||||
} |
||||
|
||||
export default Controls; |
||||
|
Loading…
Reference in new issue