我的最新版chrome48.0.2564.116 m (64-bit) 怎么提示canvas.getContext("2d")出错呢

来源:2-2 屏幕坐标和canvas坐标的转换

开普勒

2016-02-26 16:00

var canvasWidth=800;
var canvasHeight=canvasWidth;
var isMouseDown=false;
var canvas=document.getElementById("canvas");
var context=canvas.getContext("2d");
canvas.width=canvasWidth;
canvas.height=canvasHeight;
drawGrid();
canvas.onmousedown=function(e){
e.preventDefault();
isMouseDown=true;
//console.log("mouse down");
alert(e.clientX+","+e.clientY);
}
canvas.onmouseup=function(e){
e.preventDefault();
isMouseDown=false;
//console.log("mouse up");
}
canvas.onmouseout=function(e){
e.preventDefault();
isMouseDown=false;
//console.log("mouse out");
}
canvas.onmousemove=function(e){
e.preventDefault();
if(isMouseDown){
//console.log("mouse move");
//draw
}
}
function drawGrid(){
context.save();
context.strokeStyle="red";
context.beginPath();
context.moveTo(3,3);
context.lineTo(canvasWidth-3,3);
context.lineTo(canvasWidth-3,canvasHeight-3);
context.lineTo(3,canvasHeight-3);
context.closePath();
context.lineWidth=6;
context.stroke();
context.beginPath();
context.moveTo(0,0);
context.lineTo(canvasWidth,canvasHeight);
context.moveTo(canvasWidth,0);
context.lineTo(0,canvasHeight);
context.moveTo(canvasWidth/2,0);
context.lineTo(canvasWidth/2,canvasHeight);
context.moveTo(0,canvasHeight/2);
context.lineTo(canvasWidth,canvasHeight/2);
context.lineWidth=1;
context.stroke();
context.restore();
}


写回答 关注

1回答

  • qq_星稀_0
    2016-02-26 17:19:38

    什么错误啊?贴出来

    开普勒

    Uncaught TypeError: Cannot read property 'getContext' of null 这个IE 火狐都有这个,但是程序能用的。

    2016-02-26 17:22:47

    共 1 条回复 >

学写一个字

canvas系列第四课,与鼠标、触控交互,学习写出一个字

70879 学习 · 52 问题

查看课程

相似问题