aboutsummaryrefslogtreecommitdiff
path: root/files/de/web/api/canvas_api/tutorial/drawing_text
diff options
context:
space:
mode:
authorFlorian Merz <me@fiji-flo.de>2021-02-11 14:45:38 +0100
committerFlorian Merz <me@fiji-flo.de>2021-02-11 14:45:38 +0100
commit4ab365b110f2f1f2b736326b7059244a32115089 (patch)
treec3c7c0219f728ade49a78c238c51cc0c8d06ebd6 /files/de/web/api/canvas_api/tutorial/drawing_text
parent8260a606c143e6b55a467edf017a56bdcd6cba7e (diff)
downloadtranslated-content-4ab365b110f2f1f2b736326b7059244a32115089.tar.gz
translated-content-4ab365b110f2f1f2b736326b7059244a32115089.tar.bz2
translated-content-4ab365b110f2f1f2b736326b7059244a32115089.zip
unslug de: move
Diffstat (limited to 'files/de/web/api/canvas_api/tutorial/drawing_text')
-rw-r--r--files/de/web/api/canvas_api/tutorial/drawing_text/index.html165
1 files changed, 165 insertions, 0 deletions
diff --git a/files/de/web/api/canvas_api/tutorial/drawing_text/index.html b/files/de/web/api/canvas_api/tutorial/drawing_text/index.html
new file mode 100644
index 0000000000..1cd3f0bfc6
--- /dev/null
+++ b/files/de/web/api/canvas_api/tutorial/drawing_text/index.html
@@ -0,0 +1,165 @@
+---
+title: Text zeichnen
+slug: Web/Guide/HTML/Canvas_Tutorial/Drawing_text
+tags:
+ - Canvas
+ - Fortgeschritten
+ - Grafik
+ - Tutorial
+translation_of: Web/API/Canvas_API/Tutorial/Drawing_text
+---
+<div>{{CanvasSidebar}} {{PreviousNext("Web/API/Canvas_API/Tutorial/Applying_styles_and_colors", "Web/API/Canvas_API/Tutorial/Using_images")}}</div>
+
+<div class="summary">
+<p>Nachdem wir im vorigen Kapitel gesehen haben, wie man <a href="/en-US/docs/Web/API/Canvas_API/Tutorial/Applying_styles_and_colors">Gestaltung und Farben anwendet</a> , werden wir nun einen Blick darauf werfen, wie man Text auf ein <code>canvas</code> zeichnet.</p>
+</div>
+
+<h2 id="Text_zeichnen">Text zeichnen</h2>
+
+<p>Der Rendering-Kontext hält zwei Methoden zum zeichnen von Text bereit:</p>
+
+<dl>
+ <dt>{{domxref("CanvasRenderingContext2D.fillText", "fillText(text, x, y [, maxWidth])")}}</dt>
+ <dd>Füllt einen gegebenen Text an den gegebenen (x,y)-Koordinaten. Optional mit einer maximalen Breite, die zu zeichnen ist.</dd>
+ <dt>{{domxref("CanvasRenderingContext2D.strokeText", "strokeText(text, x, y [, maxWidth])")}}</dt>
+ <dd>Umrandet einen gegebenen Text an den gegebenen (x,y)-Koordinaten. Optional mit einer maximalen Breite, die zu zeichnen ist.</dd>
+</dl>
+
+<h3 id="ein_fillText-Beispiel">ein <code>fillText</code>-Beispiel</h3>
+
+<p>Der Text wird mit dem aktuellen <code>fillStyle</code> gefüllt.</p>
+
+<pre class="brush: js;highlight[4]">function draw() {
+ var ctx = document.getElementById('canvas').getContext('2d');
+ ctx.font = '48px serif';
+ ctx.fillText('Hello world', 10, 50);
+}</pre>
+
+<div class="hidden">
+<pre class="brush: html">&lt;canvas id="canvas" width="300" height="100"&gt;&lt;/canvas&gt;</pre>
+
+<pre class="brush: js">draw();</pre>
+</div>
+
+<p>{{EmbedLiveSample("A_fillText_example", 310, 110)}}</p>
+
+<h3 id="ein_strokeText-Beispiel">ein <code>strokeText</code>-Beispiel</h3>
+
+<p>Der Text wird mit dem aktuellen <code>strokeStyle</code> umrandet.</p>
+
+<pre class="brush: js;highlight[4]">function draw() {
+ var ctx = document.getElementById('canvas').getContext('2d');
+ ctx.font = '48px serif';
+ ctx.strokeText('Hello world', 10, 50);
+}</pre>
+
+<div class="hidden">
+<pre class="brush: html">&lt;canvas id="canvas" width="300" height="100"&gt;&lt;/canvas&gt;</pre>
+
+<pre class="brush: js">draw();</pre>
+</div>
+
+<p>{{EmbedLiveSample("A_strokeText_example", 310, 110)}}</p>
+
+<h2 id="Text_gestalten">Text gestalten</h2>
+
+<p>In den obigen Beispielen nutzen wir bereits die <code>font</code>-Eigentschaft, um den Text ein wenig größer als standardmäßig zu machen. Es gibt ein paar mehr Eigenschaften, die es erlauben, das Zeichnen von Text auf dem <code>canvas</code> zu beeinflussen:</p>
+
+<dl>
+ <dt>{{domxref("CanvasRenderingContext2D.font", "font = value")}}</dt>
+ <dd>Der aktuell genutzte Text-Stil, der zum Zeichnen genutzt wird. Dieser String nutzt die selbe Syntax wie die <a href="/en-US/docs/Web/CSS">CSS</a> {{cssxref("font")}}-Eigenschaft. Die Standard-Schriftart ist <code>10px sans-serif</code>.</dd>
+ <dt>{{domxref("CanvasRenderingContext2D.textAlign", "textAlign = value")}}</dt>
+ <dd>Einstellung der Text-Ausrichtung. Mögliche Werte: <code>start</code>, <code>end</code>, <code>left</code>, <code>right</code> oder <code>center</code>. Der Standard-Wert ist <code>start</code>.</dd>
+ <dt>{{domxref("CanvasRenderingContext2D.textBaseline", "textBaseline = value")}}</dt>
+ <dd>Baseline alignment setting. Possible values: <code>top</code>, <code>hanging</code>, <code>middle</code>, <code>alphabetic</code>, <code>ideographic</code>, <code>bottom</code>. The default value is <code>alphabetic</code>.</dd>
+ <dt>{{domxref("CanvasRenderingContext2D.direction", "direction = value")}}</dt>
+ <dd>Directionality. Possible values: <code>ltr</code>, <code>rtl</code>, <code>inherit</code>. The default value is <code>inherit</code>.</dd>
+</dl>
+
+<p>These properties might be familiar to you, if you have worked with CSS before.</p>
+
+<p>The following diagram from the <a class="external" href="http://www.whatwg.org/" title="http://www.whatwg.org/">WHATWG</a> demonstrates the various baselines supported by the <code>textBaseline</code> property.<img alt="The top of the em square is
+roughly at the top of the glyphs in a font, the hanging baseline is
+where some glyphs like आ are anchored, the middle is half-way
+between the top of the em square and the bottom of the em square,
+the alphabetic baseline is where characters like Á, ÿ,
+f, and Ω are anchored, the ideographic baseline is
+where glyphs like 私 and 達 are anchored, and the bottom
+of the em square is roughly at the bottom of the glyphs in a
+font. The top and bottom of the bounding box can be far from these
+baselines, due to glyphs extending far outside the em square." src="http://www.whatwg.org/specs/web-apps/current-work/images/baselines.png"></p>
+
+<h3 id="A_textBaseline_example">A textBaseline example</h3>
+
+<p>Edit the code below and see your changes update live in the canvas:</p>
+
+<pre class="brush: js;highlight[2]">ctx.font = '48px serif';
+ctx.textBaseline = 'hanging';
+ctx.strokeText('Hello world', 0, 100);
+</pre>
+
+<div class="hidden">
+<h6 id="Playable_code" name="Playable_code">Playable code</h6>
+
+<pre class="brush: html">&lt;canvas id="canvas" width="400" height="200" class="playable-canvas"&gt;&lt;/canvas&gt;
+&lt;div class="playable-buttons"&gt;
+  &lt;input id="edit" type="button" value="Edit" /&gt;
+  &lt;input id="reset" type="button" value="Reset" /&gt;
+&lt;/div&gt;
+&lt;textarea id="code" class="playable-code"&gt;
+ctx.font = "48px serif";
+ctx.textBaseline = "hanging";
+ctx.strokeText("Hello world", 0, 100);&lt;/textarea&gt;
+</pre>
+
+<pre class="brush: js">var canvas = document.getElementById('canvas');
+var ctx = canvas.getContext('2d');
+var textarea = document.getElementById('code');
+var reset = document.getElementById('reset');
+var edit = document.getElementById('edit');
+var code = textarea.value;
+
+function drawCanvas() {
+ ctx.clearRect(0, 0, canvas.width, canvas.height);
+ eval(textarea.value);
+}
+
+reset.addEventListener('click', function() {
+ textarea.value = code;
+ drawCanvas();
+});
+
+edit.addEventListener('click', function() {
+ textarea.focus();
+})
+
+textarea.addEventListener('input', drawCanvas);
+window.addEventListener('load', drawCanvas);
+</pre>
+</div>
+
+<p>{{ EmbedLiveSample('Playable_code', 700, 360) }}</p>
+
+<h2 id="Advanced_text_measurements">Advanced text measurements</h2>
+
+<p>In the case you need to obtain more details about the text, the following method allows you to measure it.</p>
+
+<dl>
+ <dt>{{domxref("CanvasRenderingContext2D.measureText", "measureText()")}}</dt>
+ <dd>Returns a {{domxref("TextMetrics")}} object containing the width, in pixels, that the specified text will be when drawn in the current text style.</dd>
+</dl>
+
+<p>The following code snippet shows how you can measure a text and get its width.</p>
+
+<pre class="brush: js;highlight[3]">function draw() {
+ var ctx = document.getElementById('canvas').getContext('2d');
+ var text = ctx.measureText('foo'); // TextMetrics object
+ text.width; // 16;
+}
+</pre>
+
+<h2 id="Gecko-specific_notes">Gecko-specific notes</h2>
+
+<p>In Gecko (the rendering engine of Firefox, Firefox OS and other Mozilla based applications), some <a href="https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D#Prefixed_APIs">prefixed APIs</a> were implemented in earlier versions to draw text on a canvas. These are now deprecated and removed, and are no longer guaranteed to work.</p>
+
+<p>{{PreviousNext("Web/API/Canvas_API/Tutorial/Applying_styles_and_colors", "Web/API/Canvas_API/Tutorial/Using_images")}}</p>