TypeError:canvas.width()和canvas.height()不是函数

时间:2018-07-17 06:25:03

标签: javascript jquery html5-canvas

我编写了一个简单的jQuery代码,但出现此错误:

  

TypeError:canvas.width()和canvas.height()不是函数

在声明变量wh时显示错误canvas.width(),并且canvas.height()未定义。这是我的代码:

$(document).ready(function() {  
  var canvas = $('#canvas')[0];  
  var ctx = canvas.getContext('2d');  
  var w = canvas.width();  
  var h = canvas.height();  
  var cw = 15;  
  var d = "right";  
  var food;  
  var score;  
  var speed = 130;
)};

1 个答案:

答案 0 :(得分:0)

替换

var w=canvas.width();  
var h=canvas.height();  

使用

var w=canvas.width;  
var h=canvas.height; 

示例是

var canvas = document.getElementsByTagName('canvas')[0];
canvas.width  = 800;
canvas.height = 600;