-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspanran-wrap.ts
More file actions
52 lines (45 loc) · 1.88 KB
/
spanran-wrap.ts
File metadata and controls
52 lines (45 loc) · 1.88 KB
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
const titles = document.querySelectorAll<HTMLHeadingElement>('h1, h2');
const wordSegmenter = new Intl.Segmenter([], { granularity: 'word' });
const letterSegmenter = new Intl.Segmenter([], { granularity: 'grapheme' });
function getRandomBetween(min, max) {
return Math.random() * (max - min) + min;
}
function wrapLettersInSpans(title: HTMLHeadingElement) {
const text = title.innerText;
const wordsSegments = Array.from(wordSegmenter.segment(text));
title.innerHTML = '';
for(const wordSegment of wordsSegments) {
const word = wordSegment.segment;
const wordSpan = document.createElement('span');
wordSpan.classList.add(wordSegment.isWordLike ? 'word' : wordSegment.segment === ' ' ? 'space' : 'punctuation');
// wrap each word's letters
const lettersSegments = Array.from(letterSegmenter.segment(word));
for(const letterSegment of lettersSegments) {
const letter = letterSegment.segment;
const letterSpan = document.createElement('span');
letterSpan.classList.add('letter');
letterSpan.style.setProperty('--left', getRandomBetween(-10, 10) + 'deg');
letterSpan.style.setProperty('--right', getRandomBetween(-10, 10) + 'deg');
// letterSpan.style.setProperty('animation-delay', (Math.random() * 5) + 'ms');
letterSpan.innerText = letter;
wordSpan.appendChild(letterSpan);
}
title.appendChild(wordSpan);
}
// title.innerHTML = '';
// for(const letter of text) {
// const span = document.createElement('span');
// span.classList.add('letter');
// const innerSpan = document.createElement('span');
// innerSpan.innerText = letter;
// span.appendChild(innerSpan);
// // span.style.display = 'inline-block';
// span.style.rotate = `${Math.random() * 10 - 5}deg`;
// title.appendChild(span);
// }
}
// setInterval(() => {
for (const title of titles) {
wrapLettersInSpans(title);
}
// }, 500);