All files / frontend/helpers newYearGarland.ts

100% Statements 22/22
100% Branches 4/4
100% Functions 5/5
100% Lines 20/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81                                            2x   2x           2x                                               2x 16x     2x 8x 8x 200x   8x       2x 11x 11x 15x 900x 900x 900x     11x 66x 44x    
/*
  Copyright (C) 2026 Sber
 
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at
 
          http://www.apache.org/licenses/LICENSE-2.0
 
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/
 
// Геометрия гирлянды новогоднего оформления (NewYearGarland.vue).
// Лампочки сгруппированы по фазам мигания: мигает слой фазы целиком (opacity HTML-слоя
// выполняется на видеокарте), а не каждая лампочка внутри SVG — иначе браузер
// перерисовывает всю гирлянду в основном потоке на каждом кадре (ERA-3755).
 
// Ширина рисунка гирлянды, px; на широком окне рисунок повторяется
export const GARLAND_TILE = 2400;
// Длина одного провиса провода; GARLAND_TILE кратен ему, поэтому шов между повторами не виден
export const GARLAND_SAG = 120;
 
export type GarlandBulb = { color: number; x: number; y: number; rotate: number; scale: number };
export type GarlandPhase = { delay: number; bulbs: GarlandBulb[] };
 
// [цвет, задержка мигания (с), x, y, поворот (град), масштаб]
const BULBS: Array<[number, number, number, number, number, number]> = [
  [3, 0, 24.3, 6.8, 0, 0.89], [1, 1.8, 58.4, 10, -8, 0.89], [3, 0, 92.6, 7.3, -10, 0.89],
  [1, 1.8, 151.8, 8, 7, 1], [2, 0.6, 174.6, 9.9, -4, 0.94], [1, 0.6, 214.1, 7.1, -5, 0.9],
  [2, 0.6, 264.9, 6.9, 7, 0.99], [3, 1.8, 300, 10, 1, 0.97], [1, 0.6, 331.9, 7.4, -10, 1.02],
  [3, 1.8, 381.4, 6.3, 3, 1.06], [1, 0.6, 417.5, 10, 9, 0.98], [3, 0, 456.7, 6.6, -13, 0.96],
  [2, 1.2, 509.6, 7.7, 7, 0.95], [3, 0, 541.1, 10, 1, 0.89], [1, 1.2, 578.9, 6.2, -7, 0.89],
  [3, 1.8, 628.2, 7.5, 14, 0.94], [2, 0, 664.6, 9.9, -4, 0.95], [3, 0.6, 693.5, 7.2, -11, 0.91],
  [2, 1.8, 745.2, 7, 13, 0.9], [1, 1.2, 778.8, 10, -4, 1.04], [3, 0.6, 816.1, 6.7, 3, 1.07],
  [1, 0.6, 861.4, 6.3, 0, 0.88], [3, 1.2, 897.2, 10, -8, 0.99], [1, 1.8, 939, 6.2, -3, 0.97],
  [3, 0, 985.2, 7, 4, 1.01], [2, 1.2, 1016.3, 10, 9, 0.91], [1, 1.2, 1054.8, 7, -13, 0.99],
  [2, 0.6, 1107.8, 7.4, -2, 1], [3, 0, 1141.6, 10, 8, 0.97], [2, 1.2, 1177.8, 6.4, 2, 0.98],
  [3, 1.8, 1221.4, 6.3, -1, 1.03], [1, 1.2, 1263.9, 10, -7, 0.92], [2, 1.2, 1289.4, 7.8, -4, 1.03],
  [3, 0.6, 1352.1, 8.1, 11, 0.98], [2, 0.6, 1378.3, 10, -5, 1.01], [1, 1.8, 1417.3, 6.5, -1, 0.92],
  [2, 1.8, 1464.7, 6.9, -2, 1.04], [3, 1.2, 1497.1, 10, 4, 0.97], [1, 1.8, 1539.1, 6.2, -9, 0.9],
  [2, 1.2, 1582.8, 6.5, 1, 0.98], [1, 0.6, 1623.6, 10, -8, 1.06], [3, 1.8, 1653.3, 7.2, -11, 0.9],
  [1, 0.6, 1706, 7.1, 10, 1.02], [3, 0.6, 1745.9, 9.9, -10, 1.04], [2, 1.2, 1774.9, 6.9, -4, 1.01],
  [1, 0, 1822.3, 6.4, 7, 0.88], [3, 0.6, 1860.3, 10, 8, 1.08], [2, 0.6, 1897.5, 6.5, -12, 0.92],
  [3, 0, 1949.6, 7.7, 2, 1.05], [2, 1.8, 1984.9, 9.9, -3, 1.01], [1, 0, 2017.5, 6.5, 0, 0.99],
  [2, 0.6, 2070.9, 7.9, 10, 1.04], [1, 1.2, 2096.1, 10, 0, 0.99], [3, 0, 2135.8, 6.8, -6, 1.04],
  [1, 0, 2191, 7.9, -3, 0.94], [2, 0.6, 2220.1, 10, 1, 0.97], [3, 0.6, 2255.9, 6.8, -7, 0.98],
  [2, 0.6, 2308.8, 7.6, 12, 1.06], [1, 1.2, 2344.1, 10, -7, 0.96], [2, 1.2, 2368.5, 8, -10, 0.92]
];
 
// Сколько повторов рисунка нужно, чтобы закрыть окно; не меньше одного
export const garlandTiles = (viewportWidth: number): number =>
  Math.max(1, Math.ceil((Number.isFinite(viewportWidth) ? viewportWidth : 0) / GARLAND_TILE));
 
// Провод на заданную ширину: волна из квадратичных кривых с провисом между точками крепления
export const garlandWire = (width: number): string => {
  let wire = 'M0 2';
  for (let x = 0; x < width; x += GARLAND_SAG) {
    wire += ` Q${x + GARLAND_SAG / 2} 20 ${x + GARLAND_SAG} 2`;
  }
  return wire;
};
 
// Лампочки всех повторов, сгруппированные по фазам мигания (по возрастанию задержки)
export const garlandPhases = (tiles: number): GarlandPhase[] => {
  const phases = new Map<number, GarlandBulb[]>();
  for (let tile = 0; tile < tiles; tile++) {
    for (const [color, delay, x, y, rotate, scale] of BULBS) {
      const bulbs = phases.get(delay) ?? [];
      bulbs.push({ color, x: x + tile * GARLAND_TILE, y, rotate, scale });
      phases.set(delay, bulbs);
    }
  }
  return [...phases.entries()]
    .sort(([a], [b]) => a - b)
    .map(([delay, bulbs]) => ({ delay, bulbs }));
};