「文字カーニングペア」などを考慮して、以下のように書いています。それはそれを考慮に入れるべきであり、大まかなテストはそれがそうすることを示唆しています。それについて何かコメントがあれば、私はあなたにその主題に関する私の質問を指摘します(HTMLキャンバスに文字間隔を追加する)
基本的には、measureText()を使用して文字列全体の幅を取得し、次に文字列の最初の文字を削除して残りの文字列の幅を測定し、その差を使用して正しい位置を計算します。したがって、カーニングペアとなど。その他の擬似コードについては、所定のリンクを参照してください。
HTMLは次のとおりです。
<canvas id="Test1" width="800px" height="200px"><p>Your browser does not support canvas.</p></canvas>
コードは次のとおりです。
this.fillTextWithSpacing = function(context, text, x, y, spacing)
{
//Start at position (X, Y).
//Measure wAll, the width of the entire string using measureText()
wAll = context.measureText(text).width;
do
{
//Remove the first character from the string
char = text.substr(0, 1);
text = text.substr(1);
//Print the first character at position (X, Y) using fillText()
context.fillText(char, x, y);
//Measure wShorter, the width of the resulting shorter string using measureText().
if (text == "")
wShorter = 0;
else
wShorter = context.measureText(text).width;
//Subtract the width of the shorter string from the width of the entire string, giving the kerned width of the character, wChar = wAll - wShorter
wChar = wAll - wShorter;
//Increment X by wChar + spacing
x += wChar + spacing;
//wAll = wShorter
wAll = wShorter;
//Repeat from step 3
} while (text != "");
}
デモ/眼球テストのコード:
element1 = document.getElementById("Test1");
textContext1 = element1.getContext('2d');
textContext1.font = "72px Verdana, sans-serif";
textContext1.textAlign = "left";
textContext1.textBaseline = "top";
textContext1.fillStyle = "#000000";
text = "Welcome to go WAVE";
this.fillTextWithSpacing(textContext1, text, 0, 0, 0);
textContext1.fillText(text, 0, 100);
理想的には、複数のランダムな文字列をスローして、ピクセルごとに比較します。また、Verdanaのデフォルトのカーニングがどれほど優れているかはわかりませんが、Arialよりも優れていることは理解しています。他のフォントに関する提案はありがたいことに受け入れられています。
だから...これまでのところそれはよさそうだ。実際、それは完璧に見えます。誰かがプロセスの欠陥を指摘することをまだ望んでいます。
それまでの間、他の人がこれに対する解決策を探しているかどうかを確認するために、これをここに置きます。