import Rx, { Observable } from 'rxjs'; // import DOM from './dom'; import { RAD } from './enums'; import Store from './store'; const random = { bool: (weight) => Math.random() < (weight || 0.5), num: (min, max) => min + Math.round(Math.random() * max), color: () => `rgb(${Math.floor(Math.random() * 255)}, ${Math.floor(Math.random() * 255)}, ${Math.floor(Math.random() * 255)})` } /* ===== Constructor ===== */ function Particle(container, bounds, options) { const color = random.color(); this.container = container; this.bounds = bounds; // this.visionGridPoints = calculateVisionGridPoints(); this.node = document.createElement('div'); this.node.className = 'particle has-vision'; this.node.style.backgroundColor = color; this.container.appendChild(this.node); // this.visionGridPoints.forEach(point => { // this.container.appendChild(point.div); // }); this.options = Object.assign({ randomizeRadius: false, randomizeRotation: false, showMovementCircle: false, showVIsionGrid: false, speed: 4 }, options); this.arc = { r: random.num(100, 200), t: random.num(0, RAD.t360), x: random.num(0, bounds.width), y: random.num(0, bounds.height) } this.particle = { clockwise: random.bool(), color, x: 0, y: 0 } this.interval = random.num(RAD.t90, RAD.t360); this.updateOptions(this.options); this.nextFrame(); }; Particle.prototype.nextFrame = function() { this.move(); repaintParticle(this.arc, this.node, this.particle); repaintCircle(this.arc, this.circle, this.particle); // if (this.options.showVisionGrid === true) { // repaintVisionGrid(this.arc, this.particle, this.visionGridPoints); // } }; Particle.prototype.updateBounds = function(bounds) { this.bounds = bounds; } Particle.prototype.updateOptions = function(options) { Object.assign(this.options, options); if (options.showMovementCircle === true && this.circle === undefined) { this.circle = document.createElement('div'); this.circle.className = 'particle-movement-circle'; this.circle.style.borderColor = this.particle.color; this.node.appendChild(this.circle); } if (options.showMovementCircle === false && this.circle !== undefined) { this.circle.parentNode.removeChild(this.circle); delete this.circle; } } Particle.prototype.move = function() { // Randomly change radius and rotation direction. if (this.interval <= 0) { this.interval = random.num(RAD.t90, RAD.t360); if (this.options.randomizeRadius === true) { this.arc = moveArc(this.arc, random.num(100, 200)); if (random.bool(0.8) && this.options.randomizeRotation === true) { this.particle.clockwise = !this.particle.clockwise; this.arc = changeDirection(this.arc); } } } // Ensure constant velocity and theta between 0 and 2π. const delta = this.options.speed / this.arc.r; this.interval -= delta; this.arc.t += (this.particle.clockwise ? -delta : +delta); this.arc.t = (this.arc.t > 0 ? this.arc.t % RAD.t360 : RAD.t360 - this.arc.t); this.particle.x = this.arc.x + this.arc.r * Math.cos(this.arc.t); this.particle.y = this.arc.y - this.arc.r * Math.sin(this.arc.t); // Overflow. if (this.particle.x < 0) { this.particle.x += this.bounds.width; this.arc.x += this.bounds.width } else if (this.particle.x > this.bounds.width) { this.particle.x -= this.bounds.width; this.arc.x -= this.bounds.width } if (this.particle.y < 0) { this.particle.y += this.bounds.height; // TODO size of area this.arc.y += this.bounds.height } else if (this.particle.y > this.bounds.height) { this.particle.y -= this.bounds.height; this.arc.y -= this.bounds.height } } function moveArc(arc, newRadius) { const r0 = arc.r; const r1 = newRadius; // Moves arc center to new radius while keeping theta constant. arc.x -= (r1 - r0) * Math.cos(arc.t); arc.y += (r1 - r0) * Math.sin(arc.t); arc.r = r1; return arc; } function changeDirection(arc) { arc.t = (arc.t + RAD.t180) % RAD.t360; arc.x -= (2 * arc.r) * Math.cos(arc.t); arc.y += (2 * arc.r) * Math.sin(arc.t); return arc; } function calculateVisionGridPoints() { const gridSize = 5; const visionRadius = 50; const squareGrid = []; for (let x = -visionRadius; x <= visionRadius; x += gridSize) { for (let y = -visionRadius; y <= visionRadius; y += gridSize) { let alpha = Math.atan(y / x); if (x === 0 && y === 0) { alpha = 0; } else if (x === 0 && y < 0) { alpha = RAD.t270; } else if (y === 0 && x < 0) { alpha = RAD.t180; } else if (x === 0 && y > 0) { alpha = RAD.t90; } else if (x < 0 && y < 0) { alpha = alpha + RAD.t180; } else if (x <= 0) { alpha = RAD.t180 + alpha; } else if (y < 0) { alpha = RAD.t360 + alpha; } squareGrid.push({ x, y, alpha }); } } const r0 = Math.pow(visionRadius, 2); const r1 = Math.pow(visionRadius - gridSize, 2); return squareGrid.reduce((acc, point) => { const p = Math.pow(point.x, 2) + Math.pow(point.y, 2); if (p > r0 || p < r1) { return acc; } const div = document.createElement('div'); div.className = 'particle-vision-dot'; acc.push(Object.assign(point, { div })); return acc; }, []); } function repaintParticle(arc, node, particle) { const rad = particle.clockwise ? RAD.t180 - arc.t : RAD.t360 - arc.t; node.style.left = `${particle.x}px`; node.style.top = `${particle.y}px`; } function repaintCircle(arc, node, particle) { if (node === undefined) { return; } node.style.width = `${2 * arc.r}px`; node.style.height = `${2 * arc.r}px`; node.style.left = `${-particle.x - arc.r + arc.x}px`; node.style.top = `${-particle.y - arc.r + arc.y}px`; node.style.borderRadius = `${arc.r}px`; } function repaintVisionGrid(arc, particle, points) { const r0 = Math.min(arc.t, arc.t - RAD.t180); const r1 = Math.max(arc.t, arc.t + RAD.t180); const gridX = particle.x - particle.x % 5; const gridY = particle.y - particle.y % 5; points.forEach(({ x, y, alpha, div }, i) => { if (alpha >= 0 && alpha <= r0) { div.style.display = (particle.clockwise ? 'none' : 'block'); // div.className = (clockwise ? 'anim3-dot removed' : 'anim3-dot'); } else if (alpha >= arc.t && alpha <= r1) { div.style.display = (particle.clockwise ? 'none' : 'block'); // div.className = (clockwise ? 'anim3-dot removed' : 'anim3-dot'); } else { div.style.display = (particle.clockwise ? 'block' : 'none'); // div.className = (clockwise ? 'anim3-dot' : 'anim3-dot removed'); } div.style.left = `${x + gridX}px`; div.style.top = `${-y + gridY}px`; }); } export default Particle;