注册 登录
编程论坛 JavaScript论坛

document.getElementById('canvas_main')获取绘图上下文总是失败

zhaoyizhao 发布于 2011-11-10 16:18, 778 次点击
相关代码如下:
function DrawSpecialClass(spclBodyInfList){
  this.ud = new Object();
  this.ud.pgraph = [ ];  
  this.ud.pdex = [ ];
  this.ud.pindex = [ ];
  this.ud.pvex = [ ];
  this.ud.pval = [ ];
  this.ud.mainCanvas;
  this.ud.mainCt;
}
DrawSpecialClass.prototype.Initpage=function(){

this.ud.mainCanvas = document.getElementById('canvas_main');
this.ud.mainCt=this.ud.mainCanvas.getContext('2d');
this.ud.mainCt.strokeStyle = '#000';
this.ud.mainCt.clearRect(0,0,this.canvasWidth,this.canvasHeight);

};

var DrawSpl=new DrawSpecialClass(spclBodyInfList);
DrawSpl.Initpage();

且已经定义画布如下:
<canvas id="canvas_main" width="1200" height="600" style="border:10px solid #97694F;"></canvas>

提示错误:
this.ud.mainCanvas is null
[在此错误处中断] this.ud.mainCt=this.ud.mainCanvas.getContext('2d');  
4 回复
#2
zhaoyizhao2011-11-10 16:18
是不是在类中不可以使用 document.getElementById('canvas_main'); 来获取画布?
#3
zhaoyizhao2011-11-10 16:23
求各位大侠指点
#4
da_9272011-12-27 09:15
<script type="text/javascript">
var c=document.getElementById("myCanvas");
var cxt=c.getContext("2d");
    cxt.beginPath();
    cxt.arc(x,y,radius,Math.PI,3*Math.PI/2,false);
    cxt.arc(c.width-x,y,radius,3*Math.PI/2,2*Math.PI,false);
    cxt.arc(c.width-x,c.height-y,radius,0,Math.PI/2,false);
    cxt.arc(x,c.height-y,radius,Math.PI/2,Math.PI,false);
    cxt.lineTo(x-radius,y);
    cxt.fill();
#5
da_9272011-12-27 09:16
var c=document.getElementById("myCanvas");
var cxt=c.getContext("2d");
1