Domanda

Vorrei compilare testo in tela come opzioni Subsccript e apice. Come faccio a raggiungere questo obiettivo.

Si prega di aiuto.

È stato utile?

Soluzione

Dal momento che non sono autorizzati a utilizzare HTML in drawText non è possibile utilizzare <sup> e sub. Invece hanno a che fare da soli.

In altre parole, quando si vuole apice è necessario cambiare il tipo di carattere ad essere più piccoli e sia disegnare il testo in una posizione y superiore o altro set textBaseline = "top". Per pedice si dovrà fare simile.

In caso contrario, è possibile utilizzare unicode. Per esempio, è valida per scrivere:

ctx.fillText('x₂', 50, 50);, ctx.fillText('normalText0123₀₁₂₃₄₅₆₇₈₉', 50, 50);, ecc.

Altri suggerimenti

La risposta e commenti sono perfetta, ho voluto aggiungere che si può facilmente convertire i numeri a quelli pedice spostando il codice di carattere da 8272, che corrisponde alla differenza tra il codice char per "0" (codice 8320) e che per "0" (codice 48).

Ad esempio:

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>

E 'ovviamente solo un esempio veloce che converte tutti i numeri a pedice, non necessariamente quello che ci vogliono sempre!

qualcosa di più utile potrebbe essere quella di convertire un valore numerico a un pedice direttamente, è possibile scorrere tutte le cifre e creare una stringa con caratteri tra "0" (codice 8320) e "9" (codice 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>

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top