Ответ и комментарии прекрасны, я хотел бы добавить, что вы можете легко преобразовать числа в индексы, сдвинув код символа на 8272, что соответствует разнице между кодом символа для «₀» (код 8320) и темдля "0" (код 48).
Например:
var text = "N1234567890";
function subNums(str)
{
var newStr = "";
for (var i=0; i<str.length; i++)
{
// Get the code of the current character
var code = str.charCodeAt(i);
if (code >= 48 && code <= 57)
{
// If it's between "0" and "9", offset the code ...
newStr += String.fromCharCode(code + 8272);
}
else
{
// ... otherwise keep the character
newStr += str[i];
}
}
return newStr
}
// Get the context
var ctx = document.getElementById('myCanvas').getContext('2d');
// Write the string
ctx.font = "20px serif";
ctx.fillText(text, 0, 20);
ctx.fillText(subNums(text), 0, 40);
<canvas id='myCanvas' width='200' height='50'></canvas>
Очевидно, что это всего лишь быстрый пример, который преобразует все числа в индекс, не обязательно то, что вы всегда хотели бы!
Что-то более полезное можетЧтобы непосредственно преобразовать числовое значение в нижний индекс, вы можете перебрать все цифры и создать строку с символами между «₀» (код 8320) и «₉» (код 8329):
// Numerical value to use as subscript
// Don't start it with 0 otherwise it will be read as an octal value!
var index = 1234567890;
function toSub(value)
{
var str = "";
// Get the number of digits, with a minimum at 0 in case the value itself is 0
var mag = Math.max(0, Math.floor(Math.log10(value)));
// Loop through all digits
while (mag >= 0)
{
// Current digit's value
var digit = Math.floor(value/Math.pow(10, mag))%10;
// Append as subscript character
str += String.fromCharCode(8320 + digit);
mag--;
}
return str;
}
// Get the context
var ctx = document.getElementById('myCanvas').getContext('2d');
// Write the string
ctx.font = "20px serif";
ctx.fillText("N" + index, 0, 20);
ctx.fillText("N" + toSub(index), 0, 40);
<canvas id='myCanvas' width='200' height='50'></canvas>