LoginSignup
metaton
@metaton (metaton Go)

Are you sure you want to delete the question?

Leaving a resolved question undeleted may help others!

javascriptで図形を描こうとしたが何も表示されない[助けて]

解決したいこと

現在javascriptでゲームを作ろうとcanvasで図形を出そうとしていたのですがなぜか何も出ずネットで調べても何も分からなかったので質問させて頂きました。

該当するソースコード

  <body>
    <canvas id="canvas" width="800" height="500"></canvas>
    <script>
      let canvas1 = document.getElementById("canvas");
      let ctx = canvas1.textContent("2d");
      window.onload = function under(){
        ctx.beginPath();
        ctx.strokeStyle = "green";
        ctx.lineWidth = 1;
        ctx.strokeRect(30,30,200,50);
        ctx.stroke();
      }
    </script>

  </body>


自分で試したこと

コードを片っ端から見直してネットでも調べましたが分かりませんでした。

0

1Answer

let ctx = canvas1.textContent("2d");

多分ここが間違い。textContentは要素のテキストを取得・設定するためのプロパティ

Node.textContentより

Node.textContent
textContent は Node のプロパティで、ノードおよびその子孫のテキストの内容を表します。

キャンバスのコンテキスト取得はgetContextメソッド

1

Comments

  1. @metaton

    Questioner
    回答ありがとうございます。
    おかげさまでちゃんと表示されました。
    とても助かりましたありがとうございます。

Your answer might help someone💌