问答详情
源自:2-2 屏幕坐标和canvas坐标的转换

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

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();
}


提问者:开普勒 2016-02-26 16:00

个回答

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

    什么错误啊?贴出来