1

我想将以下事件管理从键码替换为鼠标。我该怎么做呢?

目前支持的浏览器 = FF 3.6x。

// this block of code needs to be replaced
$(document).keydown(function(e) {
//console.log(e.keyCode);
switch(e.keyCode) {
case 38: // down
draw(x,y--);
break;
case 40: // up
draw(x,y++);
break;
case 37: // left
draw(x--,y);
break;
case 39: // right
draw(x++,y);
break;
default:
draw(x,y);
}
});

// to be replaced with something like the following and add control
// we need to get the x,y coordinates upon mouse click, not onload, how?
$(document).onmousemove = mouseMove;

function mouseMove(ev){
    ev           = ev || window.event;
    var mousePos = mouseCoords(ev); 
    alert( mousePos);
}

function mouseCoords(ev){
    if(ev.pageX || ev.pageY){
        return {x:ev.pageX, y:ev.pageY};
        return {x:ev.pageX};
    }
    return {
        x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
        y:ev.clientY + document.body.scrollTop  - document.body.clientTop
    };  
}


// keep
function draw(x,y) {
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");

var canvas = $("#canvas")[0];
var ctx = canvas.getContext("2d");

ctx.font = "15pt Verdana";
// var ctx.lineWidth = 1;

ctx.clearRect(0,0,500,500);

x1 = x + 50;
y1 = y + 50;
ctx.fillText("Oh my god his pant is falling down",x1,y1);

x2 = x + 100;
y2 = y + 100;
ctx.fillText("shi shi we did not see anything",x2,y2);

x3 = x + 200;
y3 = y + 200;
ctx.fillText("what a happy man!",x3,y3);
}

draw(x,y);
4

1 回答 1

0

http://dev.opera.com/articles/view/html5-canvas-painting/

基本上,您将 onmousemove 事件处理程序添加到画布,这需要处理 (X,Y) 的代码,这显然是不同浏览器中的不同属性。用于火狐的 layerX & layerY;Opera 的 offsetX 和 offsetY。

于 2010-08-10T22:55:06.680 回答