English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

HTML Referenzhandbuch

Vollständige Liste der HTML-Tags

HTML canvas measureText() Methode

Die Methode measureText() gibt Informationen über den TextMetrics-Objekt zurück, der die gemessene Textinformationen enthält (z.B. seine Breite).

HTML canvas Reference Manual

Online-Beispiel

Überprüfen Sie die Breite des Texts, bevor Sie auf dem Canvas schreiben:

Ihr Browser unterstützt HTML nicht5 canvas-Tag.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Verwendung der Methode measureText() im HTML Canvas-Einführung in die Grundlagen3codebox.com)</title>/<title>
</<head>
<body>
<canvas id="myCanvas" width="300" height="150" style="border:1px fest #d3d3d3>
Ihr Browser unterstützt HTML nicht5 canvas-Tag
</canvas>
<script>
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.font = "30px Arial";
var txt = "Einführung in die Grundlagen"3codebox.com"
ctx.fillText("width:") + ctx.measureText(txt).width, 10, 50)
ctx.fillText(txt, 10, 100);
</script>
</body>
</html>
Test See ‹/›

Browser Compatibility

IEFirefoxOperaChromeSafari

Internet Explorer 9Firefox, Opera, Chrome and Safari support measureText() method.

Note:Internet Explorer 8 and earlier versions do not support the <canvas> element.

Definition and Usage

The measureText() method returns an object that contains the width of the specified text (in pixels).

Tip:If you need to know the width of the text before outputting it to the canvas, please use this method.

JavaScript Syntax:context.measureText(text).width;

Parameter Value

 
ParameterDescription
textText to be measured.
HTML canvas Reference Manual