html5 上标,HTML5 Canvas +下标和上标
答案和評論是完美的,我想補充一點,你可以通過將字符代碼移動8272輕松地將數字轉換為下標,這對應于“0”(代碼8320)的字符代碼與為“0”(代碼48)。
例如:
var text = "N1234567890";
function subNums(str)
{
var newStr = "";
for (var i=0; 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);
這顯然只是一個將所有數字轉換為下標的快速示例,而不一定是您一直想要的數字!
更有用的可能是直接將數值轉換為下標,您可以遍歷所有數字并創建一個字符串,其字符在“0”(代碼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);
總結
以上是生活随笔為你收集整理的html5 上标,HTML5 Canvas +下标和上标的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: Matplotlib进阶教程:布局讲解
- 下一篇: 安装时总是显示“$(DllSelfReg
