Uncaught TypeError: Cannot set property 'fillStyle' of undefined
と表示されて値をセットできません。なぜ""で囲んだ値がundefinedになるかがわかりません。
インスタンス.プロパティ(getContext('2d')の戻り値).fillStyleだと、fillStyleがcanvas要素ではなくて、インスタンス.プロパティのプロパティを指してしまうのでしょうか?しかし、そのようものをクラスでは定義していません。
どうか、このundefinedの原因を教えて下さい。
こちらがソースコードとなります。
<!DOCTYPE html> <html lang="ja"> <head> <meta charset="UTF-8"> <title>Canvasの練習</title> <script src="../jquery-1.10.2.min.js"></script> <script> $(function() { var canvas = new draw("#canvas", 10, "#ffff00", "#0000ff"); canvas.ctx.fillStyle = "red"; canvas.ctx.fillRect(0, 0, this.ctx.width, this.ctx.height); function draw(selector, squareSide, firstColor, secondColor) { // canvas要素のオブジェクトを取得 this.canvas = $(selector).get(0); //未対応ブラウザの対処 if (!this.canvas || !this.canvas.getContext) return false; // 描画コンテキストを取得 this.ctx = this.canvas.getContext('2d'); } }); </script> </head> <body> <canvas id="mycanvas" width="600" height="600"></canvas> </body> </html>