import Rx, { Observable } from 'rxjs'; // import DOM from './dom'; import { RAD } from './enums'; import Store from './store'; const random = { bool: () => Math.random() < 0.5, num: (min, max) => min + Math.round(Math.random() * max) } 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 Particle(container, bounds, options = {}) { this.container = container; this.bounds = bounds; this.visionGridPoints = calculateVisionGridPoints(); this.node = document.createElement('div'); this.node.className = 'particle has-vision'; this.circle = document.createElement('div'); this.circle.className = 'particle-movement-circle'; this.container.appendChild(this.node); this.container.appendChild(this.circle); this.visionGridPoints.forEach(point => { this.container.appendChild(point.div); }); 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(), speed: 4, x: 0, y: 0 } this.interval = 0; this.updateOptions(options); this.nextFrame(); }; Particle.prototype.nextFrame = function() { this.move(); this.repaintParticle(); this.repaintCircle(); this.repaintVisionGrid(); } Particle.prototype.updateOptions = function(options) { this.particleImage = 'seahorse'; this.randomlyChangeRadius = (new Boolean(options.randomlyChangeRadius)) || true; this.randomlyChangeRotation = (new Boolean(options.randomlyChangeRotation)) || true; this.showCircle = (new Boolean(options.showCircle)) || false; this.showVision = (new Boolean(options.showVision)) || false; } Particle.prototype.repaintParticle = function() { const rad = this.particle.clockwise ? RAD.t180 - this.arc.t : RAD.t360 - this.arc.t; this.node.style.left = `${this.particle.x}px`; this.node.style.top = `${this.particle.y}px`; this.node.style.transform = `rotate(${rad}rad)`; } Particle.prototype.repaintCircle = function() { this.circle.style.width = `${2 * this.arc.r}px`; this.circle.style.height = `${2 * this.arc.r}px`; this.circle.style.left = `${this.arc.x - this.arc.r}px`; this.circle.style.top = `${this.arc.y - this.arc.r}px`; this.circle.style.borderRadius = `${this.arc.r}px`; } Particle.prototype.repaintVisionGrid = function() { const r0 = Math.min(this.arc.t, this.arc.t - RAD.t180); const r1 = Math.max(this.arc.t, this.arc.t + RAD.t180); const gridX = this.particle.x - this.particle.x % 5; const gridY = this.particle.y - this.particle.y % 5; this.visionGridPoints.forEach(({ x, y, alpha, div }, i) => { if (alpha >= 0 && alpha <= r0) { div.style.display = (this.particle.clockwise ? 'none' : 'block'); // div.className = (clockwise ? 'anim3-dot removed' : 'anim3-dot'); } else if (alpha >= this.arc.t && alpha <= r1) { div.style.display = (this.particle.clockwise ? 'none' : 'block'); // div.className = (clockwise ? 'anim3-dot removed' : 'anim3-dot'); } else { div.style.display = (this.particle.clockwise ? 'block' : 'none'); // div.className = (clockwise ? 'anim3-dot' : 'anim3-dot removed'); } div.style.left = `${x + gridX}px`; div.style.top = `${-y + gridY}px`; }); } Particle.prototype.move = function() { // Randomly change radius and rotation direction. this.interval -= 1; if (this.interval <= 0) { this.interval = random.num(50, 100); this.arc = moveArc(this.arc, random.num(100, 200)); if (random.bool()) { this.particle.clockwise = !this.particle.clockwise; this.arc = changeDirection(this.arc); } } // Ensure constant velocity and theta between 0 and 2π. const delta = this.particle.speed / this.arc.r; 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 } } export default Particle;