archives titles fixe
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
/**
|
||||
* @file
|
||||
* Tirage graisse/italique lettre par lettre — COPIE de l'algorithme du thème.
|
||||
*
|
||||
* ⚠ À GARDER SYNCHRONISÉ avec themes/custom/leshed/assets/js/titles.js
|
||||
* (TITLE_STYLE, randomWght(), spreadPositions(), styleWordChars()). Choix
|
||||
* délibéré : titles.js n'est pas refactoré en module partagé, le générateur
|
||||
* d'animation reproduit donc ses règles à l'identique. Toute modification
|
||||
* des règles côté thème doit être reportée ici.
|
||||
*/
|
||||
|
||||
export const TITLE_STYLE = {
|
||||
wghtThin: { min: 100, max: 200 },
|
||||
wghtMedium: { min: 200, max: 400 },
|
||||
wghtBold: { min: 800, max: 900 },
|
||||
boldRatio: 0.28,
|
||||
mediumRatio: 0.12,
|
||||
italicOnBold: 0.6,
|
||||
italicOnMedium: 0.4,
|
||||
italicOnThin: 0.30,
|
||||
wghtStep: 25,
|
||||
};
|
||||
|
||||
// Graisse des espaces : `.variable-title { font-weight: 200 }`, en romain,
|
||||
// sans le letter-spacing des `.char` (les espaces ne sont pas des `.char`).
|
||||
export const SPACE_WGHT = 200;
|
||||
// `span.char { letter-spacing: -0.1em }` dans _variable-titles.scss.
|
||||
export const LETTER_SPACING_EM = -0.1;
|
||||
|
||||
function randomInt(min, max) {
|
||||
return Math.floor(Math.random() * (max - min)) + min;
|
||||
}
|
||||
|
||||
function randomWght({ min, max }) {
|
||||
const step = TITLE_STYLE.wghtStep;
|
||||
return min + step * randomInt(0, Math.floor((max - min) / step) + 1);
|
||||
}
|
||||
|
||||
function spreadPositions(n, k, exclude = new Set()) {
|
||||
const set = new Set();
|
||||
for (let i = 0; i < k; i++) {
|
||||
const start = Math.floor((i * n) / k);
|
||||
const end = Math.floor(((i + 1) * n) / k);
|
||||
const span = Math.max(1, end - start);
|
||||
let pos = randomInt(start, start + span);
|
||||
let guard = 0;
|
||||
while ((exclude.has(pos) || set.has(pos)) && guard < span) {
|
||||
pos = start + ((pos - start + 1) % span);
|
||||
guard++;
|
||||
}
|
||||
set.add(pos);
|
||||
}
|
||||
return set;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tirage d'un mot de `n` lettres : [{wght, ital}, ...].
|
||||
* Même logique que styleWordChars() du thème, sans le DOM.
|
||||
*/
|
||||
export function drawWord(n) {
|
||||
if (!n) return [];
|
||||
const bold = spreadPositions(n, Math.round(n * TITLE_STYLE.boldRatio));
|
||||
const medium = spreadPositions(n, Math.round(n * TITLE_STYLE.mediumRatio), bold);
|
||||
|
||||
const decisions = [];
|
||||
for (let i = 0; i < n; i++) {
|
||||
let range;
|
||||
let pItal;
|
||||
if (bold.has(i)) {
|
||||
range = TITLE_STYLE.wghtBold;
|
||||
pItal = TITLE_STYLE.italicOnBold;
|
||||
}
|
||||
else if (medium.has(i)) {
|
||||
range = TITLE_STYLE.wghtMedium;
|
||||
pItal = TITLE_STYLE.italicOnMedium;
|
||||
}
|
||||
else {
|
||||
range = TITLE_STYLE.wghtThin;
|
||||
pItal = TITLE_STYLE.italicOnThin;
|
||||
}
|
||||
decisions.push({ wght: randomWght(range), ital: Math.random() < pItal });
|
||||
}
|
||||
|
||||
if (!decisions.some((d) => d.ital)) {
|
||||
const accents = [...bold, ...medium];
|
||||
const idx = accents.length ? accents[randomInt(0, accents.length)] : randomInt(0, n);
|
||||
decisions[idx].ital = true;
|
||||
}
|
||||
|
||||
let run = 0;
|
||||
for (const d of decisions) {
|
||||
if (d.ital) {
|
||||
run += 1;
|
||||
if (run > 2) {
|
||||
d.ital = false;
|
||||
run = 0;
|
||||
}
|
||||
}
|
||||
else {
|
||||
run = 0;
|
||||
}
|
||||
}
|
||||
return decisions;
|
||||
}
|
||||
Reference in New Issue
Block a user