Skip to content

Commit f9889ae

Browse files
committed
fix: Use grapheme segmenter for proper Devanagari typewriter rendering
1 parent 8654351 commit f9889ae

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

portfolio.md

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,33 +1135,46 @@ full-width: true
11351135
'यदिहास्ति तदन्यत्र यद्नास्ति न तद्क्वचित्'
11361136
];
11371137

1138+
// Split into grapheme clusters for proper Devanagari handling
1139+
function toGraphemes(str) {
1140+
if (typeof Intl !== 'undefined' && Intl.Segmenter) {
1141+
return [...new Intl.Segmenter().segment(str)].map(s => s.segment);
1142+
}
1143+
// Fallback: split by spaces and treat each word as unit
1144+
return str.split(/(\s+)/).filter(s => s.length > 0);
1145+
}
1146+
11381147
let quoteIndex = 0;
11391148
let charIndex = 0;
11401149
let isDeleting = false;
1150+
let currentGraphemes = [];
11411151
const element = document.getElementById('sanskrit-typewriter');
1142-
const typeSpeed = 80;
1143-
const deleteSpeed = 40;
1152+
const typeSpeed = 100;
1153+
const deleteSpeed = 50;
11441154
const pauseTime = 2000;
11451155

11461156
function typeWriter() {
1147-
const currentQuote = quotes[quoteIndex];
1157+
if (currentGraphemes.length === 0) {
1158+
currentGraphemes = toGraphemes(quotes[quoteIndex]);
1159+
}
11481160

11491161
if (isDeleting) {
1150-
element.textContent = currentQuote.substring(0, charIndex - 1);
11511162
charIndex--;
1163+
element.textContent = currentGraphemes.slice(0, charIndex).join('');
11521164
} else {
1153-
element.textContent = currentQuote.substring(0, charIndex + 1);
11541165
charIndex++;
1166+
element.textContent = currentGraphemes.slice(0, charIndex).join('');
11551167
}
11561168

11571169
let delay = isDeleting ? deleteSpeed : typeSpeed;
11581170

1159-
if (!isDeleting && charIndex === currentQuote.length) {
1171+
if (!isDeleting && charIndex === currentGraphemes.length) {
11601172
delay = pauseTime;
11611173
isDeleting = true;
11621174
} else if (isDeleting && charIndex === 0) {
11631175
isDeleting = false;
11641176
quoteIndex = (quoteIndex + 1) % quotes.length;
1177+
currentGraphemes = toGraphemes(quotes[quoteIndex]);
11651178
delay = 500;
11661179
}
11671180

0 commit comments

Comments
 (0)