@gitlab/ui
Version:
GitLab UI Components
55 lines (52 loc) • 1.52 kB
JavaScript
import { CONFIG, GLYPHS } from './constants';
const randomBetween = (min, max) => min + Math.random() * (max - min);
const sample = values => values[Math.floor(Math.random() * values.length)];
const glyph = () => sample(GLYPHS);
const measureGrid = _ref => {
let width = _ref.width,
height = _ref.height;
return {
across: Math.ceil(width / CONFIG.cell),
rows: Math.ceil(height / CONFIG.cell)
};
};
const createColumns = (stage, colors) => {
const _measureGrid = measureGrid(stage),
across = _measureGrid.across,
rows = _measureGrid.rows;
return Array.from({
length: across
}, (value, column) => ({
column,
rows,
row: -randomBetween(0, CONFIG.startSpread),
speed: randomBetween(CONFIG.minSpeed, CONFIG.maxSpeed),
color: sample(colors),
head: null,
steps: []
})).filter(() => Math.random() < CONFIG.columnShare);
};
const advanceColumn = (column, delta) => {
if (column.row > column.rows + CONFIG.restartAfter) {
return {
...column,
row: -randomBetween(0, CONFIG.restartSpread),
head: null,
steps: []
};
}
const row = column.row + column.speed * delta;
const from = Math.max(0, Math.ceil(column.row));
const to = Math.min(column.rows, Math.floor(row));
const steps = [];
for (let step = from; step <= to; step += 1) {
steps.push(step);
}
return {
...column,
row,
head: steps.length ? steps.at(-1) : column.head,
steps
};
};
export { advanceColumn, createColumns, glyph, measureGrid };