{"id":2734,"date":"2022-06-24T15:04:20","date_gmt":"2022-06-24T13:04:20","guid":{"rendered":"https:\/\/ekiwi.de\/?p=2734"},"modified":"2022-06-24T15:04:20","modified_gmt":"2022-06-24T13:04:20","slug":"html-canvas-text-linie-text-ausgeben","status":"publish","type":"post","link":"https:\/\/ekiwi.de\/index.php\/2734\/html-canvas-text-linie-text-ausgeben\/","title":{"rendered":"HTML-Canvas: Text, Linie, Text ausgeben"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Inhalt<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/ekiwi.de\/index.php\/2734\/html-canvas-text-linie-text-ausgeben\/#Linie_zeichnen\" >Linie zeichnen<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/ekiwi.de\/index.php\/2734\/html-canvas-text-linie-text-ausgeben\/#Kreis_zeichnen\" >Kreis zeichnen<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/ekiwi.de\/index.php\/2734\/html-canvas-text-linie-text-ausgeben\/#Text_ausgeben\" >Text ausgeben<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/ekiwi.de\/index.php\/2734\/html-canvas-text-linie-text-ausgeben\/#Bespiel_online_anschauen_und_testen\" >Bespiel online anschauen und testen<\/a><\/li><\/ul><\/nav><\/div>\n<p>Kleine Beispiele f\u00fcr den Umgang mit dem HTML-Canvas Element.<\/p>\n<p><!--more--><\/p>\n<p>Zeichen mit dem Canvas-Element. Einige Beispiele.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Linie_zeichnen\"><\/span>Linie zeichnen<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Im ersten Beispiel zeichen wir eine einfache Linie. <\/p>\n<pre>&lt;canvas id=&quot;canvasLine&quot; width=&quot;250&quot; height=&quot;150&quot; style=&quot;border: 1px solid #cccccc&quot;&gt;&lt;\/canvas&gt;\r\n \r\n&lt;script&gt;\r\n    {\r\n        var c = document.getElementById(&quot;canvasLine&quot;);\r\n        let ctx = c.getContext(&quot;2d&quot;);\r\n        ctx.moveTo(0, 0);\r\n        ctx.lineTo(250, 150);\r\n        ctx.stroke();\r\n    }\r\n&lt;\/script&gt;<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/wp-content\/uploads\/2022\/06\/canvas_1.png\" alt=\"\" width=\"266\" height=\"166\" class=\"alignnone size-full wp-image-2735\" \/><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Kreis_zeichnen\"><\/span>Kreis zeichnen<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Nun ein Kreis, wir k\u00f6nnen hier auch die Farbe setzen und die Linienst\u00e4rke. <\/p>\n<pre>&lt;canvas id=&quot;canvasCircle&quot; width=&quot;250&quot; height=&quot;150&quot; style=&quot;border: 1px solid #cccccc&quot;&gt;&lt;\/canvas&gt;\r\n \r\n&lt;script&gt;\r\n    {\r\n        var c = document.getElementById(&quot;canvasCircle&quot;);\r\n        let ctx = c.getContext(&quot;2d&quot;);\r\n        ctx.strokeStyle = &quot;#FF0000&quot;;\r\n        ctx.lineWidth = 5;\r\n        ctx.beginPath();\r\n        ctx.arc(100, 75, 40, 0, 2 * Math.PI);\r\n        ctx.stroke();\r\n    }\r\n&lt;\/script&gt;<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/wp-content\/uploads\/2022\/06\/canvas_2.png\" alt=\"\" width=\"273\" height=\"169\" class=\"alignnone size-full wp-image-2736\" \/><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Text_ausgeben\"><\/span>Text ausgeben<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Auch Text l\u00e4sst sich ausgeben, auch hier k\u00f6nnen wir die Farbe setzen. Die Schriftart muss auf dem jeweiligen Computer vorhanden sein, damit die Anzeige funktioniert.<\/p>\n<pre>&lt;script&gt;\r\n    {\r\n        var c = document.getElementById(&quot;canvasText&quot;);\r\n        var ctx = c.getContext(&quot;2d&quot;);\r\n        ctx.fillStyle = &quot;#00FF00&quot;;\r\n        ctx.font = &quot;30px Comic Sans MS&quot;;\r\n        ctx.fontColor = &quot;#FF0000&quot;;\r\n        ctx.fillText(&quot;Hello World&quot;, 50, 50);\r\n    }\r\n&lt;\/script&gt;<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/wp-content\/uploads\/2022\/06\/canvas_3.png\" alt=\"\" width=\"276\" height=\"172\" class=\"alignnone size-full wp-image-2737\" \/><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Bespiel_online_anschauen_und_testen\"><\/span>Bespiel online anschauen und testen<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><a href=\"https:\/\/ekiwi.de\/tryit\/?id=211\" target=\"_blank\" rel=\"noopener\">Das gesamte Beispiel gibt es hier zum Testen<\/a><\/p>","protected":false},"excerpt":{"rendered":"<p>Kleine Beispiele f\u00fcr den Umgang mit dem HTML-Canvas Element.<\/p>\n","protected":false},"author":1,"featured_media":2262,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_links_to":"","_links_to_target":""},"categories":[5],"tags":[726,4],"class_list":["post-2734","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-hmtl","tag-canvas","tag-html"],"_links":{"self":[{"href":"https:\/\/ekiwi.de\/index.php\/wp-json\/wp\/v2\/posts\/2734","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ekiwi.de\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ekiwi.de\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ekiwi.de\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ekiwi.de\/index.php\/wp-json\/wp\/v2\/comments?post=2734"}],"version-history":[{"count":0,"href":"https:\/\/ekiwi.de\/index.php\/wp-json\/wp\/v2\/posts\/2734\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ekiwi.de\/index.php\/wp-json\/wp\/v2\/media\/2262"}],"wp:attachment":[{"href":"https:\/\/ekiwi.de\/index.php\/wp-json\/wp\/v2\/media?parent=2734"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ekiwi.de\/index.php\/wp-json\/wp\/v2\/categories?post=2734"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ekiwi.de\/index.php\/wp-json\/wp\/v2\/tags?post=2734"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}