English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Die Methode measureText() gibt Informationen über den TextMetrics-Objekt zurück, der die gemessene Textinformationen enthält (z.B. seine Breite).
Überprüfen Sie die Breite des Texts, bevor Sie auf dem Canvas schreiben:
<!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 ‹/›
IEFirefoxOperaChromeSafari
Internet Explorer 9Firefox, Opera, Chrome and Safari support measureText() method.
Note:Internet Explorer 8 and earlier versions do not support the <canvas> element.
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 | Description |
---|---|
text | Text to be measured. |