@gitlab/ui
Version:
GitLab UI Components
93 lines (90 loc) • 3.22 kB
JavaScript
import { REACH_HEADROOM, CONFIG, MIN_AUTO_COUNT, DEFAULT_COUNT, AREA_EXPONENT, EDGE_ANGLE, POINT_ANGLE, POINT_POWER, MIN_REACH } from './constants';
const randomBetween = (min, max) => min + Math.random() * (max - min);
const degrees = value => value * Math.PI / 180;
const aimJitter = () => randomBetween(-degrees(CONFIG.aimJitter), degrees(CONFIG.aimJitter));
const countFor = stage => {
const share = stage.width * stage.height / (window.innerWidth * window.innerHeight);
return Math.round(Math.max(MIN_AUTO_COUNT, DEFAULT_COUNT * share ** AREA_EXPONENT));
};
const launchFromEdges = (stage, index) => {
const fromLeft = index % 2 === 0;
return {
x: fromLeft ? -10 : stage.width + 10,
y: stage.height + 10,
aim: Math.max(EDGE_ANGLE, Math.atan2(stage.height, stage.width)),
spread: degrees(CONFIG.edgeSpread),
direction: fromLeft ? 1 : -1,
power: 1
};
};
const launchFromPoint = origin => ({
x: origin.x,
y: origin.y,
aim: POINT_ANGLE,
spread: degrees(CONFIG.pointSpread),
direction: 1,
power: POINT_POWER
});
const createPiece = _ref => {
let stage = _ref.stage,
origin = _ref.origin,
index = _ref.index,
jitter = _ref.jitter,
maxSpeed = _ref.maxSpeed;
const launch = origin ? launchFromPoint(origin) : launchFromEdges(stage, index);
const depth = Math.random();
const reach = Math.sqrt(randomBetween(MIN_REACH, 1));
const speed = maxSpeed * launch.power * reach * (0.85 + depth * 0.2);
const angle = launch.aim + jitter + randomBetween(-launch.spread, launch.spread);
return {
depth,
x: launch.x,
y: launch.y,
delay: randomBetween(0, CONFIG.burstWindow),
radius: CONFIG.blobCells + depth * CONFIG.blobCellRange,
stretch: CONFIG.stretch + Math.random() * CONFIG.stretchRange,
peak: CONFIG.peak + depth * CONFIG.peakRange,
decayFrom: randomBetween(CONFIG.decayMin, 1),
velocityX: Math.cos(angle) * speed * launch.direction,
velocityY: -Math.sin(angle) * speed,
swayAmplitude: randomBetween(0.02, 0.08) * (1.3 - depth * 0.5),
swayFrequency: randomBetween(0.001, 0.003),
swayPhase: Math.random() * Math.PI * 2
};
};
const createPieces = _ref2 => {
let stage = _ref2.stage,
origin = _ref2.origin,
count = _ref2.count;
const travel = Math.max(Math.hypot(stage.width, stage.height), stage.height * REACH_HEADROOM);
const maxSpeed = travel * CONFIG.drag;
const jitter = aimJitter();
return Array.from({
length: count !== null && count !== void 0 ? count : countFor(stage)
}, (value, index) => createPiece({
stage,
origin,
index,
jitter,
maxSpeed
}));
};
const advancePiece = (piece, _ref3) => {
let elapsed = _ref3.elapsed,
delta = _ref3.delta;
if (elapsed < piece.delay) {
return piece;
}
const drag = Math.exp(-CONFIG.drag * delta);
const velocityX = piece.velocityX * drag;
const velocityY = (piece.velocityY + CONFIG.gravity * delta) * drag;
const sway = Math.sin(piece.swayPhase + elapsed * piece.swayFrequency) * piece.swayAmplitude;
return {
...piece,
velocityX,
velocityY,
x: piece.x + (velocityX + sway) * delta,
y: piece.y + velocityY * delta
};
};
export { advancePiece, createPieces };