Latest web development tutorials

Metoda HTML canvas measureText ()

HTML Instrukcja płótno referencyjny HTML Instrukcja płótno referencyjny

Przykłady

Przed wyjściem tekstu płótno, sprawdzić szerokość czcionki:

YourbrowserdoesnotsupporttheHTML5canvastag.

JavaScript:

var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
ctx.font="30px Arial";
var txt="Hello World"
ctx.fillText("width:" + ctx.measureText(txt).width,10,50)
ctx.fillText(txt,10,100);

Spróbuj »

Pomoc Browser

Internet ExplorerFirefoxOperaGoogle ChromeSafari

Internet Explorer 9, Firefox, Opera, Chrome i Safari wsparcie measureText metoda ().

Uwaga: 8 i wcześniejsze wersje programu Internet Explorer nie obsługuje <canvas> elementu.


Definicja i Wykorzystanie

Metoda measureText () zwraca obiekt, który zawiera określony czcionki w pikselach szerokości.

Wskazówka: Jeśli musisz tekstu wyjściowego na płótnie wcześniej, rozumiem szerokość tekstu, należy użyć tej metody.

Składnia JavaScript: Kontekst.measureText (tekst) .width;

parametr Wartość

参数 描述
text 要测量的文本。


HTML Instrukcja płótno referencyjny HTML Instrukcja płótno referencyjny