我是新的HTML5,我想創建一個事件監聽器在我的鼠標,我寫了下面的代碼,但不能理解Y,我不能創建事件監聽器在我的畫布元素,好心幫在HTML5問題的畫布上的事件監聽器
var canvasDiv = document.getElementById('canvas');
canvas_simple = document.createElement('canvas');
canvas_simple.setAttribute('height', canvasHeight);
canvas_simple.setAttribute('id', 'canvasSimple');
canvasDiv.appendChild(canvas_simple);
if(typeof G_vmlCanvasManager != 'undefined')
{
canvas_simple = G_vmlCanvasManager.initElement(canvas_simple);
}
context_simple = canvas_simple.getContext("2d");
context_simple.addEventListener('mousemove', ev_mousemove, false);
在答我想給我的事件監聽器代碼也輕,可能是它有一個錯誤也
var started = false;
function ev_mousemove (ev) {
var x, y;
if (ev.layerX || ev.layerX == 0) { // Firefox
x = ev.layerX;
y = ev.layerY;
}
else if (ev.offsetX || ev.offsetX == 0) { // Opera
x = ev.offsetX;
y = ev.offsetY;
}
if (!started) {
context.beginPath();
context.moveTo(x, y);
started = true;
}
else {
context.strokeStyle = "#df4b26";
context.lineJoin = "round";
context.lineWidth = 10;
context.lineTo(x, y);
context.stroke();
}
}
我即使監聽器不工作:s事件監聽器代碼爲 –
var started = false;函數ev_mousemove(ev){var x,y; //獲取鼠標相對於畫布元素的位置。 if(ev.layerX || ev.layerX == 0){// Firefox x = ev.layerX; y = ev.layerY; } \t else if(ev.offsetX || ev.offsetX == 0){// Opera x = ev.offsetX; y = ev.offsetY; } //事件處理程序的工作方式類似於跟蹤鼠標移動的繪圖筆。我們開始繪製一條由線組成的路徑。 if(!started){ –
context.beginPath(); context.moveTo(x,y); started = true; } \t else { \t \t context.strokeStyle =「#df4b26」; \t \t context.lineJoin =「round」; context.lineWidth = 10; context.lineTo(x,y); context.stroke(); } } –